You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Support source-phase imports (import source / import defer and dynamic import.source(...) / import.defer(...)) (terser#1682)
* add support for source-phase imports (`import source`, `import.source(...)`)
Implements the TC39 source-phase imports proposal
(https://github.com/tc39/proposal-source-phase-imports) for both static
and dynamic forms:
// static
import source wasmModule from "./foo.wasm";
import defer * as ns from "./mod.js";
// dynamic
const m = await import.source("./foo.wasm");
const m = await import.defer("./mod.js");
Without this, terser dropped the phase keyword on the mozilla-AST
round-trip used by tools like Emscripten's acorn-optimizer, silently
changing runtime semantics.
Static side:
* Parser recognises the contextual `source`/`defer` phase keyword
after `import`, with peek-based disambiguation so existing patterns
like `import source from "x"` keep parsing as default-name imports.
* AST_Import gains a `phase` field (null for plain imports).
* mozilla-ast and the printer carry phase through.
Dynamic side:
* New AST_DynamicImport node owns the whole `import.source(...)` /
`import.defer(...)` expression, including its args. It is NOT an
AST_Call. Plain `import(x)` is still parsed as an AST_Call with a
synthetic `import` SymbolRef callee.
* mozilla-ast: ImportExpression with `phase` round-trips to/from
AST_DynamicImport.
* size, equivalent-to and has_side_effects/may_throw updated.
* address review feedback: tidy comments and use (x || null) === pattern
* Update test/compress/harmony.js
* Update test/compress/harmony.js
---------
Co-authored-by: Fábio Santos <fabiosantosart@gmail.com>
phase: "[string?] Phase keyword: 'source', 'defer', or null.",
1523
1525
imported_name: "[AST_SymbolImport] The name of the variable holding the module's default export.",
1524
1526
imported_names: "[AST_NameMapping*] The names of non-default imported variables",
1525
1527
module_name: "[AST_String] String literal describing where this module came from",
@@ -1560,6 +1562,40 @@ var AST_ImportMeta = DEFNODE("ImportMeta", null, function AST_ImportMeta(props)
1560
1562
$documentation: "A reference to import.meta",
1561
1563
});
1562
1564
1565
+
varAST_DynamicImport=DEFNODE(
1566
+
"DynamicImport",
1567
+
"phase args",
1568
+
functionAST_DynamicImport(props){
1569
+
if(props){
1570
+
this.phase=props.phase;
1571
+
this.args=props.args;
1572
+
this.start=props.start;
1573
+
this.end=props.end;
1574
+
}
1575
+
1576
+
this.flags=0;
1577
+
},
1578
+
{
1579
+
$documentation: "A phased dynamic import expression: `import.source(specifier [, options])` or `import.defer(specifier [, options])`. Plain `import(x)` continues to be parsed as an AST_Call with a synthetic `import` SymbolRef callee.",
1580
+
$propdoc: {
1581
+
phase: "[string] Phase keyword ('source' or 'defer').",
1582
+
args: "[AST_Node*] specifier followed by optional options argument"
0 commit comments