xbintsc 0.6.23 → 0.6.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/driver/bundler/concatenate.d.ts +23 -0
- package/dist/src/driver/bundler/concatenate.js +210 -0
- package/dist/src/driver/bundler/concatenate.js.map +1 -0
- package/dist/src/driver/bundler/merge.d.ts +4 -4
- package/dist/src/driver/bundler/merge.js +24 -207
- package/dist/src/driver/bundler/merge.js.map +1 -1
- package/dist/src/driver/bundler/{namespace-import.d.ts → namespace-object.d.ts} +5 -1
- package/dist/src/driver/bundler/{namespace-import.js → namespace-object.js} +6 -2
- package/dist/src/driver/bundler/namespace-object.js.map +1 -0
- package/dist/src/driver/bundler/{reexport-fixup.d.ts → reexport-resolution.d.ts} +9 -3
- package/dist/src/driver/bundler/{reexport-fixup.js → reexport-resolution.js} +16 -6
- package/dist/src/driver/bundler/reexport-resolution.js.map +1 -0
- package/dist/src/driver/modules.d.ts +12 -5
- package/dist/src/driver/modules.js +12 -5
- package/dist/src/driver/modules.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/tests/driver/deferred-reexport.test.js +25 -0
- package/dist/tests/driver/deferred-reexport.test.js.map +1 -1
- package/package.json +7 -7
- package/src/driver/bundler/README.md +55 -0
- package/src/driver/bundler/concatenate.ts +244 -0
- package/src/driver/bundler/merge.ts +24 -215
- package/src/driver/bundler/{namespace-import.ts → namespace-object.ts} +5 -1
- package/src/driver/bundler/{reexport-fixup.ts → reexport-resolution.ts} +15 -5
- package/src/driver/modules.ts +12 -5
- package/src/driver/version.ts +1 -1
- package/dist/src/driver/bundler/namespace-import.js.map +0 -1
- package/dist/src/driver/bundler/reexport-fixup.js.map +0 -1
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Build the synthetic object literal that stands in for a namespace binding.
|
|
3
|
+
*
|
|
4
|
+
* `import * as ns from "./mod"` and `export * as ns from "./mod"` are lowered
|
|
5
|
+
* to `const <final> = { exportName: finalName, ... }`, because the merged
|
|
6
|
+
* module has no runtime namespace object to point at.
|
|
3
7
|
*/
|
|
4
8
|
import { SyntaxKind, } from "../../ast/nodes.js";
|
|
5
9
|
/** Lower `import * as ns from "./mod"` to `const m0_ns = { ...exports }`. */
|
|
@@ -43,4 +47,4 @@ export function namespaceImportToStatement(statement, finalName, dependency) {
|
|
|
43
47
|
end: statement.end,
|
|
44
48
|
};
|
|
45
49
|
}
|
|
46
|
-
//# sourceMappingURL=namespace-
|
|
50
|
+
//# sourceMappingURL=namespace-object.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"namespace-object.js","sourceRoot":"","sources":["../../../../src/driver/bundler/namespace-object.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,UAAU,GAMX,MAAM,oBAAoB,CAAC;AAG5B,6EAA6E;AAC7E,MAAM,UAAU,0BAA0B,CACxC,SAAoB,EACpB,SAAiB,EACjB,UAAwB;IAExB,MAAM,UAAU,GAAyB,EAAE,CAAC;IAC5C,KAAK,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,IAAI,UAAU,CAAC,OAAO,EAAE,CAAC;QACxD,UAAU,CAAC,IAAI,CAAC;YACd,IAAI,EAAE,UAAU,CAAC,kBAAkB;YACnC,IAAI,EAAE,EAAE,IAAI,EAAE,UAAU,CAAC,UAAU,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,SAAS,CAAC,KAAK,EAAE;YACvG,WAAW,EAAE,EAAE,IAAI,EAAE,UAAU,CAAC,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,SAAS,CAAC,KAAK,EAAE;YACxG,KAAK,EAAE,SAAS,CAAC,KAAK;YACtB,GAAG,EAAE,SAAS,CAAC,KAAK;SACrB,CAAC,CAAC;IACL,CAAC;IACD,MAAM,WAAW,GAA4B;QAC3C,IAAI,EAAE,UAAU,CAAC,uBAAuB;QACxC,UAAU;QACV,KAAK,EAAE,SAAS,CAAC,KAAK;QACtB,GAAG,EAAE,SAAS,CAAC,GAAG;KACnB,CAAC;IACF,MAAM,WAAW,GAAwB;QACvC,IAAI,EAAE,UAAU,CAAC,mBAAmB;QACpC,IAAI,EAAE,EAAE,IAAI,EAAE,UAAU,CAAC,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,SAAS,CAAC,KAAK,EAAE;QACpG,WAAW,EAAE,KAAK;QAClB,WAAW;QACX,KAAK,EAAE,SAAS,CAAC,KAAK;QACtB,GAAG,EAAE,SAAS,CAAC,GAAG;KACnB,CAAC;IACF,MAAM,IAAI,GAA4B;QACpC,IAAI,EAAE,UAAU,CAAC,uBAAuB;QACxC,eAAe,EAAE,OAAgB;QACjC,YAAY,EAAE,CAAC,WAAW,CAAC;QAC3B,KAAK,EAAE,SAAS,CAAC,KAAK;QACtB,GAAG,EAAE,SAAS,CAAC,GAAG;KACnB,CAAC;IACF,OAAO;QACL,IAAI,EAAE,UAAU,CAAC,iBAAiB;QAClC,eAAe,EAAE,IAAI;QACrB,SAAS,EAAE,EAAE;QACb,KAAK,EAAE,SAAS,CAAC,KAAK;QACtB,GAAG,EAAE,SAAS,CAAC,GAAG;KACnB,CAAC;AACJ,CAAC"}
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Resolve re-export chains and rebuild namespace objects from final names.
|
|
3
3
|
*
|
|
4
4
|
* Phase 2 records `export { x } from "./dep"` by copying the dependency's
|
|
5
5
|
* export *value* at that moment; if `dep` re-exports an import, that value is
|
|
6
6
|
* the pre-rename local name and is only corrected by Phase 3's import rewrite.
|
|
7
7
|
* A multi-level chain (`index -> sdk -> tools`) therefore needs a second pass,
|
|
8
8
|
* and a namespace import reaching back across a cycle needs the final names.
|
|
9
|
-
* This
|
|
10
|
-
*
|
|
9
|
+
* This pass re-resolves module re-exports until the export maps stop changing,
|
|
10
|
+
* then rebuilds every eager namespace object from those final names.
|
|
11
|
+
*
|
|
12
|
+
* Deferred modules (`<script src>`) are the exception: their bindings live in a
|
|
13
|
+
* wrapper function, so Phase 3 already lowered any namespace binding between
|
|
14
|
+
* two deferred modules to an alias that calls `__xt_deferred_<dep>()`. Such a
|
|
15
|
+
* binding must be left alone; rebuilding it here would reference wrapper-local
|
|
16
|
+
* names from the top level (TS3002 `Cannot find name`).
|
|
11
17
|
*/
|
|
12
18
|
import { type Statement } from "../../ast/nodes.js";
|
|
13
19
|
import type { ModuleRecord } from "./types.js";
|
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Resolve re-export chains and rebuild namespace objects from final names.
|
|
3
3
|
*
|
|
4
4
|
* Phase 2 records `export { x } from "./dep"` by copying the dependency's
|
|
5
5
|
* export *value* at that moment; if `dep` re-exports an import, that value is
|
|
6
6
|
* the pre-rename local name and is only corrected by Phase 3's import rewrite.
|
|
7
7
|
* A multi-level chain (`index -> sdk -> tools`) therefore needs a second pass,
|
|
8
8
|
* and a namespace import reaching back across a cycle needs the final names.
|
|
9
|
-
* This
|
|
10
|
-
*
|
|
9
|
+
* This pass re-resolves module re-exports until the export maps stop changing,
|
|
10
|
+
* then rebuilds every eager namespace object from those final names.
|
|
11
|
+
*
|
|
12
|
+
* Deferred modules (`<script src>`) are the exception: their bindings live in a
|
|
13
|
+
* wrapper function, so Phase 3 already lowered any namespace binding between
|
|
14
|
+
* two deferred modules to an alias that calls `__xt_deferred_<dep>()`. Such a
|
|
15
|
+
* binding must be left alone; rebuilding it here would reference wrapper-local
|
|
16
|
+
* names from the top level (TS3002 `Cannot find name`).
|
|
11
17
|
*/
|
|
12
18
|
import { SyntaxKind, } from "../../ast/nodes.js";
|
|
13
|
-
import { namespaceImportToStatement } from "./namespace-
|
|
19
|
+
import { namespaceImportToStatement } from "./namespace-object.js";
|
|
14
20
|
export function resolveReexports(records, dependencyOf, namespaceStatements, deferredModules = new Set()) {
|
|
15
21
|
let changed = true;
|
|
16
22
|
// A re-export chain cannot be longer than the module count, so this bound
|
|
@@ -81,7 +87,11 @@ export function resolveReexports(records, dependencyOf, namespaceStatements, def
|
|
|
81
87
|
const bindings = clause?.namedBindings;
|
|
82
88
|
if (bindings?.kind === SyntaxKind.NamespaceImport) {
|
|
83
89
|
const dependency = dependencyOf(record, declaration.moduleSpecifier.value);
|
|
84
|
-
|
|
90
|
+
/* Phase 3 already lowered a deferred module's namespace import of
|
|
91
|
+
* another deferred module to an alias read from the dependency's
|
|
92
|
+
* wrapper. Rebuilding the object here would reference the
|
|
93
|
+
* dependency's final names, which are wrapper-local (TS3002). */
|
|
94
|
+
if (dependency && !(deferredModules.has(record.path) && deferredModules.has(dependency.path))) {
|
|
85
95
|
namespaceStatements.set(statement, namespaceImportToStatement(statement, `${record.prefix}${bindings.name.text}`, dependency));
|
|
86
96
|
}
|
|
87
97
|
}
|
|
@@ -89,4 +99,4 @@ export function resolveReexports(records, dependencyOf, namespaceStatements, def
|
|
|
89
99
|
}
|
|
90
100
|
}
|
|
91
101
|
}
|
|
92
|
-
//# sourceMappingURL=reexport-
|
|
102
|
+
//# sourceMappingURL=reexport-resolution.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reexport-resolution.js","sourceRoot":"","sources":["../../../../src/driver/bundler/reexport-resolution.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EACL,UAAU,GAKX,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,0BAA0B,EAAE,MAAM,uBAAuB,CAAC;AAEnE,MAAM,UAAU,gBAAgB,CAC9B,OAAuB,EACvB,YAAmF,EACnF,mBAA8C,EAC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,CAAC,CAAC;oBAC3E,IAAI,UAAU,EAAE,CAAC;wBACf,mBAAmB,CAAC,GAAG,CACrB,SAAS,EACT,0BAA0B,CAAC,SAAS,EAAE,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,EAAE,UAAU,CAAC,CAC/E,CAAC;oBACJ,CAAC;gBACH,CAAC;YACH,CAAC;iBAAM,IAAI,SAAS,CAAC,IAAI,KAAK,UAAU,CAAC,iBAAiB,EAAE,CAAC;gBAC3D,MAAM,WAAW,GAAG,SAA8B,CAAC;gBACnD,MAAM,MAAM,GAAG,WAAW,CAAC,YAAY,CAAC;gBACxC,IAAI,MAAM,EAAE,UAAU;oBAAE,SAAS;gBACjC,MAAM,QAAQ,GAAG,MAAM,EAAE,aAAa,CAAC;gBACvC,IAAI,QAAQ,EAAE,IAAI,KAAK,UAAU,CAAC,eAAe,EAAE,CAAC;oBAClD,MAAM,UAAU,GAAG,YAAY,CAAC,MAAM,EAAE,WAAW,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;oBAC3E;;;qFAGiE;oBACjE,IAAI,UAAU,IAAI,CAAC,CAAC,eAAe,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,eAAe,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;wBAC9F,mBAAmB,CAAC,GAAG,CACrB,SAAS,EACT,0BAA0B,CAAC,SAAS,EAAE,GAAG,MAAM,CAAC,MAAM,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,UAAU,CAAC,CAC3F,CAAC;oBACJ,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -6,11 +6,18 @@
|
|
|
6
6
|
* Each module's top-level bindings are renamed to a globally unique name and
|
|
7
7
|
* imported names are rewritten to the (renamed) exported binding.
|
|
8
8
|
*
|
|
9
|
-
* The implementation lives under `bundler/`
|
|
10
|
-
* resolve.ts
|
|
11
|
-
* graph.ts
|
|
12
|
-
*
|
|
13
|
-
*
|
|
9
|
+
* The implementation lives under `bundler/` (see `bundler/README.md`):
|
|
10
|
+
* resolve.ts specifier/path/package resolution
|
|
11
|
+
* graph.ts import-graph loading (parse + bind)
|
|
12
|
+
* merge.ts bundling pass orchestrator (phases 1-3)
|
|
13
|
+
* reexport-resolution.ts re-export chain fixpoint + namespace objects
|
|
14
|
+
* namespace-object.ts namespace object literal builder
|
|
15
|
+
* deferred.ts `<script src>` deferred module wrappers
|
|
16
|
+
* dynamic-import.ts `import()` lowering
|
|
17
|
+
* commonjs.ts CommonJS compatibility in `node_modules`
|
|
18
|
+
* concatenate.ts final concatenation (phases 4-5)
|
|
19
|
+
* symbols.ts top-level symbol renaming
|
|
20
|
+
* types.ts shared types
|
|
14
21
|
*
|
|
15
22
|
* This file remains the public entry point for the driver and tests.
|
|
16
23
|
*/
|
|
@@ -6,11 +6,18 @@
|
|
|
6
6
|
* Each module's top-level bindings are renamed to a globally unique name and
|
|
7
7
|
* imported names are rewritten to the (renamed) exported binding.
|
|
8
8
|
*
|
|
9
|
-
* The implementation lives under `bundler/`
|
|
10
|
-
* resolve.ts
|
|
11
|
-
* graph.ts
|
|
12
|
-
*
|
|
13
|
-
*
|
|
9
|
+
* The implementation lives under `bundler/` (see `bundler/README.md`):
|
|
10
|
+
* resolve.ts specifier/path/package resolution
|
|
11
|
+
* graph.ts import-graph loading (parse + bind)
|
|
12
|
+
* merge.ts bundling pass orchestrator (phases 1-3)
|
|
13
|
+
* reexport-resolution.ts re-export chain fixpoint + namespace objects
|
|
14
|
+
* namespace-object.ts namespace object literal builder
|
|
15
|
+
* deferred.ts `<script src>` deferred module wrappers
|
|
16
|
+
* dynamic-import.ts `import()` lowering
|
|
17
|
+
* commonjs.ts CommonJS compatibility in `node_modules`
|
|
18
|
+
* concatenate.ts final concatenation (phases 4-5)
|
|
19
|
+
* symbols.ts top-level symbol renaming
|
|
20
|
+
* types.ts shared types
|
|
14
21
|
*
|
|
15
22
|
* This file remains the public entry point for the driver and tests.
|
|
16
23
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"modules.js","sourceRoot":"","sources":["../../../src/driver/modules.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"modules.js","sourceRoot":"","sources":["../../../src/driver/modules.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -9,5 +9,5 @@
|
|
|
9
9
|
* `main`/`master` only (see `.githooks/pre-commit`), so feature branches never
|
|
10
10
|
* change this line and merging two of them cannot conflict here.
|
|
11
11
|
*/
|
|
12
|
-
export const COMPILER_VERSION = "0.6.
|
|
12
|
+
export const COMPILER_VERSION = "0.6.24";
|
|
13
13
|
//# sourceMappingURL=version.js.map
|
|
@@ -58,5 +58,30 @@ describe("deferred module re-exports", () => {
|
|
|
58
58
|
"main.ts": 'import { __xt_deferred_module as __xt_deferred_0_0 } from "./app.ts";',
|
|
59
59
|
})).toEqual([]);
|
|
60
60
|
});
|
|
61
|
+
it("keeps a namespace import that is one wrapper removed from the script", () => {
|
|
62
|
+
expect(unresolvedNames({
|
|
63
|
+
"lib.ts": "const answer = 42;\nexport { answer };",
|
|
64
|
+
"mid.ts": "import * as lib from './lib';\nexport function run() { return lib.answer; }",
|
|
65
|
+
"app.ts": 'import { run } from "./mid";\nconsole.log(run());',
|
|
66
|
+
"main.ts": 'import { __xt_deferred_module as __xt_deferred_0_0 } from "./app.ts";',
|
|
67
|
+
})).toEqual([]);
|
|
68
|
+
});
|
|
69
|
+
it("keeps a namespace import at the script's own top level", () => {
|
|
70
|
+
expect(unresolvedNames({
|
|
71
|
+
"lib.ts": "const answer = 42;\nexport { answer };",
|
|
72
|
+
"app.ts": "import * as lib from './lib';\nconsole.log(lib.answer);",
|
|
73
|
+
"main.ts": 'import { __xt_deferred_module as __xt_deferred_0_0 } from "./app.ts";',
|
|
74
|
+
})).toEqual([]);
|
|
75
|
+
});
|
|
76
|
+
it("still rebuilds a namespace import whose dependency stayed eager", () => {
|
|
77
|
+
expect(unresolvedNames({
|
|
78
|
+
"lib.ts": "const answer = 42;\nexport { answer };",
|
|
79
|
+
"eager.ts": "import * as lib from './lib';\nexport const seen = lib.answer;",
|
|
80
|
+
"app.ts": 'import { seen } from "./eager";\nconsole.log(seen);',
|
|
81
|
+
"main.ts": 'import { seen } from "./eager";\n' +
|
|
82
|
+
'import { __xt_deferred_module as __xt_deferred_0_0 } from "./app.ts";\n' +
|
|
83
|
+
"console.log(seen);",
|
|
84
|
+
})).toEqual([]);
|
|
85
|
+
});
|
|
61
86
|
});
|
|
62
87
|
//# sourceMappingURL=deferred-reexport.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deferred-reexport.test.js","sourceRoot":"","sources":["../../../tests/driver/deferred-reexport.test.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxE,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,qCAAqC,CAAC;AACpE,OAAO,EAAE,IAAI,EAAE,MAAM,4BAA4B,CAAC;AAClD,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AAEtE,MAAM,WAAW,GAAa,EAAE,CAAC;AAEjC,SAAS,CAAC,GAAG,EAAE;IACb,KAAK,MAAM,SAAS,IAAI,WAAW,CAAC,MAAM,CAA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AAE,GAAG,EAAE;QAChE,MAAM,CACJ,eAAe,CAAC;YACd,QAAQ,EAAE,wCAAwC;YAClD,QAAQ,EAAE,yDAAyD;YACnE,SAAS,EAAE,uEAAuE;SACnF,CAAC,CACH,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAChB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,iEAAiE,EAAE,GAAG,EAAE;QACzE,MAAM,CACJ,eAAe,CAAC;YACd,QAAQ,EAAE,wCAAwC;YAClD,UAAU,EAAE,gEAAgE;YAC5E,QAAQ,EAAE,qDAAqD;YAC/D,SAAS,EACP,mCAAmC;gBACnC,yEAAyE;gBACzE,oBAAoB;SACvB,CAAC,CACH,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAChB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xbintsc",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.24",
|
|
4
4
|
"description": "xbintsc - a TypeScript binary compiler (TypeScript -> LLVM IR -> native binary)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -80,12 +80,12 @@
|
|
|
80
80
|
"vitest": "^2.1.8"
|
|
81
81
|
},
|
|
82
82
|
"optionalDependencies": {
|
|
83
|
-
"@xbintsc/linux-x64": "0.6.
|
|
84
|
-
"@xbintsc/linux-arm64": "0.6.
|
|
85
|
-
"@xbintsc/darwin-x64": "0.6.
|
|
86
|
-
"@xbintsc/darwin-arm64": "0.6.
|
|
87
|
-
"@xbintsc/win32-x64": "0.6.
|
|
88
|
-
"@xbintsc/win32-arm64": "0.6.
|
|
83
|
+
"@xbintsc/linux-x64": "0.6.24",
|
|
84
|
+
"@xbintsc/linux-arm64": "0.6.24",
|
|
85
|
+
"@xbintsc/darwin-x64": "0.6.24",
|
|
86
|
+
"@xbintsc/darwin-arm64": "0.6.24",
|
|
87
|
+
"@xbintsc/win32-x64": "0.6.24",
|
|
88
|
+
"@xbintsc/win32-arm64": "0.6.24"
|
|
89
89
|
},
|
|
90
90
|
"engines": {
|
|
91
91
|
"node": ">=22"
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Source-level module bundler
|
|
2
|
+
|
|
3
|
+
xbintsc has no runtime module system and no module linker. The driver resolves
|
|
4
|
+
every reachable module, renames its top-level bindings to globally unique
|
|
5
|
+
`<prefix><name>` identifiers, rewrites imports to those final names, and
|
|
6
|
+
concatenates the whole graph into one source file for codegen.
|
|
7
|
+
|
|
8
|
+
The public entry point is `bundleModules()` in `../modules.ts`; everything in
|
|
9
|
+
this directory is an implementation detail of it.
|
|
10
|
+
|
|
11
|
+
## Pipeline
|
|
12
|
+
|
|
13
|
+
| Phase | Location | Work |
|
|
14
|
+
| --- | --- | --- |
|
|
15
|
+
| 1 | `merge.ts` | Record top-level bindings and pick a unique final name per symbol. |
|
|
16
|
+
| 1b | `commonjs.ts` | Give CommonJS `node_modules` files synthetic `module` / `exports` state. |
|
|
17
|
+
| 2 | `merge.ts` | Fill every module's `exports` map from declarations and re-exports. |
|
|
18
|
+
| 2b | `commonjs.ts` | Lower `require("…")` to the already-bundled dependency's exports. |
|
|
19
|
+
| 3 | `merge.ts` | Rewrite imported names; lower deferred imports and namespace bindings. |
|
|
20
|
+
| 3b | `reexport-resolution.ts` | Re-resolve re-export chains to a fixpoint; rebuild eager namespace objects. |
|
|
21
|
+
| 4 | `concatenate.ts` | Emit statements in dependency order, dropping local imports and export wrappers. |
|
|
22
|
+
| 5 | `concatenate.ts` | Apply the final rename to each top-level declaration and reference. |
|
|
23
|
+
|
|
24
|
+
Phases 4-5 live in `concatenate.ts` so `merge.ts` stays focused on name and
|
|
25
|
+
export resolution.
|
|
26
|
+
|
|
27
|
+
## Files
|
|
28
|
+
|
|
29
|
+
| File | Responsibility |
|
|
30
|
+
| --- | --- |
|
|
31
|
+
| `types.ts` | `ModuleRecord`, `BundleResult`, resolution types. |
|
|
32
|
+
| `resolve.ts` | Specifier resolution: relative paths, directory indexes, package `exports`, `node_modules`. |
|
|
33
|
+
| `graph.ts` | Load and bind the import graph in dependency order. |
|
|
34
|
+
| `symbols.ts` | Top-level symbol lookup, declaration/reference renaming, export-map remapping. |
|
|
35
|
+
| `merge.ts` | Orchestrates phases 1-3 and produces the resolved module records. |
|
|
36
|
+
| `reexport-resolution.ts` | Fixpoint over `export … from` chains; rebuilds namespace objects from final names. |
|
|
37
|
+
| `namespace-object.ts` | Synthesizes `const ns = { name: finalName, … }` for namespace imports/re-exports. |
|
|
38
|
+
| `deferred.ts` | `<script src>` deferred evaluation: wrapper functions, alias lowering, deferred-set computation. |
|
|
39
|
+
| `dynamic-import.ts` | Lowers string-literal `import()` to a promise over the target's wrapper. |
|
|
40
|
+
| `commonjs.ts` | CommonJS interop for `node_modules` sources. |
|
|
41
|
+
| `concatenate.ts` | Phases 4-5: emit the merged statement list and rename declarations. |
|
|
42
|
+
|
|
43
|
+
## Invariants
|
|
44
|
+
|
|
45
|
+
- **No live bindings.** An imported name is a snapshot of the exported value at
|
|
46
|
+
module-evaluation time.
|
|
47
|
+
- **The entry module is last** in the record order; dependencies are
|
|
48
|
+
post-order.
|
|
49
|
+
- **Deferred modules own their bindings.** A deferred module's top-level names
|
|
50
|
+
are local to its `__xt_deferred_*` wrapper. A namespace binding between two
|
|
51
|
+
deferred modules must stay an alias that reads the dependency's wrapper
|
|
52
|
+
(`deferred.ts`); it must not be rebuilt at top level from wrapper-local names
|
|
53
|
+
(TS3002). Regression coverage: `tests/driver/deferred-reexport.test.ts`.
|
|
54
|
+
- **Re-exports are settled before concatenation.** Phase 3b runs to a fixpoint
|
|
55
|
+
so multi-level `export … from` chains and cycle back-edges see final names.
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Concatenate the merged modules into one source file (bundle phases 4-5).
|
|
3
|
+
*
|
|
4
|
+
* Phase 4 walks every module in dependency order and copies its statements into
|
|
5
|
+
* a single statement list: external imports stay at top level, local imports are
|
|
6
|
+
* dropped (their bindings were rewritten in phase 3), and a deferred module's
|
|
7
|
+
* body is emitted inside its `__xt_deferred_*` wrapper. Phase 5 then renames
|
|
8
|
+
* every top-level declaration and reference to its final unique name.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { dirname } from "node:path";
|
|
12
|
+
import {
|
|
13
|
+
ModifierKind,
|
|
14
|
+
SyntaxKind,
|
|
15
|
+
type ExportAssignment,
|
|
16
|
+
type ExportDeclaration,
|
|
17
|
+
type Identifier,
|
|
18
|
+
type ImportDeclaration,
|
|
19
|
+
type Modifier,
|
|
20
|
+
type Node,
|
|
21
|
+
type SourceFileNode,
|
|
22
|
+
type Statement,
|
|
23
|
+
type VariableDeclaration,
|
|
24
|
+
type VariableDeclarationList,
|
|
25
|
+
} from "../../ast/nodes.js";
|
|
26
|
+
import { SymbolKind } from "../../binder/binder.js";
|
|
27
|
+
import { deferredModuleFunctions, eagerModuleFunctions, type DeferredAlias } from "./deferred.js";
|
|
28
|
+
import { classifyDependency } from "./resolve.js";
|
|
29
|
+
import { renameSymbol, topLevelSymbols } from "./symbols.js";
|
|
30
|
+
import type { ModuleRecord } from "./types.js";
|
|
31
|
+
|
|
32
|
+
export interface ConcatenateOptions {
|
|
33
|
+
readonly records: readonly ModuleRecord[];
|
|
34
|
+
readonly deferredModules: ReadonlySet<string>;
|
|
35
|
+
readonly deferredAliases: ReadonlyMap<string, readonly DeferredAlias[]>;
|
|
36
|
+
readonly markerTargets: ReadonlySet<string>;
|
|
37
|
+
readonly dynamicTargets: ReadonlySet<string>;
|
|
38
|
+
readonly namespaceStatements: ReadonlyMap<Statement, Statement>;
|
|
39
|
+
readonly externalSpecifiers: ReadonlySet<string>;
|
|
40
|
+
readonly moduleOverrides?: Readonly<Record<string, string>>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function concatenateModules(options: ConcatenateOptions): SourceFileNode {
|
|
44
|
+
const {
|
|
45
|
+
records,
|
|
46
|
+
deferredModules,
|
|
47
|
+
deferredAliases,
|
|
48
|
+
markerTargets,
|
|
49
|
+
dynamicTargets,
|
|
50
|
+
namespaceStatements,
|
|
51
|
+
externalSpecifiers,
|
|
52
|
+
moduleOverrides,
|
|
53
|
+
} = options;
|
|
54
|
+
const merged: Statement[] = [];
|
|
55
|
+
/* Every node keeps the module it was parsed from, so a later codegen
|
|
56
|
+
* diagnostic can name the right file and line instead of pointing into the
|
|
57
|
+
* entry module (whose text happens to start at the same offsets). */
|
|
58
|
+
const origins = new Map<object, string>();
|
|
59
|
+
/* A synthesized module (an extension asset loader such as the `gui` `.html`
|
|
60
|
+
* transform) may contain spans copied from another file — a `<script
|
|
61
|
+
* src="app.js">` body, say. Nodes inside such a span keep that file, so a
|
|
62
|
+
* diagnostic points at real source and a plain-`.js` script stays exempt
|
|
63
|
+
* from the type checker. */
|
|
64
|
+
const originOf = (record: ModuleRecord, node: Node): string => {
|
|
65
|
+
const ranges = record.originRanges;
|
|
66
|
+
if (ranges === undefined) return record.path;
|
|
67
|
+
for (const range of ranges) {
|
|
68
|
+
if (node.start >= range.start && node.end <= range.end) return range.file;
|
|
69
|
+
}
|
|
70
|
+
return record.path;
|
|
71
|
+
};
|
|
72
|
+
const trackOrigin = (record: ModuleRecord, root: Node): void => {
|
|
73
|
+
const visit = (node: Node): void => {
|
|
74
|
+
origins.set(node, originOf(record, node));
|
|
75
|
+
const fields = node as unknown as Record<string, unknown>;
|
|
76
|
+
for (const key of Object.keys(fields)) {
|
|
77
|
+
if (key === "kind" || key === "start" || key === "end" || key === "text" || key === "value" || key === "raw") {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const value = fields[key];
|
|
81
|
+
if (Array.isArray(value)) {
|
|
82
|
+
for (const child of value) if (isNode(child)) visit(child);
|
|
83
|
+
} else if (isNode(value)) {
|
|
84
|
+
visit(value);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
visit(root);
|
|
89
|
+
};
|
|
90
|
+
const push = (record: ModuleRecord, statement: Statement): void => {
|
|
91
|
+
trackOrigin(record, statement);
|
|
92
|
+
merged.push(statement);
|
|
93
|
+
};
|
|
94
|
+
const first = records[records.length - 1]!;
|
|
95
|
+
for (const record of records) {
|
|
96
|
+
if (deferredModules.has(record.path)) {
|
|
97
|
+
/* A deferred module is evaluated by the wrapper when a script runs. Its
|
|
98
|
+
* external imports stay top-level (codegen resolves them there); eager
|
|
99
|
+
* namespace objects likewise. */
|
|
100
|
+
const topLevel: Statement[] = [];
|
|
101
|
+
const body: Statement[] = [];
|
|
102
|
+
for (const statement of record.sourceFile.statements) {
|
|
103
|
+
if (statement.kind === SyntaxKind.ImportDeclaration) {
|
|
104
|
+
const declaration = statement as ImportDeclaration;
|
|
105
|
+
const resolution = classifyDependency(
|
|
106
|
+
dirname(record.path), declaration.moduleSpecifier.value, externalSpecifiers, moduleOverrides,
|
|
107
|
+
);
|
|
108
|
+
if (resolution.kind === "external") topLevel.push(statement);
|
|
109
|
+
const synthetic = namespaceStatements.get(statement);
|
|
110
|
+
if (synthetic) topLevel.push(synthetic);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (statement.kind === SyntaxKind.ExportDeclaration) {
|
|
114
|
+
const declaration = statement as ExportDeclaration;
|
|
115
|
+
const inner = (declaration as unknown as { declaration?: Statement }).declaration;
|
|
116
|
+
if (inner) body.push(inner);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (statement.kind === SyntaxKind.ExportAssignment) {
|
|
120
|
+
body.push(exportAssignmentToStatement(record, statement as ExportAssignment));
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
stripExportModifiers(statement);
|
|
124
|
+
body.push(statement);
|
|
125
|
+
}
|
|
126
|
+
for (const statement of topLevel) push(record, statement);
|
|
127
|
+
for (const statement of deferredModuleFunctions(
|
|
128
|
+
record,
|
|
129
|
+
deferredAliases.get(record.path) ?? [],
|
|
130
|
+
body,
|
|
131
|
+
)) {
|
|
132
|
+
push(record, statement);
|
|
133
|
+
}
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
for (const statement of record.sourceFile.statements) {
|
|
137
|
+
if (statement.kind === SyntaxKind.ImportDeclaration) {
|
|
138
|
+
// External (extension / built-in) imports stay in place for codegen.
|
|
139
|
+
const declaration = statement as ImportDeclaration;
|
|
140
|
+
const resolution = classifyDependency(
|
|
141
|
+
dirname(record.path), declaration.moduleSpecifier.value, externalSpecifiers, moduleOverrides,
|
|
142
|
+
);
|
|
143
|
+
if (resolution.kind === "external") {
|
|
144
|
+
push(record, statement);
|
|
145
|
+
} else {
|
|
146
|
+
const synthetic = namespaceStatements.get(statement);
|
|
147
|
+
if (synthetic) push(record, synthetic);
|
|
148
|
+
}
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (statement.kind === SyntaxKind.ExportDeclaration) {
|
|
152
|
+
const declaration = statement as ExportDeclaration;
|
|
153
|
+
const synthetic = namespaceStatements.get(statement);
|
|
154
|
+
if (synthetic) {
|
|
155
|
+
push(record, synthetic);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const inner = (declaration as unknown as { declaration?: Statement }).declaration;
|
|
159
|
+
if (inner) push(record, inner);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (statement.kind === SyntaxKind.ExportAssignment) {
|
|
163
|
+
const assignment = statement as ExportAssignment;
|
|
164
|
+
push(record, exportAssignmentToStatement(record, assignment));
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
push(record, statement);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/* A marker or dynamic-import target that stayed eager still needs a wrapper:
|
|
172
|
+
* its exports are already initialised. */
|
|
173
|
+
for (const record of records) {
|
|
174
|
+
if (deferredModules.has(record.path) || (!markerTargets.has(record.path) && !dynamicTargets.has(record.path))) continue;
|
|
175
|
+
for (const statement of eagerModuleFunctions(record)) push(record, statement);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
for (const record of records) {
|
|
179
|
+
for (const symbol of topLevelSymbols(record)) {
|
|
180
|
+
if (symbol.kind === SymbolKind.Import) continue;
|
|
181
|
+
const finalName = record.finalNames.get(symbol.id);
|
|
182
|
+
if (finalName) renameSymbol(symbol, finalName);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
kind: SyntaxKind.SourceFile,
|
|
188
|
+
statements: merged,
|
|
189
|
+
fileName: first.path,
|
|
190
|
+
origins,
|
|
191
|
+
moduleTexts: new Map(records.map((record) => [record.path, record.text])),
|
|
192
|
+
modulePrefixes: records.map((record) => record.prefix),
|
|
193
|
+
text: records.map((record) => record.text).join("\n"),
|
|
194
|
+
start: 0,
|
|
195
|
+
end: 0,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Drop `export` / `export default` from a statement moved into a wrapper. */
|
|
200
|
+
function stripExportModifiers(statement: Statement): void {
|
|
201
|
+
const modifiers = (statement as { modifiers?: Modifier[] }).modifiers;
|
|
202
|
+
if (!modifiers) return;
|
|
203
|
+
(statement as { modifiers: Modifier[] }).modifiers = modifiers.filter(
|
|
204
|
+
(modifier) => modifier.modifierKind !== ModifierKind.Export && modifier.modifierKind !== ModifierKind.Default,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** True for anything shaped like an AST node (constructed nodes carry no trivia). */
|
|
209
|
+
function isNode(value: unknown): value is Node {
|
|
210
|
+
return !!value && typeof value === "object" && "kind" in (value as object);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Turn `export default expr` into a synthetic `const` declaration. */
|
|
214
|
+
function exportAssignmentToStatement(record: ModuleRecord, assignment: ExportAssignment): Statement {
|
|
215
|
+
const name: Identifier = {
|
|
216
|
+
kind: SyntaxKind.Identifier,
|
|
217
|
+
text: `${record.prefix}default`,
|
|
218
|
+
start: assignment.start,
|
|
219
|
+
end: assignment.start,
|
|
220
|
+
};
|
|
221
|
+
record.exports.set("default", name.text);
|
|
222
|
+
const declaration: VariableDeclaration = {
|
|
223
|
+
kind: SyntaxKind.VariableDeclaration,
|
|
224
|
+
name,
|
|
225
|
+
exclamation: false,
|
|
226
|
+
initializer: assignment.expression,
|
|
227
|
+
start: assignment.start,
|
|
228
|
+
end: assignment.end,
|
|
229
|
+
};
|
|
230
|
+
const list: VariableDeclarationList = {
|
|
231
|
+
kind: SyntaxKind.VariableDeclarationList,
|
|
232
|
+
declarationKind: "const" as const,
|
|
233
|
+
declarations: [declaration],
|
|
234
|
+
start: assignment.start,
|
|
235
|
+
end: assignment.end,
|
|
236
|
+
};
|
|
237
|
+
return {
|
|
238
|
+
kind: SyntaxKind.VariableStatement,
|
|
239
|
+
declarationList: list,
|
|
240
|
+
modifiers: [],
|
|
241
|
+
start: assignment.start,
|
|
242
|
+
end: assignment.end,
|
|
243
|
+
};
|
|
244
|
+
}
|