-
Notifications
You must be signed in to change notification settings - Fork 3
/
transform.ts
481 lines (448 loc) · 15.8 KB
/
transform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import type { NodePath, Visitor } from "@babel/traverse";
import type { BindingEntry } from "dynohot/runtime/binding";
import * as assert from "node:assert/strict";
import { parse, types as t } from "@babel/core";
import convertSourceMap from "convert-source-map";
import Fn from "dynohot/functional";
import { BindingType } from "dynohot/runtime/binding";
import { generate, makeRootPath, traverse } from "./babel-shim.js";
const makeLocalGetter = (localName: string, exportName: string) =>
t.objectProperty(
t.stringLiteral(exportName),
t.arrowFunctionExpression([], t.identifier(localName)));
const extractName = (node: t.Identifier | t.StringLiteral) =>
t.isStringLiteral(node) ? node.value : node.name;
interface ImportEntry {
identifier: string;
importSpecifier: string;
specifier: string;
bindings: BindingEntry[];
}
export function transformModuleSource(
filename: string,
importAssertions: Record<string, string>,
sourceText: string,
sourceMap: unknown,
) {
const file = function() {
try {
const file = parse(sourceText, {
babelrc: false,
configFile: false,
filename,
retainLines: true,
sourceType: "module",
parserOpts: {
plugins: [
"explicitResourceManagement",
[ "importAttributes", { deprecatedAssertSyntax: true } ],
],
},
});
assert.ok(file);
return file;
} finally {
// nb: Babel has uncharacteristically poor hygiene here and assigns `Error.prepareStackTrace`
// when you invoke `parse` and doesn't even bother to put it back. This causes nodejs's source
// map feature to bail out and show plain source files.
// In nodejs v20.0+ loaders run in a separate context, so it isn't an issue there, but otherwise
// source maps just won't work.
// https://github.com/babel/babel/blob/74b5ac21d0fb516ecc8d8375cc75b4446b6c9735/packages/babel-core/src/errors/rewrite-stack-trace.ts#L140
delete Error.prepareStackTrace;
}
}();
// Run transformation
const path = makeRootPath(file);
const state = transformProgram(path);
// Generate new source code from modified AST
const result = generate(file, {
filename: filename.replace(/(\..+?|)$/, ".hot$1"),
retainLines: true,
sourceMaps: true,
// @ts-expect-error -- Types are out of date? https://github.com/babel/babel/blame/c446ff85c28e117ebf3cd72cd34ef358f1077aa8/packages/babel-generator/src/index.ts#L190
inputSourceMap: sourceMap,
});
// Generate source map comment
const sourceMapComment = result.map ? convertSourceMap.fromObject(result.map).toComment() : "";
// Make import declarations
const imports = Fn.join(Fn.map(
state.importDeclarations,
declaration =>
`import ${declaration.identifier} from ${JSON.stringify(declaration.importSpecifier)};`,
), "\n");
// `ModuleBody`
const body = `{ async: ${state.usesTopLevelAwait}, execute }`;
// nb: We omit `import.meta` in the case the module doesn't use it at all. No
// telling if this is actually important to the runtime environment but it seemed
// important enough to make it into the compartments proposal:
// https://github.com/tc39/proposal-compartments/blob/7e60fdbce66ef2d97370007afeb807192c653333/1-static-analysis.md#design-rationales
const importMeta = state.usesImportMeta ? "import.meta" : "null";
// `LoadedModuleRequestEntry`
const requestEntries = `[ ${Fn.join(Fn.map(
state.importDeclarations,
declaration => {
const bindings = JSON.stringify(declaration.bindings);
const specifier = JSON.stringify(declaration.specifier);
return `{ controller: ${declaration.identifier}, specifier: ${specifier}, bindings: ${bindings} }`;
},
), ", ")} ]`;
const loader = `module().load(${body}, ${importMeta}, ${state.usesDynamicImport}, "module", ${JSON.stringify(importAssertions)}, ${requestEntries});`;
// Build final module source
return `${result.code}\n${sourceMapComment}\n${imports}\n${loader}\n`;
}
function transformProgram(program: NodePath<t.Program>) {
const exportedGetters = t.objectExpression([]);
const importDeclarations: ImportEntry[] = [];
const importedBindings = new Map<string, {
bindings: BindingEntry[];
exportName: string | null;
}>();
const specifierToBindings = new Map<string, BindingEntry[]>();
interface ModuleRequestNode {
assertions?: t.ImportAttribute[] | null;
attributes?: t.ImportAttribute[] | null;
source?: t.StringLiteral | null;
}
const acquireModuleRequestBindings = (moduleRequest: ModuleRequestNode) => {
assert.ok(moduleRequest.source);
// Convert import assertions into `with` URL search parameters. That way the underlying
// loader can pass forward the assertions, but the runtime imports will be plain.
const attributes = moduleRequest.attributes ?? moduleRequest.assertions ?? [];
const specifier = moduleRequest.source.value;
const params = new URLSearchParams([
[ "specifier", specifier ],
...Fn.map(
attributes, assertion => [
"with",
String(new URLSearchParams([ [ extractName(assertion.key), extractName(assertion.value) ] ])),
]),
] as [ string, string ][]);
const importSpecifier = `hot:module?${String(params)}`;
return specifierToBindings.get(importSpecifier) ?? function() {
const localName = `_${specifier.replaceAll(/[^A-Za-z0-9_$]/g, "_")}`;
const bindings: BindingEntry[] = [];
specifierToBindings.set(importSpecifier, bindings);
importDeclarations.push({
bindings,
identifier: localName,
importSpecifier,
specifier,
});
return bindings;
}();
};
// First step is to process import statements. These are rewritten to pass the requested
// bindings to the runtime module controller. This is done first so that later we know which
// names are re-exports. Since exports and imports can only occur at the top level there is no
// need for a recursive visitor, just looping over the top-level statements is sufficient.
for (const statement of program.get("body")) {
if (statement.isImportDeclaration()) {
// import identifier from "specifier";
const bindings = acquireModuleRequestBindings(statement.node);
for (const specifier of statement.node.specifiers) {
if (t.isImportDefaultSpecifier(specifier)) {
importedBindings.set(specifier.local.name, { exportName: "default", bindings });
bindings.push({
type: BindingType.import,
name: "default",
...specifier.local.name !== "default" && {
as: specifier.local.name,
},
});
} else if (t.isImportSpecifier(specifier)) {
const exportName = extractName(specifier.imported);
importedBindings.set(specifier.local.name, { exportName, bindings });
bindings.push({
type: BindingType.import,
name: exportName,
...specifier.local.name !== "default" && {
as: specifier.local.name,
},
});
} else {
assert.ok(t.isImportNamespaceSpecifier(specifier));
importedBindings.set(specifier.local.name, { exportName: null, bindings });
bindings.push({
type: BindingType.importStar,
as: specifier.local.name,
});
}
}
statement.remove();
}
}
// Now we walk the program statements again and process exports.
for (const statement of program.get("body")) {
if (statement.isExportAllDeclaration()) {
// export * from "specifier";
const bindings = acquireModuleRequestBindings(statement.node);
bindings.push({ type: BindingType.exportStar });
statement.remove();
} else if (statement.isExportDefaultDeclaration()) {
// export default expression;
const declaration = statement.get("declaration");
if (declaration.isClassDeclaration() || declaration.isFunctionDeclaration()) {
if (declaration.node.id == null) {
// Default exported classes and function declarations don't require a
// name. When we strip the `export` keyword this violates a bunch of
// invariants. We will give these declarations names.
declaration.node.id = program.scope.generateUidIdentifier("default");
program.scope.registerDeclaration(declaration);
}
// Add binding to this declaration by name
exportedGetters.properties.push(makeLocalGetter(declaration.node.id.name, "default"));
statement.replaceWith(declaration);
} else {
const indirectExport = declaration.isIdentifier() ? importedBindings.get(declaration.node.name) : undefined;
if (indirectExport === undefined) {
// This is an export by value. Care must be taken to avoid exporting a live
// binding to an underlying expression.
assert.ok(declaration.isExpression());
const id = program.scope.generateUid("default");
const next = statement.replaceWith(
t.variableDeclaration("const", [
t.variableDeclarator(t.identifier(id), declaration.node),
]));
program.scope.registerDeclaration(next[0]);
exportedGetters.properties.push(makeLocalGetter(id, "default"));
} else {
// This is actually a re-export of an existing import
if (indirectExport.exportName === null) {
// import * as foo from "bar";
// export default foo;
indirectExport.bindings.push({
type: BindingType.indirectStarExport,
as: "default",
});
} else {
// import foo from "bar";
// export default foo;
indirectExport.bindings.push({
type: BindingType.indirectExport,
name: indirectExport.exportName,
as: "default",
});
}
statement.remove();
}
}
} else if (statement.isExportNamedDeclaration()) {
if (statement.node.source) {
// export ... from "specifier";
assert.ok(!statement.node.declaration);
const bindings = acquireModuleRequestBindings(statement.node);
for (const specifier of statement.node.specifiers) {
if (t.isExportSpecifier(specifier)) {
const { exported, local } = specifier;
const exportedName = extractName(exported);
bindings.push({
type: BindingType.indirectExport,
name: local.name,
...exportedName !== local.name && {
as: exportedName,
},
});
} else {
assert.ok(t.isExportNamespaceSpecifier(specifier));
const { exported } = specifier;
const exportedName = extractName(exported);
bindings.push({
type: BindingType.indirectStarExport,
as: exportedName,
});
}
}
statement.remove();
continue;
}
// export { identifier };
for (const specifier of statement.node.specifiers) {
if (t.isExportSpecifier(specifier)) {
const { exported, local } = specifier;
const exportName = extractName(exported);
const indirectExport = importedBindings.get(local.name);
if (indirectExport === undefined) {
exportedGetters.properties.push(makeLocalGetter(local.name, exportName));
} else if (indirectExport.exportName === null) {
// import * as foo from "bar";
// export { foo };
indirectExport.bindings.push({
type: BindingType.indirectStarExport,
as: exportName,
});
} else {
// import foo from "bar";
// export { foo };
indirectExport.bindings.push({
type: BindingType.indirectExport,
name: indirectExport.exportName,
...exportName !== indirectExport.exportName && {
as: exportName,
},
});
}
}
}
// export function identifier() {}
const { declaration } = statement.node;
if (declaration) {
if (t.isClassDeclaration(declaration) || t.isFunctionDeclaration(declaration)) {
const name = declaration.id!.name;
exportedGetters.properties.push(makeLocalGetter(name, name));
} else if (t.isVariableDeclaration(declaration)) {
const { declarations } = declaration;
for (const declaration of declarations) {
for (const localName of Object.keys(t.getOuterBindingIdentifiers(declaration))) {
exportedGetters.properties.push(makeLocalGetter(localName, localName));
}
}
}
statement.replaceWith(declaration);
} else {
statement.remove();
}
}
}
// Now run the recursive visitor which replaces all runtime-managed references with getters.
// While we're in there we will also look for dynamic imports and top-level awaits.
const holderName = program.scope.generateUid("$");
const importDynamicName = program.scope.generateUid("import");
const importMetaName = program.scope.generateUid("meta");
const visitorState: VisitorState = {
holderName,
importedLocalNames: new Set(importedBindings.keys()),
importDynamicName,
importMetaName,
program,
usesDynamicImport: false,
usesImportMeta: false,
usesTopLevelAwait: false,
};
traverse(program.node, importToGetterVisitor, program.scope, visitorState);
// Finally, assemble a new body with the re-written imports, import descriptors, runtime-defined
// default export, and module body generator
program.node.body = [
t.functionDeclaration(
t.identifier("execute"),
[
t.identifier(importMetaName),
t.identifier(importDynamicName),
],
t.blockStatement([
t.variableDeclaration("let", [
t.variableDeclarator(
t.identifier(holderName),
t.yieldExpression(t.arrayExpression([
t.arrowFunctionExpression(
[ t.identifier("next") ],
t.blockStatement([
t.expressionStatement(
t.assignmentExpression("=", t.identifier(holderName), t.identifier("next"))),
])),
exportedGetters,
]))),
]),
t.expressionStatement(t.yieldExpression()),
...program.node.body,
]),
true,
visitorState.usesTopLevelAwait),
];
return {
importDeclarations,
usesDynamicImport: visitorState.usesDynamicImport,
usesImportMeta: visitorState.usesImportMeta,
usesTopLevelAwait: visitorState.usesTopLevelAwait,
};
}
interface VisitorState {
readonly importedLocalNames: ReadonlySet<string>;
readonly holderName: string;
readonly importDynamicName: string;
readonly importMetaName: string;
readonly program: NodePath<t.Program>;
usesDynamicImport: boolean;
usesImportMeta: boolean;
usesTopLevelAwait: boolean;
}
const importToGetterVisitor: Visitor<VisitorState> = {
// Look for top-level await
AwaitExpression(path) {
if (!this.usesTopLevelAwait && path.scope.getFunctionParent() === null) {
this.usesTopLevelAwait = true;
}
},
ForAwaitStatement(path) {
if (!this.usesTopLevelAwait && path.scope.getFunctionParent() === null) {
this.usesTopLevelAwait = true;
}
},
// Look for dynamic imports
CallExpression(path) {
if (t.isImport(path.node.callee)) {
this.usesDynamicImport = true;
path.get("callee").replaceWith(t.identifier(this.importDynamicName));
}
},
// Replace `import.meta`
MetaProperty(path) {
if (path.node.meta.name === "import" && path.node.property.name === "meta") {
path.replaceWith(t.identifier(this.importMetaName));
this.usesImportMeta = true;
}
},
// Replace imported bindings
Identifier(path) {
const localName = path.node.name;
if (this.importedLocalNames.has(localName)) {
const bindingScope = path.scope.getBinding(localName)?.scope;
if (bindingScope === undefined) {
path.replaceWith(t.callExpression(
t.memberExpression(t.identifier(this.holderName), path.node),
[]));
path.skip();
}
}
},
ClassMethod(path) {
if (!path.node.computed) {
path.skipKey("key");
}
},
ClassProperty(path) {
if (!path.node.computed) {
path.skipKey("key");
}
},
LabeledStatement(path) {
path.skipKey("label");
},
MemberExpression(path) {
if (!path.node.computed) {
path.skipKey("property");
}
},
ObjectMethod(path) {
if (!path.node.computed) {
path.skipKey("key");
}
},
ObjectProperty(path) {
if (!path.node.computed && t.isIdentifier(path.node.key)) {
if (path.node.shorthand) {
const localName = path.node.key.name;
if (this.importedLocalNames.has(localName)) {
const bindingScope = path.scope.getBinding(localName)?.scope;
if (bindingScope === undefined) {
path.replaceWith(t.objectProperty(
path.node.key,
t.callExpression(
t.memberExpression(t.identifier(this.holderName), t.identifier(localName)),
[])));
}
}
}
path.skipKey("key");
}
},
};