xbintsc 0.6.28 → 0.7.0
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/binder/declarations.d.ts +1 -0
- package/dist/src/binder/declarations.js +37 -0
- package/dist/src/binder/declarations.js.map +1 -1
- package/dist/src/binder/references.js +12 -0
- package/dist/src/binder/references.js.map +1 -1
- package/dist/src/checker/globals.js +1 -0
- package/dist/src/checker/globals.js.map +1 -1
- package/dist/src/checker/type-system.d.ts +5 -0
- package/dist/src/checker/type-system.js +22 -3
- package/dist/src/checker/type-system.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/codegen/generator/calls/access.js +25 -0
- package/dist/src/codegen/generator/calls/access.js.map +1 -1
- package/dist/src/codegen/generator/calls/invocation.js +13 -0
- package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
- package/dist/src/codegen/generator/calls/super.js +3 -4
- package/dist/src/codegen/generator/calls/super.js.map +1 -1
- package/dist/src/codegen/generator/calls/unwrap.d.ts +18 -0
- package/dist/src/codegen/generator/calls/unwrap.js +45 -0
- package/dist/src/codegen/generator/calls/unwrap.js.map +1 -0
- package/dist/src/codegen/generator/expressions/assignment.js +14 -0
- package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
- package/dist/src/codegen/generator/expressions/module-values.d.ts +6 -0
- package/dist/src/codegen/generator/expressions/module-values.js +51 -17
- package/dist/src/codegen/generator/expressions/module-values.js.map +1 -1
- package/dist/src/codegen/generator/expressions/primary.js +9 -5
- package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
- package/dist/src/codegen/generator/generator.js +4 -0
- package/dist/src/codegen/generator/generator.js.map +1 -1
- package/dist/src/codegen/generator/lookup-tables.d.ts +22 -0
- package/dist/src/codegen/generator/lookup-tables.js +48 -0
- package/dist/src/codegen/generator/lookup-tables.js.map +1 -0
- package/dist/src/codegen/generator/module.js +38 -7
- package/dist/src/codegen/generator/module.js.map +1 -1
- package/dist/src/codegen/generator/state.js +17 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.d.ts +2 -6
- package/dist/src/codegen/generator/tables.js +18 -12
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/bundler/commonjs.d.ts +9 -2
- package/dist/src/driver/bundler/commonjs.js +11 -4
- package/dist/src/driver/bundler/commonjs.js.map +1 -1
- package/dist/src/driver/bundler/concatenate.d.ts +20 -0
- package/dist/src/driver/bundler/concatenate.js +68 -4
- package/dist/src/driver/bundler/concatenate.js.map +1 -1
- package/dist/src/driver/bundler/deferred.d.ts +29 -1
- package/dist/src/driver/bundler/deferred.js +114 -5
- package/dist/src/driver/bundler/deferred.js.map +1 -1
- package/dist/src/driver/bundler/dynamic-import.d.ts +6 -0
- package/dist/src/driver/bundler/dynamic-import.js +59 -3
- package/dist/src/driver/bundler/dynamic-import.js.map +1 -1
- package/dist/src/driver/bundler/graph.d.ts +13 -0
- package/dist/src/driver/bundler/graph.js +2 -2
- package/dist/src/driver/bundler/graph.js.map +1 -1
- package/dist/src/driver/bundler/merge.js +32 -8
- package/dist/src/driver/bundler/merge.js.map +1 -1
- package/dist/src/driver/bundler/namespace-object.d.ts +11 -0
- package/dist/src/driver/bundler/namespace-object.js +32 -0
- package/dist/src/driver/bundler/namespace-object.js.map +1 -1
- package/dist/src/driver/bundler/reexport-resolution.js +8 -2
- package/dist/src/driver/bundler/reexport-resolution.js.map +1 -1
- package/dist/src/driver/bundler/symbols.d.ts +4 -0
- package/dist/src/driver/bundler/symbols.js +14 -1
- package/dist/src/driver/bundler/symbols.js.map +1 -1
- package/dist/src/driver/config.d.ts +1 -1
- package/dist/src/driver/options.d.ts +1 -4
- package/dist/src/driver/runtime-lib.d.ts +27 -0
- package/dist/src/driver/runtime-lib.js +78 -2
- package/dist/src/driver/runtime-lib.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +24 -5
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/driver/version.js.map +1 -1
- package/dist/src/extensions/node/buffer/index.d.ts +5 -4
- package/dist/src/extensions/node/buffer/index.js +5 -4
- package/dist/src/extensions/node/buffer/index.js.map +1 -1
- package/dist/src/extensions/node/events/index.js +8 -1
- package/dist/src/extensions/node/events/index.js.map +1 -1
- package/dist/src/extensions/node/fs/callbacks.d.ts +9 -0
- package/dist/src/extensions/node/fs/callbacks.js +42 -0
- package/dist/src/extensions/node/fs/callbacks.js.map +1 -0
- package/dist/src/extensions/node/fs/index.d.ts +2 -3
- package/dist/src/extensions/node/fs/index.js +11 -4
- package/dist/src/extensions/node/fs/index.js.map +1 -1
- package/dist/src/extensions/node/process/index.d.ts +10 -3
- package/dist/src/extensions/node/process/index.js +30 -5
- package/dist/src/extensions/node/process/index.js.map +1 -1
- package/dist/src/extensions/registry.d.ts +7 -0
- package/dist/src/extensions/registry.js +13 -4
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/lexer/scanner.d.ts +7 -1
- package/dist/src/lexer/scanner.js +15 -0
- package/dist/src/lexer/scanner.js.map +1 -1
- package/dist/src/parser/context.d.ts +10 -0
- package/dist/src/parser/context.js +42 -3
- package/dist/src/parser/context.js.map +1 -1
- package/dist/src/parser/literals.js +6 -2
- package/dist/src/parser/literals.js.map +1 -1
- package/dist/tests/binder/binder.test.js +43 -0
- package/dist/tests/binder/binder.test.js.map +1 -1
- package/dist/tests/codegen/llvm.test.js +15 -0
- package/dist/tests/codegen/llvm.test.js.map +1 -1
- package/dist/tests/driver/bundler-resolve.test.js +12 -0
- package/dist/tests/driver/bundler-resolve.test.js.map +1 -1
- package/dist/tests/driver/commonjs-prelude.test.d.ts +11 -0
- package/dist/tests/driver/commonjs-prelude.test.js +69 -0
- package/dist/tests/driver/commonjs-prelude.test.js.map +1 -0
- package/dist/tests/driver/compiler.test.js +42 -1
- package/dist/tests/driver/compiler.test.js.map +1 -1
- package/dist/tests/driver/html-deferred-cycles.test.d.ts +10 -0
- package/dist/tests/driver/html-deferred-cycles.test.js +75 -0
- package/dist/tests/driver/html-deferred-cycles.test.js.map +1 -0
- package/dist/tests/driver/namespace-type-exports.test.d.ts +9 -0
- package/dist/tests/driver/namespace-type-exports.test.js +59 -0
- package/dist/tests/driver/namespace-type-exports.test.js.map +1 -0
- package/dist/tests/driver/runtime-lib.test.js +49 -1
- package/dist/tests/driver/runtime-lib.test.js.map +1 -1
- package/dist/tests/e2e/compile-basics.test.js +53 -0
- package/dist/tests/e2e/compile-basics.test.js.map +1 -1
- package/dist/tests/e2e/compile-deferred-marker.test.d.ts +14 -0
- package/dist/tests/e2e/compile-deferred-marker.test.js +68 -0
- package/dist/tests/e2e/compile-deferred-marker.test.js.map +1 -0
- package/dist/tests/e2e/compile-deferred-reexport.test.d.ts +12 -0
- package/dist/tests/e2e/compile-deferred-reexport.test.js +89 -0
- package/dist/tests/e2e/compile-deferred-reexport.test.js.map +1 -0
- package/dist/tests/e2e/compile-error-module.test.d.ts +9 -0
- package/dist/tests/e2e/compile-error-module.test.js +29 -0
- package/dist/tests/e2e/compile-error-module.test.js.map +1 -0
- package/dist/tests/e2e/compile-gc-promises.test.d.ts +11 -0
- package/dist/tests/e2e/compile-gc-promises.test.js +52 -0
- package/dist/tests/e2e/compile-gc-promises.test.js.map +1 -0
- package/dist/tests/e2e/compile-modules.test.js +40 -0
- package/dist/tests/e2e/compile-modules.test.js.map +1 -1
- package/dist/tests/e2e/compile-node-fs-callbacks.test.d.ts +6 -0
- package/dist/tests/e2e/compile-node-fs-callbacks.test.js +46 -0
- package/dist/tests/e2e/compile-node-fs-callbacks.test.js.map +1 -0
- package/dist/tests/e2e/compile-node-inheritance.test.d.ts +6 -0
- package/dist/tests/e2e/compile-node-inheritance.test.js +48 -0
- package/dist/tests/e2e/compile-node-inheritance.test.js.map +1 -0
- package/dist/tests/e2e/compile-node-require.test.d.ts +9 -0
- package/dist/tests/e2e/compile-node-require.test.js +29 -0
- package/dist/tests/e2e/compile-node-require.test.js.map +1 -0
- package/dist/tests/e2e/compile-node-stats.test.d.ts +8 -0
- package/dist/tests/e2e/compile-node-stats.test.js +26 -0
- package/dist/tests/e2e/compile-node-stats.test.js.map +1 -0
- package/dist/tests/e2e/compile-proxy.test.d.ts +6 -0
- package/dist/tests/e2e/compile-proxy.test.js +66 -0
- package/dist/tests/e2e/compile-proxy.test.js.map +1 -0
- package/dist/tests/e2e/compile-web-globals.test.d.ts +6 -0
- package/dist/tests/e2e/compile-web-globals.test.js +94 -0
- package/dist/tests/e2e/compile-web-globals.test.js.map +1 -0
- package/dist/tests/e2e/fetch.test.js +1 -1
- package/dist/tests/e2e/gui-script-import-order.test.js +24 -0
- package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
- package/dist/tests/e2e/json-stringify-to-json.test.d.ts +10 -0
- package/dist/tests/e2e/json-stringify-to-json.test.js +61 -0
- package/dist/tests/e2e/json-stringify-to-json.test.js.map +1 -0
- package/dist/tests/e2e/node-process.test.d.ts +7 -0
- package/dist/tests/e2e/node-process.test.js +50 -0
- package/dist/tests/e2e/node-process.test.js.map +1 -0
- package/dist/tests/e2e/runtime-coverage-buffer.test.js +31 -0
- package/dist/tests/e2e/runtime-coverage-buffer.test.js.map +1 -1
- package/dist/tests/e2e/web-streams.test.js +44 -29
- package/dist/tests/e2e/web-streams.test.js.map +1 -1
- package/dist/tests/parser/literals.test.js +19 -0
- package/dist/tests/parser/literals.test.js.map +1 -1
- package/dist/tests/parser/statements.test.js +13 -0
- package/dist/tests/parser/statements.test.js.map +1 -1
- package/doc/ai/cli.md +1 -0
- package/doc/ai/zh-CN/cli.md +1 -0
- package/doc/implemented/binder.md +2 -1
- package/doc/implemented/checker.md +5 -0
- package/doc/implemented/lexer.md +1 -1
- package/doc/implemented/modules.md +23 -1
- package/doc/implemented/node/buffer.md +18 -5
- package/doc/implemented/node/fs.md +22 -1
- package/doc/implemented/node/process.md +65 -5
- package/doc/implemented/node.md +7 -1
- package/doc/implemented/operators.md +3 -1
- package/doc/implemented/parser.md +1 -1
- package/doc/implemented/runtime-core.md +6 -2
- package/doc/implemented/stdlib.md +69 -2
- package/doc/unimplemented/classes.md +1 -0
- package/doc/unimplemented/modules.md +1 -0
- package/doc/unimplemented/stdlib.md +2 -1
- package/doc/xbintsc.config.schema.json +5 -0
- package/doc/zh-CN/implemented/binder.md +2 -1
- package/doc/zh-CN/implemented/checker.md +3 -0
- package/doc/zh-CN/implemented/lexer.md +1 -1
- package/doc/zh-CN/implemented/modules.md +17 -1
- package/doc/zh-CN/implemented/node/buffer.md +3 -1
- package/doc/zh-CN/implemented/node/fs.md +19 -1
- package/doc/zh-CN/implemented/node/process.md +59 -5
- package/doc/zh-CN/implemented/node.md +1 -1
- package/doc/zh-CN/implemented/operators.md +2 -1
- package/doc/zh-CN/implemented/parser.md +1 -1
- package/doc/zh-CN/implemented/runtime-core.md +3 -2
- package/doc/zh-CN/implemented/stdlib.md +58 -2
- package/doc/zh-CN/unimplemented/classes.md +1 -0
- package/doc/zh-CN/unimplemented/modules.md +1 -0
- package/doc/zh-CN/unimplemented/stdlib.md +2 -1
- package/package.json +7 -7
- package/runtime/ext_node/buffer/buffer.c +5 -4
- package/runtime/ext_node/buffer/parts/byte-access.inc +115 -11
- package/runtime/ext_node/buffer/parts/prototype.inc +4 -1
- package/runtime/ext_node/events/events.c +9 -0
- package/runtime/ext_node/fs/callbacks.c +138 -0
- package/runtime/ext_node/fs/fs_ops.c +28 -15
- package/runtime/ext_node/module/module.c +18 -9
- package/runtime/ext_node/process/process.c +117 -32
- package/runtime/ext_node/process/streams.c +370 -0
- package/runtime/ext_node/process/streams.h +20 -0
- package/runtime/rt.h +36 -0
- package/runtime/rt_internal.h +10 -10
- package/runtime/xt_alloc.c +67 -49
- package/runtime/xt_async_io.c +3 -0
- package/runtime/xt_builtins.c +26 -23
- package/runtime/xt_constructors.c +101 -2
- package/runtime/xt_containers/access.inc +29 -1
- package/runtime/xt_containers/function_prototypes.inc +58 -0
- package/runtime/xt_containers/objects.inc +18 -2
- package/runtime/xt_containers.c +1 -0
- package/runtime/xt_exit.c +34 -0
- package/runtime/xt_fetch/headers.inc +1 -0
- package/runtime/xt_fetch/response.inc +28 -8
- package/runtime/xt_fetch/webapi.inc +82 -0
- package/runtime/xt_fetch.c +5 -0
- package/runtime/xt_gc_verify.c +135 -0
- package/runtime/xt_intl.c +224 -0
- package/runtime/xt_io/exception.inc +40 -1
- package/runtime/xt_module_values.c +82 -0
- package/runtime/xt_proxy.c +140 -0
- package/runtime/xt_stdlib/dispatch.inc +98 -9
- package/runtime/xt_stdlib2/date.inc +7 -1
- package/runtime/xt_stdlib2/json.inc +28 -0
- package/runtime/xt_stdlib2/json_stringify.inc +70 -53
- package/runtime/xt_web.h +27 -0
- package/runtime/xt_web_abort.c +373 -0
- package/scripts/build-runtime.ts +8 -0
- package/src/binder/declarations.ts +34 -0
- package/src/binder/references.ts +12 -0
- package/src/checker/globals.ts +1 -0
- package/src/checker/type-system.ts +23 -3
- package/src/codegen/generator/calls/access.ts +23 -0
- package/src/codegen/generator/calls/invocation.ts +12 -0
- package/src/codegen/generator/calls/super.ts +5 -4
- package/src/codegen/generator/calls/unwrap.ts +47 -0
- package/src/codegen/generator/expressions/assignment.ts +15 -0
- package/src/codegen/generator/expressions/module-values.ts +57 -19
- package/src/codegen/generator/expressions/primary.ts +9 -9
- package/src/codegen/generator/generator.ts +4 -0
- package/src/codegen/generator/lookup-tables.ts +63 -0
- package/src/codegen/generator/module.ts +38 -7
- package/src/codegen/generator/state.ts +17 -0
- package/src/codegen/generator/tables.ts +18 -12
- package/src/driver/bundler/commonjs.ts +12 -5
- package/src/driver/bundler/concatenate.ts +84 -7
- package/src/driver/bundler/deferred.ts +122 -6
- package/src/driver/bundler/dynamic-import.ts +75 -2
- package/src/driver/bundler/graph.ts +2 -2
- package/src/driver/bundler/merge.ts +31 -5
- package/src/driver/bundler/namespace-object.ts +44 -0
- package/src/driver/bundler/reexport-resolution.ts +12 -2
- package/src/driver/bundler/symbols.ts +17 -0
- package/src/driver/config.ts +1 -1
- package/src/driver/options.ts +1 -4
- package/src/driver/runtime-lib.ts +87 -2
- package/src/driver/runtime-objects.ts +24 -5
- package/src/driver/version.ts +1 -1
- package/src/extensions/node/buffer/index.ts +5 -4
- package/src/extensions/node/events/index.ts +8 -1
- package/src/extensions/node/fs/callbacks.ts +45 -0
- package/src/extensions/node/fs/index.ts +11 -4
- package/src/extensions/node/process/index.ts +30 -5
- package/src/extensions/registry.ts +21 -4
- package/src/lexer/scanner.ts +16 -1
- package/src/parser/context.ts +39 -3
- package/src/parser/literals.ts +6 -2
|
@@ -24,9 +24,18 @@ import {
|
|
|
24
24
|
type VariableDeclarationList,
|
|
25
25
|
} from "../../ast/nodes.js";
|
|
26
26
|
import { SymbolKind } from "../../binder/binder.js";
|
|
27
|
-
import {
|
|
27
|
+
import {
|
|
28
|
+
assignmentStatement,
|
|
29
|
+
deferredAliasDeclarations,
|
|
30
|
+
deferredModuleFunctions,
|
|
31
|
+
deferredModuleSplit,
|
|
32
|
+
eagerModuleFunctions,
|
|
33
|
+
letDeclaration,
|
|
34
|
+
simpleVariableDeclarations,
|
|
35
|
+
type DeferredAlias,
|
|
36
|
+
} from "./deferred.js";
|
|
28
37
|
import { classifyDependency } from "./resolve.js";
|
|
29
|
-
import { renameSymbol, topLevelSymbols } from "./symbols.js";
|
|
38
|
+
import { moduleScope, renameSymbol, topLevelSymbols } from "./symbols.js";
|
|
30
39
|
import type { ModuleRecord } from "./types.js";
|
|
31
40
|
|
|
32
41
|
export interface ConcatenateOptions {
|
|
@@ -38,6 +47,13 @@ export interface ConcatenateOptions {
|
|
|
38
47
|
readonly namespaceStatements: ReadonlyMap<Statement, Statement>;
|
|
39
48
|
readonly externalSpecifiers: ReadonlySet<string>;
|
|
40
49
|
readonly moduleOverrides?: Readonly<Record<string, string>>;
|
|
50
|
+
/**
|
|
51
|
+
* `module`/`exports` state for every CommonJS module, emitted above the first
|
|
52
|
+
* module body. Node creates the pair when a module starts loading, so a
|
|
53
|
+
* `require` cycle reads the earlier module's partly filled object instead of
|
|
54
|
+
* one that is declared later in the concatenated program.
|
|
55
|
+
*/
|
|
56
|
+
readonly cjsPrelude?: readonly { readonly record: ModuleRecord; readonly statement: Statement }[];
|
|
41
57
|
}
|
|
42
58
|
|
|
43
59
|
export function concatenateModules(options: ConcatenateOptions): SourceFileNode {
|
|
@@ -50,6 +66,7 @@ export function concatenateModules(options: ConcatenateOptions): SourceFileNode
|
|
|
50
66
|
namespaceStatements,
|
|
51
67
|
externalSpecifiers,
|
|
52
68
|
moduleOverrides,
|
|
69
|
+
cjsPrelude,
|
|
53
70
|
} = options;
|
|
54
71
|
const merged: Statement[] = [];
|
|
55
72
|
/* Every node keeps the module it was parsed from, so a later codegen
|
|
@@ -91,6 +108,11 @@ export function concatenateModules(options: ConcatenateOptions): SourceFileNode
|
|
|
91
108
|
trackOrigin(record, statement);
|
|
92
109
|
merged.push(statement);
|
|
93
110
|
};
|
|
111
|
+
/* A module's `module`/`exports` pair exists as soon as the module starts
|
|
112
|
+
* loading, so hoist every pair above the first module body. A `require`
|
|
113
|
+
* cycle then reads the earlier module's object (partly filled, like Node)
|
|
114
|
+
* instead of a variable declared later in the concatenated program. */
|
|
115
|
+
for (const entry of cjsPrelude ?? []) push(entry.record, entry.statement);
|
|
94
116
|
const first = records[records.length - 1]!;
|
|
95
117
|
for (const record of records) {
|
|
96
118
|
if (deferredModules.has(record.path)) {
|
|
@@ -98,7 +120,15 @@ export function concatenateModules(options: ConcatenateOptions): SourceFileNode
|
|
|
98
120
|
* external imports stay top-level (codegen resolves them there); eager
|
|
99
121
|
* namespace objects likewise. */
|
|
100
122
|
const topLevel: Statement[] = [];
|
|
123
|
+
const hoisted: Statement[] = [];
|
|
124
|
+
const hoistedVariables: { name: string; start: number; end: number }[] = [];
|
|
101
125
|
const body: Statement[] = [];
|
|
126
|
+
/* A module cycle calls a dependency's *function declarations* before its
|
|
127
|
+
* body has run — that is what hoisting means in JavaScript. Keeping them
|
|
128
|
+
* inside the wrapper makes them `undefined` at that moment, so the
|
|
129
|
+
* declarations move to the top level and their module-level variable
|
|
130
|
+
* bindings become top-level `let`s whose values the wrapper assigns. */
|
|
131
|
+
const split = deferredModuleSplit(record);
|
|
102
132
|
for (const statement of record.sourceFile.statements) {
|
|
103
133
|
if (statement.kind === SyntaxKind.ImportDeclaration) {
|
|
104
134
|
const declaration = statement as ImportDeclaration;
|
|
@@ -112,6 +142,14 @@ export function concatenateModules(options: ConcatenateOptions): SourceFileNode
|
|
|
112
142
|
}
|
|
113
143
|
if (statement.kind === SyntaxKind.ExportDeclaration) {
|
|
114
144
|
const declaration = statement as ExportDeclaration;
|
|
145
|
+
/* `export * as ns from "..."` needs its synthetic namespace binding
|
|
146
|
+
* inside the wrapper: it reads the dependency's exports, which only
|
|
147
|
+
* exist once that dependency's wrapper has run. */
|
|
148
|
+
const synthetic = namespaceStatements.get(statement);
|
|
149
|
+
if (synthetic) {
|
|
150
|
+
body.push(synthetic);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
115
153
|
const inner = (declaration as unknown as { declaration?: Statement }).declaration;
|
|
116
154
|
if (inner) body.push(inner);
|
|
117
155
|
continue;
|
|
@@ -121,14 +159,42 @@ export function concatenateModules(options: ConcatenateOptions): SourceFileNode
|
|
|
121
159
|
continue;
|
|
122
160
|
}
|
|
123
161
|
stripExportModifiers(statement);
|
|
162
|
+
if (split) {
|
|
163
|
+
if (statement.kind === SyntaxKind.FunctionDeclaration) {
|
|
164
|
+
hoisted.push(statement);
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const declarations = simpleVariableDeclarations(statement);
|
|
168
|
+
if (declarations) {
|
|
169
|
+
const scope = moduleScope(record);
|
|
170
|
+
for (const declaration of declarations) {
|
|
171
|
+
/* The rename pass rewrites references to the final name, so the
|
|
172
|
+
* synthetic binding and its assignment must use it too. */
|
|
173
|
+
const symbol = scope?.symbols.get(declaration.name);
|
|
174
|
+
const finalName =
|
|
175
|
+
(symbol ? record.finalNames.get(symbol.id) : undefined) ?? declaration.name;
|
|
176
|
+
hoistedVariables.push({
|
|
177
|
+
name: finalName,
|
|
178
|
+
start: declaration.start,
|
|
179
|
+
end: declaration.end,
|
|
180
|
+
});
|
|
181
|
+
if (declaration.initializer) {
|
|
182
|
+
body.push(
|
|
183
|
+
assignmentStatement(finalName, declaration.initializer, statement.start, statement.end),
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
124
190
|
body.push(statement);
|
|
125
191
|
}
|
|
192
|
+
const aliases = deferredAliases.get(record.path) ?? [];
|
|
126
193
|
for (const statement of topLevel) push(record, statement);
|
|
127
|
-
for (const statement of
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
)) {
|
|
194
|
+
for (const statement of deferredAliasDeclarations(aliases)) push(record, statement);
|
|
195
|
+
if (hoistedVariables.length > 0) push(record, letDeclaration(hoistedVariables));
|
|
196
|
+
for (const statement of hoisted) push(record, statement);
|
|
197
|
+
for (const statement of deferredModuleFunctions(record, aliases, body)) {
|
|
132
198
|
push(record, statement);
|
|
133
199
|
}
|
|
134
200
|
continue;
|
|
@@ -242,3 +308,14 @@ function exportAssignmentToStatement(record: ModuleRecord, assignment: ExportAss
|
|
|
242
308
|
end: assignment.end,
|
|
243
309
|
};
|
|
244
310
|
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Whether a deferred module's top-level declarations can be split into
|
|
314
|
+
* top-level bindings plus in-wrapper initialisation.
|
|
315
|
+
*
|
|
316
|
+
* Function declarations are hoisted (a cycle may call them before any body
|
|
317
|
+
* runs), so the bindings they reference must be top level too. That works for
|
|
318
|
+
* plain `const`/`let`/`var x = ...`; a destructuring pattern, a class or an
|
|
319
|
+
* enum needs code we do not synthesise, so those modules keep everything inside
|
|
320
|
+
* the wrapper (and behave as before).
|
|
321
|
+
*/
|
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
type Expression,
|
|
25
25
|
type FunctionDeclaration,
|
|
26
26
|
type Identifier,
|
|
27
|
+
type ExportDeclaration,
|
|
27
28
|
type ImportDeclaration,
|
|
28
29
|
type ObjectLiteralElementLike,
|
|
29
30
|
type PropertyAssignment,
|
|
@@ -35,6 +36,9 @@ import {
|
|
|
35
36
|
type VariableStatement,
|
|
36
37
|
} from "../../ast/nodes.js";
|
|
37
38
|
import { SymbolKind } from "../../binder/types.js";
|
|
39
|
+
import { AssignmentOperator } from "../../ast/operators.js";
|
|
40
|
+
import { TokenKind } from "../../lexer/token.js";
|
|
41
|
+
import { isTypeOnlyExport, isTypeOnlyImport } from "./graph.js";
|
|
38
42
|
import { classifyDependency } from "./resolve.js";
|
|
39
43
|
import { topLevelSymbols } from "./symbols.js";
|
|
40
44
|
import type { ModuleRecord } from "./types.js";
|
|
@@ -105,6 +109,7 @@ function constDeclaration(
|
|
|
105
109
|
initializer: Expression | undefined,
|
|
106
110
|
start: number,
|
|
107
111
|
end: number,
|
|
112
|
+
declarationKind: "const" | "let" = "const",
|
|
108
113
|
): VariableStatement {
|
|
109
114
|
const declaration: VariableDeclaration = {
|
|
110
115
|
kind: SyntaxKind.VariableDeclaration,
|
|
@@ -117,22 +122,80 @@ function constDeclaration(
|
|
|
117
122
|
const list: VariableDeclarationList = {
|
|
118
123
|
kind: SyntaxKind.VariableDeclarationList,
|
|
119
124
|
declarations: [declaration],
|
|
120
|
-
declarationKind
|
|
125
|
+
declarationKind,
|
|
121
126
|
start,
|
|
122
127
|
end,
|
|
123
128
|
};
|
|
124
129
|
return { kind: SyntaxKind.VariableStatement, declarationList: list, modifiers: [], start, end };
|
|
125
130
|
}
|
|
126
131
|
|
|
127
|
-
/**
|
|
128
|
-
|
|
132
|
+
/**
|
|
133
|
+
* `<alias> = <wrapper>()[.<importedName>];`
|
|
134
|
+
*
|
|
135
|
+
* The alias itself is declared at top level (see
|
|
136
|
+
* {@link deferredAliasDeclarations}) so that a function declaration hoisted
|
|
137
|
+
* out of the wrapper can reference it: a module cycle calls that function
|
|
138
|
+
* before the wrapper body has run.
|
|
139
|
+
*/
|
|
140
|
+
function aliasStatement(alias: DeferredAlias): Statement {
|
|
129
141
|
const callee = identifier(alias.wrapper, alias.start, alias.end);
|
|
130
142
|
const namespace = call(callee, [], alias.start, alias.end);
|
|
131
143
|
const value =
|
|
132
144
|
alias.importedName === undefined || alias.namespace
|
|
133
145
|
? namespace
|
|
134
146
|
: property(namespace, alias.importedName, alias.start, alias.end);
|
|
135
|
-
return
|
|
147
|
+
return assignmentStatement(alias.name, value, alias.start, alias.end);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** `<name> = <value>;` — used to assign a top-level `let` from inside a wrapper. */
|
|
151
|
+
export function assignmentStatement(name: string, value: Expression, start: number, end: number): Statement {
|
|
152
|
+
const assignment = {
|
|
153
|
+
kind: SyntaxKind.BinaryExpression,
|
|
154
|
+
left: identifier(name, start, end),
|
|
155
|
+
operator: AssignmentOperator.Assign,
|
|
156
|
+
right: value,
|
|
157
|
+
operatorToken: { kind: TokenKind.Equals, text: "=", precededByLineBreak: false, start, end },
|
|
158
|
+
start,
|
|
159
|
+
end,
|
|
160
|
+
} as unknown as Expression;
|
|
161
|
+
return {
|
|
162
|
+
kind: SyntaxKind.ExpressionStatement,
|
|
163
|
+
expression: assignment,
|
|
164
|
+
start,
|
|
165
|
+
end,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** `let a, b;` for bindings a deferred module declares at its top level. */
|
|
170
|
+
export function letDeclaration(
|
|
171
|
+
names: readonly { readonly name: string; readonly start: number; readonly end: number }[],
|
|
172
|
+
): Statement {
|
|
173
|
+
const declarations: VariableDeclaration[] = names.map((entry) => ({
|
|
174
|
+
kind: SyntaxKind.VariableDeclaration,
|
|
175
|
+
name: identifier(entry.name, entry.start, entry.end),
|
|
176
|
+
exclamation: false,
|
|
177
|
+
initializer: undefined,
|
|
178
|
+
start: entry.start,
|
|
179
|
+
end: entry.end,
|
|
180
|
+
}));
|
|
181
|
+
const list: VariableDeclarationList = {
|
|
182
|
+
kind: SyntaxKind.VariableDeclarationList,
|
|
183
|
+
declarations,
|
|
184
|
+
declarationKind: "let",
|
|
185
|
+
start: declarations[0]!.start,
|
|
186
|
+
end: declarations[declarations.length - 1]!.end,
|
|
187
|
+
};
|
|
188
|
+
return { kind: SyntaxKind.VariableStatement, declarationList: list, modifiers: [], start: list.start, end: list.end };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** `let <alias>;` for every alias that binds a name, emitted at top level. */
|
|
192
|
+
export function deferredAliasDeclarations(aliases: readonly DeferredAlias[]): Statement[] {
|
|
193
|
+
const statements: Statement[] = [];
|
|
194
|
+
for (const alias of aliases) {
|
|
195
|
+
if (alias.sideEffect || alias.name.length === 0) continue;
|
|
196
|
+
statements.push(constDeclaration(alias.name, undefined, alias.start, alias.end, "let"));
|
|
197
|
+
}
|
|
198
|
+
return statements;
|
|
136
199
|
}
|
|
137
200
|
|
|
138
201
|
/** A wrapper call used for a side-effect-only import. */
|
|
@@ -299,14 +362,18 @@ export function computeDeferredModules(
|
|
|
299
362
|
const resolution = classifyDependency(dirname(record.path), specifier, externalSpecifiers, moduleOverrides);
|
|
300
363
|
return resolution.kind === "file" ? byPath.get(resolution.path) : undefined;
|
|
301
364
|
};
|
|
365
|
+
/* A type-only import or re-export is erased, so it is not a module edge: it
|
|
366
|
+
* must not make its target eager (`export type { T } from "pkg"` used to
|
|
367
|
+
* pull a whole package into the startup path). */
|
|
302
368
|
const specifierOf = (statement: Statement): string | undefined => {
|
|
303
369
|
if (statement.kind === SyntaxKind.ImportDeclaration) {
|
|
304
370
|
const declaration = statement as ImportDeclaration;
|
|
305
|
-
if (declaration
|
|
371
|
+
if (isTypeOnlyImport(declaration)) return undefined;
|
|
306
372
|
return declaration.moduleSpecifier.value;
|
|
307
373
|
}
|
|
308
374
|
if (statement.kind === SyntaxKind.ExportDeclaration) {
|
|
309
|
-
const declaration = statement as
|
|
375
|
+
const declaration = statement as ExportDeclaration;
|
|
376
|
+
if (isTypeOnlyExport(declaration)) return undefined;
|
|
310
377
|
return declaration.moduleSpecifier?.value;
|
|
311
378
|
}
|
|
312
379
|
return undefined;
|
|
@@ -350,3 +417,52 @@ export function computeDeferredModules(
|
|
|
350
417
|
}
|
|
351
418
|
return deferred;
|
|
352
419
|
}
|
|
420
|
+
/** The declarations of a `const`/`let`/`var` statement with plain identifiers. */
|
|
421
|
+
export function simpleVariableDeclarations(
|
|
422
|
+
statement: Statement,
|
|
423
|
+
): { name: string; initializer?: Expression; start: number; end: number }[] | undefined {
|
|
424
|
+
if (statement.kind !== SyntaxKind.VariableStatement) return undefined;
|
|
425
|
+
const list = (statement as VariableStatement).declarationList;
|
|
426
|
+
const declarations: { name: string; initializer?: Expression; start: number; end: number }[] = [];
|
|
427
|
+
for (const declaration of list.declarations) {
|
|
428
|
+
if (declaration.name.kind !== SyntaxKind.Identifier) return undefined;
|
|
429
|
+
declarations.push({
|
|
430
|
+
name: (declaration.name as Identifier).text,
|
|
431
|
+
initializer: declaration.initializer,
|
|
432
|
+
start: declaration.start,
|
|
433
|
+
end: declaration.end,
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
return declarations;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Whether a deferred module's top-level declarations can be split into
|
|
441
|
+
* top-level bindings plus in-wrapper initialisation.
|
|
442
|
+
*
|
|
443
|
+
* Function declarations are hoisted (a cycle may call them before any body
|
|
444
|
+
* runs), so the bindings they reference must be top level too. That works for
|
|
445
|
+
* plain `const`/`let`/`var x = ...`; a destructuring pattern, a class or an
|
|
446
|
+
* enum needs code we do not synthesise, so those modules keep everything inside
|
|
447
|
+
* the wrapper (and behave as before).
|
|
448
|
+
*/
|
|
449
|
+
export function deferredModuleSplit(record: ModuleRecord): boolean {
|
|
450
|
+
for (const statement of record.sourceFile.statements) {
|
|
451
|
+
if (
|
|
452
|
+
statement.kind === SyntaxKind.ImportDeclaration ||
|
|
453
|
+
statement.kind === SyntaxKind.ExportDeclaration ||
|
|
454
|
+
statement.kind === SyntaxKind.ExportAssignment ||
|
|
455
|
+
statement.kind === SyntaxKind.FunctionDeclaration
|
|
456
|
+
) {
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
if (statement.kind === SyntaxKind.VariableStatement) {
|
|
460
|
+
if (simpleVariableDeclarations(statement) === undefined) return false;
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
if (statement.kind === SyntaxKind.ClassDeclaration || statement.kind === SyntaxKind.EnumDeclaration) {
|
|
464
|
+
return false;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
return true;
|
|
468
|
+
}
|
|
@@ -9,7 +9,21 @@
|
|
|
9
9
|
* and is left in place for the code generator to reject.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
SyntaxKind,
|
|
14
|
+
type CallExpression,
|
|
15
|
+
type Expression,
|
|
16
|
+
type Identifier,
|
|
17
|
+
type ImportDeclaration,
|
|
18
|
+
type ImportExpression,
|
|
19
|
+
type ImportSpecifier,
|
|
20
|
+
type NamedImports,
|
|
21
|
+
type Node,
|
|
22
|
+
type PropertyAccessExpression,
|
|
23
|
+
type SourceFileNode,
|
|
24
|
+
type Statement,
|
|
25
|
+
type StringLiteral,
|
|
26
|
+
} from "../../ast/nodes.js";
|
|
13
27
|
import { deferredWrapperName } from "./deferred.js";
|
|
14
28
|
import type { ModuleRecord } from "./types.js";
|
|
15
29
|
|
|
@@ -65,6 +79,47 @@ function member(object: string, name: string, anchor: number): PropertyAccessExp
|
|
|
65
79
|
};
|
|
66
80
|
}
|
|
67
81
|
|
|
82
|
+
/** `import { __xt_deferred_module as <name> } from "<specifier>"`. */
|
|
83
|
+
function markerImport(name: string, specifier: string, anchor: number): ImportDeclaration {
|
|
84
|
+
const moduleSpecifier: StringLiteral = {
|
|
85
|
+
kind: SyntaxKind.StringLiteral,
|
|
86
|
+
text: JSON.stringify(specifier),
|
|
87
|
+
value: specifier,
|
|
88
|
+
raw: JSON.stringify(specifier),
|
|
89
|
+
start: anchor,
|
|
90
|
+
end: anchor,
|
|
91
|
+
};
|
|
92
|
+
const element: ImportSpecifier = {
|
|
93
|
+
kind: SyntaxKind.ImportSpecifier,
|
|
94
|
+
isTypeOnly: false,
|
|
95
|
+
propertyName: identifier("__xt_deferred_module", anchor),
|
|
96
|
+
name: identifier(name, anchor),
|
|
97
|
+
start: anchor,
|
|
98
|
+
end: anchor,
|
|
99
|
+
};
|
|
100
|
+
const namedBindings: NamedImports = {
|
|
101
|
+
kind: SyntaxKind.NamedImports,
|
|
102
|
+
elements: [element],
|
|
103
|
+
start: anchor,
|
|
104
|
+
end: anchor,
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
kind: SyntaxKind.ImportDeclaration,
|
|
108
|
+
importClause: {
|
|
109
|
+
kind: SyntaxKind.ImportClause,
|
|
110
|
+
name: undefined,
|
|
111
|
+
isTypeOnly: false,
|
|
112
|
+
namedBindings,
|
|
113
|
+
start: anchor,
|
|
114
|
+
end: anchor,
|
|
115
|
+
},
|
|
116
|
+
moduleSpecifier,
|
|
117
|
+
attributes: [],
|
|
118
|
+
start: anchor,
|
|
119
|
+
end: anchor,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
68
123
|
function call(expression: Expression, args: Expression[], anchor: number): CallExpression {
|
|
69
124
|
return { kind: SyntaxKind.CallExpression, expression, typeArguments: [], arguments: args, optional: false, start: anchor, end: anchor };
|
|
70
125
|
}
|
|
@@ -115,6 +170,12 @@ export function rewriteDynamicImports(
|
|
|
115
170
|
* Rewrite every module's dynamic imports in place and return the set of module
|
|
116
171
|
* paths that are reached only through a dynamic import (so the merge can emit a
|
|
117
172
|
* namespace wrapper for them).
|
|
173
|
+
*
|
|
174
|
+
* A literal dynamic import must not evaluate its target at startup: it is
|
|
175
|
+
* deferred by marking the target with the same synthetic import the GUI
|
|
176
|
+
* `<script src>` loader uses (see `deferred.ts`). The marker edge is skipped by
|
|
177
|
+
* the eager-reachability walk, so a module that *only* appears in an
|
|
178
|
+
* `import(...)` is wrapped and its body runs on the first call.
|
|
118
179
|
*/
|
|
119
180
|
export function prepareDynamicImports(
|
|
120
181
|
records: readonly ModuleRecord[],
|
|
@@ -122,14 +183,26 @@ export function prepareDynamicImports(
|
|
|
122
183
|
): Set<string> {
|
|
123
184
|
const targets = new Set<string>();
|
|
124
185
|
for (const record of records) {
|
|
186
|
+
const markers: Statement[] = [];
|
|
187
|
+
const marked = new Set<string>();
|
|
125
188
|
const resolve = (specifier: string): string | undefined => {
|
|
126
189
|
const dependency = dependencyOf(record, specifier);
|
|
127
|
-
if (dependency)
|
|
190
|
+
if (!dependency) return undefined;
|
|
191
|
+
targets.add(dependency.path);
|
|
192
|
+
if (!marked.has(dependency.path)) {
|
|
193
|
+
marked.add(dependency.path);
|
|
194
|
+
markers.push(
|
|
195
|
+
markerImport(`__xt_dynamic_${markers.length}`, specifier, record.sourceFile.start),
|
|
196
|
+
);
|
|
197
|
+
}
|
|
128
198
|
return dependency ? deferredWrapperName(dependency) : undefined;
|
|
129
199
|
};
|
|
130
200
|
for (const statement of record.sourceFile.statements) {
|
|
131
201
|
rewriteDynamicImports(statement, resolve, record.sourceFile.start);
|
|
132
202
|
}
|
|
203
|
+
if (markers.length > 0) {
|
|
204
|
+
(record.sourceFile.statements as Statement[]).unshift(...markers);
|
|
205
|
+
}
|
|
133
206
|
}
|
|
134
207
|
return targets;
|
|
135
208
|
}
|
|
@@ -26,7 +26,7 @@ import type { ModuleRecord, OriginRange } from "./types.js";
|
|
|
26
26
|
* and, in particular, must not turn the mutual `import type` references
|
|
27
27
|
* between source files into a circular-import error.
|
|
28
28
|
*/
|
|
29
|
-
function isTypeOnlyImport(declaration: ImportDeclaration): boolean {
|
|
29
|
+
export function isTypeOnlyImport(declaration: ImportDeclaration): boolean {
|
|
30
30
|
const clause = declaration.importClause;
|
|
31
31
|
if (!clause) return false;
|
|
32
32
|
if (clause.isTypeOnly) return true;
|
|
@@ -39,7 +39,7 @@ function isTypeOnlyImport(declaration: ImportDeclaration): boolean {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
/** True when an export declaration only re-exports types. */
|
|
42
|
-
function isTypeOnlyExport(declaration: ExportDeclaration): boolean {
|
|
42
|
+
export function isTypeOnlyExport(declaration: ExportDeclaration): boolean {
|
|
43
43
|
if (declaration.isTypeOnly) return true;
|
|
44
44
|
const clause = declaration.exportClause;
|
|
45
45
|
if (!clause || clause.kind !== SyntaxKind.NamedExports) return false;
|
|
@@ -27,6 +27,7 @@ import { lowerCommonJSRequires, prepareCommonJS } from "./commonjs.js";
|
|
|
27
27
|
import {
|
|
28
28
|
computeDeferredModules,
|
|
29
29
|
deferredAliasName,
|
|
30
|
+
deferredModuleSplit,
|
|
30
31
|
deferredWrapperName,
|
|
31
32
|
isDeferredMarkerImport,
|
|
32
33
|
type DeferredAlias,
|
|
@@ -39,6 +40,7 @@ import { classifyDependency } from "./resolve.js";
|
|
|
39
40
|
import {
|
|
40
41
|
declarationName,
|
|
41
42
|
hasModifier,
|
|
43
|
+
isTypeOnlySymbol,
|
|
42
44
|
moduleScope,
|
|
43
45
|
renameImport,
|
|
44
46
|
renameReferences,
|
|
@@ -75,8 +77,12 @@ export function bundleModules(
|
|
|
75
77
|
}
|
|
76
78
|
|
|
77
79
|
// Phase 1b: give every CommonJS module its `module`/`exports` state and
|
|
78
|
-
// record its export surface, so later phases can re-export it.
|
|
79
|
-
|
|
80
|
+
// record its export surface, so later phases can re-export it. The state
|
|
81
|
+
// itself is hoisted above every module body (see `cjsPrelude` below).
|
|
82
|
+
const cjsPrelude: Array<{ record: ModuleRecord; statement: Statement }> = [];
|
|
83
|
+
for (const record of records) {
|
|
84
|
+
for (const statement of prepareCommonJS(record)) cjsPrelude.push({ record, statement });
|
|
85
|
+
}
|
|
80
86
|
|
|
81
87
|
// Phase 2: collect each module's exported names.
|
|
82
88
|
const namespaceStatements = new Map<Statement, Statement>();
|
|
@@ -99,7 +105,10 @@ export function bundleModules(
|
|
|
99
105
|
if (target) record.exports.set(exportedName, target);
|
|
100
106
|
} else {
|
|
101
107
|
const symbol = symbolByName.get(localName);
|
|
102
|
-
|
|
108
|
+
/* A type-only local (`export { FooOptions }`) has no runtime
|
|
109
|
+
* binding; exporting its name would make every namespace import
|
|
110
|
+
* of this module read a slot that does not exist. */
|
|
111
|
+
if (symbol && !isTypeOnlySymbol(symbol)) {
|
|
103
112
|
const finalName = record.finalNames.get(symbol.id) ?? symbol.name;
|
|
104
113
|
record.exports.set(exportedName, finalName);
|
|
105
114
|
}
|
|
@@ -129,7 +138,7 @@ export function bundleModules(
|
|
|
129
138
|
const name = declarationName(statement) ?? declarationName((statement as { declaration?: Node }).declaration ?? statement);
|
|
130
139
|
if (name) {
|
|
131
140
|
const symbol = symbolByName.get(name.text);
|
|
132
|
-
if (symbol) record.exports.set("default", record.finalNames.get(symbol.id) ?? symbol.name);
|
|
141
|
+
if (symbol && !isTypeOnlySymbol(symbol)) record.exports.set("default", record.finalNames.get(symbol.id) ?? symbol.name);
|
|
133
142
|
}
|
|
134
143
|
continue;
|
|
135
144
|
}
|
|
@@ -137,7 +146,7 @@ export function bundleModules(
|
|
|
137
146
|
const names = exportedNames(statement);
|
|
138
147
|
for (const name of names) {
|
|
139
148
|
const symbol = symbolByName.get(name);
|
|
140
|
-
if (symbol) record.exports.set(name, record.finalNames.get(symbol.id) ?? symbol.name);
|
|
149
|
+
if (symbol && !isTypeOnlySymbol(symbol)) record.exports.set(name, record.finalNames.get(symbol.id) ?? symbol.name);
|
|
141
150
|
}
|
|
142
151
|
}
|
|
143
152
|
}
|
|
@@ -269,6 +278,22 @@ export function bundleModules(
|
|
|
269
278
|
if (specifier.isTypeOnly) continue;
|
|
270
279
|
const importedName = specifier.propertyName?.text ?? specifier.name.text;
|
|
271
280
|
const local = scope.symbols.get(specifier.name.text);
|
|
281
|
+
/* A function declaration is hoisted out of the dependency's wrapper
|
|
282
|
+
* (see `concatenateModules`), so referring to its final name works
|
|
283
|
+
* even inside a module cycle — which is exactly when the alias
|
|
284
|
+
* would still be `undefined`. The dependency is still initialised
|
|
285
|
+
* once, through a side-effect alias. */
|
|
286
|
+
const target = dependency.exports.get(importedName);
|
|
287
|
+
const dependencyScope = moduleScope(dependency);
|
|
288
|
+
const hoisted =
|
|
289
|
+
target !== undefined &&
|
|
290
|
+
deferredModuleSplit(dependency) &&
|
|
291
|
+
dependencyScope?.symbols.get(importedName)?.kind === SymbolKind.Function;
|
|
292
|
+
if (hoisted) {
|
|
293
|
+
if (local) renameReferences(local, target);
|
|
294
|
+
aliases.push({ name: "", wrapper, sideEffect: true, start, end });
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
272
297
|
const alias = deferredAliasName(record, aliases.length, importedName);
|
|
273
298
|
if (local) renameReferences(local, alias);
|
|
274
299
|
aliases.push({
|
|
@@ -351,6 +376,7 @@ export function bundleModules(
|
|
|
351
376
|
namespaceStatements,
|
|
352
377
|
externalSpecifiers,
|
|
353
378
|
moduleOverrides,
|
|
379
|
+
cjsPrelude,
|
|
354
380
|
});
|
|
355
381
|
return { sourceFile, text: sourceFile.text, moduleCount: records.length };
|
|
356
382
|
}
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
|
|
9
9
|
import {
|
|
10
10
|
SyntaxKind,
|
|
11
|
+
type CallExpression,
|
|
12
|
+
type Identifier,
|
|
11
13
|
type ObjectLiteralExpression,
|
|
12
14
|
type PropertyAssignment,
|
|
13
15
|
type Statement,
|
|
@@ -16,6 +18,39 @@ import {
|
|
|
16
18
|
} from "../../ast/nodes.js";
|
|
17
19
|
import type { ModuleRecord } from "./types.js";
|
|
18
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Lower a *deferred* module's `export * as ns from "./dep"` to
|
|
23
|
+
* `const <final> = __xt_deferred_<dep>()`.
|
|
24
|
+
*
|
|
25
|
+
* The dependency's export names only exist inside its own wrapper function, so
|
|
26
|
+
* an object literal of final names would reference names that are not in scope
|
|
27
|
+
* (TS3002) or be `undefined` at run time. The wrapper returns the dependency's
|
|
28
|
+
* exports object, which is what the namespace export needs, and calling it also
|
|
29
|
+
* initialises the dependency first.
|
|
30
|
+
*/
|
|
31
|
+
export function deferredNamespaceToStatement(
|
|
32
|
+
statement: Statement,
|
|
33
|
+
finalName: string,
|
|
34
|
+
wrapperName: string,
|
|
35
|
+
): Statement {
|
|
36
|
+
const callee: Identifier = {
|
|
37
|
+
kind: SyntaxKind.Identifier,
|
|
38
|
+
text: wrapperName,
|
|
39
|
+
start: statement.start,
|
|
40
|
+
end: statement.start,
|
|
41
|
+
};
|
|
42
|
+
const initializer: CallExpression = {
|
|
43
|
+
kind: SyntaxKind.CallExpression,
|
|
44
|
+
expression: callee,
|
|
45
|
+
arguments: [],
|
|
46
|
+
typeArguments: [],
|
|
47
|
+
optional: false,
|
|
48
|
+
start: statement.start,
|
|
49
|
+
end: statement.end,
|
|
50
|
+
};
|
|
51
|
+
return variableStatement(statement, finalName, initializer);
|
|
52
|
+
}
|
|
53
|
+
|
|
19
54
|
/** Lower `import * as ns from "./mod"` to `const m0_ns = { ...exports }`. */
|
|
20
55
|
export function namespaceImportToStatement(
|
|
21
56
|
statement: Statement,
|
|
@@ -38,6 +73,15 @@ export function namespaceImportToStatement(
|
|
|
38
73
|
start: statement.start,
|
|
39
74
|
end: statement.end,
|
|
40
75
|
};
|
|
76
|
+
return variableStatement(statement, finalName, initializer);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** `const <finalName> = <initializer>` at the statement's offsets. */
|
|
80
|
+
function variableStatement(
|
|
81
|
+
statement: Statement,
|
|
82
|
+
finalName: string,
|
|
83
|
+
initializer: ObjectLiteralExpression | CallExpression,
|
|
84
|
+
): Statement {
|
|
41
85
|
const declaration: VariableDeclaration = {
|
|
42
86
|
kind: SyntaxKind.VariableDeclaration,
|
|
43
87
|
name: { kind: SyntaxKind.Identifier, text: finalName, start: statement.start, end: statement.start },
|
|
@@ -24,7 +24,8 @@ import {
|
|
|
24
24
|
type Statement,
|
|
25
25
|
} from "../../ast/nodes.js";
|
|
26
26
|
import type { ModuleRecord } from "./types.js";
|
|
27
|
-
import { namespaceImportToStatement } from "./namespace-object.js";
|
|
27
|
+
import { deferredNamespaceToStatement, namespaceImportToStatement } from "./namespace-object.js";
|
|
28
|
+
import { deferredWrapperName } from "./deferred.js";
|
|
28
29
|
|
|
29
30
|
export function resolveReexports(
|
|
30
31
|
records: ModuleRecord[],
|
|
@@ -82,9 +83,18 @@ export function resolveReexports(
|
|
|
82
83
|
const nsName = (declaration.exportClause as { name: Identifier }).name.text;
|
|
83
84
|
const dependency = dependencyOf(record, declaration.moduleSpecifier.value);
|
|
84
85
|
if (dependency) {
|
|
86
|
+
/* Both deferred: the dependency's final names live inside its
|
|
87
|
+
* wrapper, so read its exports object instead of building one. */
|
|
88
|
+
const deferred = deferredModules.has(record.path) && deferredModules.has(dependency.path);
|
|
85
89
|
namespaceStatements.set(
|
|
86
90
|
statement,
|
|
87
|
-
|
|
91
|
+
deferred
|
|
92
|
+
? deferredNamespaceToStatement(
|
|
93
|
+
statement,
|
|
94
|
+
`${record.prefix}${nsName}`,
|
|
95
|
+
deferredWrapperName(dependency),
|
|
96
|
+
)
|
|
97
|
+
: namespaceImportToStatement(statement, `${record.prefix}${nsName}`, dependency),
|
|
88
98
|
);
|
|
89
99
|
}
|
|
90
100
|
}
|
|
@@ -36,6 +36,23 @@ export function declarationName(node: Node): Identifier | undefined {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
/** True when every declaration of this symbol is erased before codegen: an
|
|
40
|
+
* interface, a type alias, or an ambient `declare`. Such a symbol has no
|
|
41
|
+
* runtime slot, so a module namespace object must not reference it. */
|
|
42
|
+
export function isTypeOnlySymbol(symbol: SymbolInfo): boolean {
|
|
43
|
+
return symbol.declarations.length > 0 && symbol.declarations.every(isErasedDeclaration);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isErasedDeclaration(node: Node): boolean {
|
|
47
|
+
if (
|
|
48
|
+
node.kind === SyntaxKind.InterfaceDeclaration ||
|
|
49
|
+
node.kind === SyntaxKind.TypeAliasDeclaration
|
|
50
|
+
) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
return hasModifier(node, ModifierKind.Declare);
|
|
54
|
+
}
|
|
55
|
+
|
|
39
56
|
/** Rewrite a symbol's declaration and every reference to `finalName`. */
|
|
40
57
|
export function renameSymbol(symbol: SymbolInfo, finalName: string): void {
|
|
41
58
|
for (const declaration of symbol.declarations) {
|
package/src/driver/config.ts
CHANGED
|
@@ -59,7 +59,7 @@ export interface ProjectConfig {
|
|
|
59
59
|
readonly features?: readonly string[];
|
|
60
60
|
/** Optional link features to keep out even when auto-detection finds a use. */
|
|
61
61
|
readonly noFeatures?: readonly string[];
|
|
62
|
-
/**
|
|
62
|
+
/** Exact module specifiers mapped to local replacement files. */
|
|
63
63
|
readonly moduleOverrides?: Readonly<Record<string, string>>;
|
|
64
64
|
readonly force?: boolean;
|
|
65
65
|
readonly app?: AppConfig;
|