-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.ts
101 lines (86 loc) 路 3.08 KB
/
index.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
import { declare } from "@babel/helper-plugin-utils";
import type { NodePath } from "@babel/traverse";
import type { types as t } from "@babel/core";
import syntaxImportSourcePhase from "@babel/plugin-syntax-import-source-phase";
export default declare(({ assertVersion, types: t, template }) => {
assertVersion(7);
function buildFetch(
path: NodePath<t.ImportDeclaration | t.ImportExpression>,
) {
let specifier = path.node.source;
let specifierRef: t.Expression;
if (t.isStringLiteral(specifier)) {
specifierRef = specifier;
} else {
specifierRef = path.scope.generateDeclaredUidIdentifier("specifier");
specifier = t.assignmentExpression(
"=",
t.cloneNode(specifierRef),
specifier,
);
}
return template.expression.ast`
WebAssembly.compileStreaming(fetch(
import.meta.resolve?.(${specifier}) ??
new URL(${t.cloneNode(specifierRef)}, import.meta.url)
))
`;
}
return {
name: "proposal-import-wasm-source-to-fetch",
inherits: syntaxImportSourcePhase,
visitor: {
Program(path) {
if (path.node.sourceType !== "module") return;
// TS reports that 'Assertions require every name in the call target to be
// declared with an explicit type annotation.' if we just call
// t.assertImportDefaultSpecifier, so we _explicitly_ annotate `t` as
// having type `typeof t`. Unfortunately this is a design limitation of
// the TS type checker, so we need to manually help it:
// https://github.com/microsoft/TypeScript/issues/36931
const t2: typeof t = t;
const ids: t.Identifier[] = [];
const fetches: t.Expression[] = [];
for (const decl of path.get("body")) {
if (!decl.isImportDeclaration({ phase: "source" })) continue;
if (decl.node.attributes?.length || decl.node.assertions?.length) {
throw path.buildCodeFrameError(
"`import source` with import attributes cannot be compiled.",
);
}
const specifier = decl.node.specifiers[0];
t2.assertImportDefaultSpecifier(specifier);
ids.push(specifier.local);
fetches.push(buildFetch(decl));
decl.remove();
}
if (ids.length === 0) return;
let lhs: t.LVal, rhs: t.Expression;
if (ids.length === 1) {
lhs = ids[0];
rhs = fetches[0];
} else {
lhs = t.arrayPattern(ids);
rhs = template.expression.ast`
Promise.all(${t.arrayExpression(fetches)})
`;
}
path.unshiftContainer(
"body",
t.variableDeclaration("const", [
t.variableDeclarator(lhs, t.awaitExpression(rhs)),
]),
);
},
ImportExpression(path) {
if (path.node.phase !== "source") return;
if (path.node.options) {
throw path.buildCodeFrameError(
"`import.source` with an options bag cannot be compiled.",
);
}
path.replaceWith(buildFetch(path));
},
},
};
});