Skip to content

Commit 40616f0

Browse files
authored
[esm-integration] Fix acorn optimizer to handle ESM imports/exports (#24059)
Closure compiler still can't handle these sadly.
1 parent 629b012 commit 40616f0

File tree

5 files changed

+62
-11
lines changed

5 files changed

+62
-11
lines changed

test/js_optimizer/JSDCE-output.js

+14
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ function h(a) {
3333

3434
print(h(123));
3535

36+
export function i(a) {
37+
return a + 1;
38+
}
39+
40+
function j(a) {
41+
return a + 1;
42+
}
43+
44+
export { j as k };
45+
46+
export default function l(a) {
47+
return a + 1;
48+
}
49+
3650
(function() {
3751
var z = fleefl();
3852
var zz = fleefl();

test/js_optimizer/JSDCE.js

+17
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ function h(a) {
3131
}
3232
print(h(123));
3333

34+
// ES6 exported
35+
export function i(a) {
36+
return a+1;
37+
}
38+
39+
// ES6 export via export specifier
40+
function j(a) {
41+
return a+1;
42+
}
43+
44+
export { j as k };
45+
46+
// ES6 default export
47+
export default function l(a) {
48+
return a+1;
49+
}
50+
3451
// inner workings
3552
(function() {
3653
var x;

test/test_core.py

+13
Original file line numberDiff line numberDiff line change
@@ -9562,6 +9562,19 @@ def test_wasm_worker_malloc(self):
95629562
def test_wasm_worker_wait_async(self):
95639563
self.do_runf('atomic/test_wait_async.c', emcc_args=['-sWASM_WORKERS'])
95649564

9565+
@requires_node_canary
9566+
@no_wasm64("wasm64 requires wasm export wrappers")
9567+
def test_esm_integration(self):
9568+
# TODO(sbc): WASM_ESM_INTEGRATION doesn't currently work with closure.
9569+
# self.maybe_closure()
9570+
self.node_args += ['--experimental-wasm-modules', '--no-warnings']
9571+
self.run_process([EMCC, '-o', 'hello_world.mjs', '-sWASM_ESM_INTEGRATION', '-Wno-experimental', test_file('hello_world.c')] + self.get_emcc_args())
9572+
create_file('runner.mjs', '''
9573+
import init from "./hello_world.mjs";
9574+
await init();
9575+
''')
9576+
self.assertContained('hello, world!', self.run_js('runner.mjs'))
9577+
95659578

95669579
# Generate tests for everything
95679580
def make_run(name, emcc_args, settings=None, env=None,

test/test_other.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -367,16 +367,6 @@ def test_esm_source_phase_imports(self):
367367
self.assertContained('import source wasmModule from', read_file('hello_world.mjs'))
368368
self.assertContained('hello, world!', self.run_js('hello_world.mjs'))
369369

370-
@requires_node_canary
371-
def test_esm_integration(self):
372-
self.node_args += ['--experimental-wasm-modules', '--no-warnings']
373-
self.run_process([EMCC, '-o', 'hello_world.mjs', '-sWASM_ESM_INTEGRATION', '-Wno-experimental', test_file('hello_world.c')])
374-
create_file('runner.mjs', '''
375-
import init from "./hello_world.mjs";
376-
await init();
377-
''')
378-
self.assertContained('hello, world!', self.run_js('runner.mjs'))
379-
380370
@parameterized({
381371
'': ([],),
382372
'node': (['-sENVIRONMENT=node'],),
@@ -2941,7 +2931,7 @@ def test_extern_prepost(self):
29412931
@parameterized({
29422932
'minifyGlobals': (['minifyGlobals'],),
29432933
'minifyLocals': (['minifyLocals'],),
2944-
'JSDCE': (['JSDCE'],),
2934+
'JSDCE': (['JSDCE', '--export-es6'],),
29452935
'JSDCE-hasOwnProperty': (['JSDCE'],),
29462936
'JSDCE-defaultArg': (['JSDCE'],),
29472937
'JSDCE-fors': (['JSDCE'],),

tools/acorn-optimizer.mjs

+17
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,23 @@ function JSDCE(ast, aggressive) {
440440
const name = node.name;
441441
ensureData(scopes[scopes.length - 1], name).use = 1;
442442
},
443+
ExportDefaultDeclaration(node, c) {
444+
const name = node.declaration.id.name;
445+
ensureData(scopes[scopes.length - 1], name).use = 1;
446+
c(node.declaration);
447+
},
448+
ExportNamedDeclaration(node, c) {
449+
if (node.declaration) {
450+
const name = node.declaration.id.name;
451+
ensureData(scopes[scopes.length - 1], name).use = 1;
452+
c(node.declaration);
453+
} else {
454+
for (const specifier of node.specifiers) {
455+
const name = specifier.local.name;
456+
ensureData(scopes[scopes.length - 1], name).use = 1;
457+
}
458+
}
459+
},
443460
});
444461

445462
// toplevel

0 commit comments

Comments
 (0)