Skip to content

Add a rollup plugin for tree shaking Emscripten output. #23964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ tools/coverage/
htmlcov/
coverage.xml

/tools/rollup-plugin-emscripten/node_modules

.DS_Store

# Test output
Expand Down
4 changes: 4 additions & 0 deletions bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
'package.json',
'package-lock.json'
], ['npm', 'ci']),
('npm rollup plugin packages', [
'tools/rollup-plugin-emscripten/package.json',
'tools/rollup-plugin-emscripten/package-lock.json'
], ['npm', 'ci', '--prefix', 'tools/rollup-plugin-emscripten']),
('create entry points', [
'tools/maint/create_entry_points.py',
'tools/maint/run_python.bat',
Expand Down
6 changes: 6 additions & 0 deletions test/rollup_plugin/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import init, { _used_externally } from './library.mjs';

await init();

console.log(_used_externally());
console.log('done');
16 changes: 16 additions & 0 deletions test/rollup_plugin/library.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <emscripten.h>

// Should NOT be removed (used by index.mjs).
EMSCRIPTEN_KEEPALIVE int used_externally() {
return 42;
}

// Should NOT be removed (used by library.js).
EMSCRIPTEN_KEEPALIVE int used_internally() {
return 99;
}

// Should be removed.
EMSCRIPTEN_KEEPALIVE int unused() {
return 0xDEAD;
}
9 changes: 9 additions & 0 deletions test/rollup_plugin/library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

addToLibrary({
$placeHolder__deps: ['used_internally'],
$placeHolder: function() {
_used_internally();
}
});

extraLibraryFuncs.push('$placeHolder');
4 changes: 4 additions & 0 deletions test/rollup_plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "test-rollup-plugin",
"type": "module"
}
15 changes: 15 additions & 0 deletions test/rollup_plugin/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import emscriptenPlugin from 'rollup-plugin-emscripten';

export default {
input: 'index.mjs',
output: {
dir: 'dist',
format: 'es'
},
plugins: [
emscriptenPlugin({
'input': 'library.mjs',
'wasmMetaDCE': 'BINARYEN_PATH'
})
]
};
33 changes: 33 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -15860,6 +15860,39 @@ def test_rollup(self):
shutil.copy('hello.wasm', 'dist/')
self.assertContained('hello, world!', self.run_js('dist/bundle.mjs'))

@parameterized({
'': ('',),
'O1': ('-O1',),
'O2': ('-O2',),
'O3': ('-O3',),
})
def test_rollup_plugin(self, opt):
def get_exports(path):
with webassembly.Module(path) as module:
return [export.name for export in module.get_exports()]

# Pass the path to wasm-metadce into the plugin.
copytree(test_file('rollup_plugin'), '.')
rollup_config_file = 'rollup.config.mjs'
replacement_path = str(Path(building.get_binaryen_bin(), 'wasm-metadce'))
modified_content = read_file(rollup_config_file).replace("BINARYEN_PATH", replacement_path)
write_file(rollup_config_file, modified_content)

self.run_process([EMCC, 'library.c', opt, '--no-entry', '-sMODULARIZE=instance', '-sENVIRONMENT=node', '--js-library', 'library.js', '-o', 'library.mjs'])
self.run_process(['npm', 'install', path_from_root('tools/rollup-plugin-emscripten')])
self.run_process(shared.get_npm_cmd('rollup') + ['--config'])
exports_pre_rollup = get_exports('library.wasm')
self.assertContained('42\ndone\n', self.run_js('dist/index.js'))
exports = get_exports('dist/library.wasm')
self.assertTrue(len(exports) < len(exports_pre_rollup))
if opt != '-O3':
# In O3 wasm export names are minified.
self.assertTrue('used_externally' in exports)
self.assertTrue('used_internally' in exports)
self.assertTrue('unused' not in exports)



def test_rlimit(self):
self.do_other_test('test_rlimit.c', emcc_args=['-O1'])

Expand Down
46 changes: 46 additions & 0 deletions tools/rollup-plugin-emscripten/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tools/rollup-plugin-emscripten/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "rollup-plugin-emscripten",
"version": "0.0.1",
"description": "Rollup plugin for Emscripten output with tree shaking.",
"main": "src/index.mjs",
"dependencies": {
"tmp": "^0.2.3",
"which": "^5.0.0"
}
}
Loading