Support source-phase imports in the acorn/terser optimizer pipeline#26967
Open
guybedford wants to merge 1 commit into
Open
Support source-phase imports in the acorn/terser optimizer pipeline#26967guybedford wants to merge 1 commit into
guybedford wants to merge 1 commit into
Conversation
sbc100
reviewed
May 16, 2026
Collaborator
sbc100
left a comment
There was a problem hiding this comment.
Could you add -O2 to the test_esm_source_phase_imports test? Maybe using @parameterized? I assume if you do this without this patch the test would crash?
2801da8 to
5fed2cc
Compare
When -sSOURCE_PHASE_IMPORTS=1, emcc emits ```js import source wasmModule from './foo.wasm'; ``` in its JS runtime. At -O2/-O3/-Os/-Oz the emitted JS is run through tools/acorn-optimizer.mjs which currently fails with a SyntaxError at parse time because acorn 8.x does not yet understand the source-phase imports proposal (https://github.com/tc39/proposal-source-phase-imports). Wire in the acorn-import-phases plugin so acorn can parse the syntax, and pull in the matching terser support (downstream of emscripten-core/terser#1) so the round-trip through from_mozilla_ast / to_mozilla_ast preserves the `phase` keyword on the way back out. Without the terser side, terser would silently drop the keyword and the host would return the module's exports namespace instead of a WebAssembly.Module, changing runtime semantics. * package.json: add acorn-import-phases dependency. * tools/acorn-optimizer.mjs: extend acorn with the plugin and use the extended parser at the parse site. * third_party/terser/terser.js: rebuilt from the emscripten-core/terser branch with source-phase imports support (PR emscripten-core/terser#1, the v5.18.2 downstream port of upstream terser PR terser/terser#1682). * test/js_optimizer/sourcePhaseImports{,-output}.js: new fixture that feeds two `import source` declarations through the JSDCE pass and checks the keyword survives. * test/test_other.py: register the fixture in test_js_optimizer, and parametrize test_esm_source_phase_imports across no-args and -O2 to exercise the optimizer pipeline.
5fed2cc to
f299e76
Compare
Collaborator
Author
Sure, I've bumped to |
sbc100
approved these changes
May 16, 2026
Collaborator
sbc100
left a comment
There was a problem hiding this comment.
lgtm, but maybe we want to wait for the terser changes to land, either in our fork or in upstream. Unless you want to land this first for some reason?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
-sSOURCE_PHASE_IMPORTS=1, emcc emitsimport source wasmModule from './foo.wasm'in its JS runtime, but at-O2+ the optimizer crashes because acorn 8.x doesn't yet understand the syntax. Even once acorn parses it, the bundled terser silently drops thesourcekeyword on the mozilla-AST round-trip, which changes runtime semantics (host returns the exports namespace instead of aWebAssembly.Module).This wires in the
acorn-import-phasesplugin so acorn parses the syntax, and pulls in matching terser support so the keyword survives printing. Terser side is emscripten-core/terser#1 (the v4.8.0 downstream port of upstream terser PR terser/terser#1682) — the vendoredthird_party/terser/terser.jshere is rebuilt from that branch.New fixture
test/js_optimizer/sourcePhaseImports.jsruns twoimport sourcedeclarations through JSDCE and checks the keyword survives; runs via./test/runner other.test_js_optimizer_sourcePhaseImports.That terser PR should ideally land first so this can be rebuilt from a merged branch; happy to rebase the bundle afterwards.