dismantle 0.2.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/LICENSE +7 -0
- package/README.md +40 -0
- package/dist/cjs/development/index.cjs +1484 -0
- package/dist/cjs/development/index.cjs.map +7 -0
- package/dist/cjs/development/runtime.cjs +1484 -0
- package/dist/cjs/development/runtime.cjs.map +7 -0
- package/dist/cjs/production/index.cjs +1 -0
- package/dist/cjs/production/runtime.cjs +1 -0
- package/dist/esm/development/index.mjs +1453 -0
- package/dist/esm/development/index.mjs.map +7 -0
- package/dist/esm/development/runtime.mjs +1453 -0
- package/dist/esm/development/runtime.mjs.map +7 -0
- package/dist/esm/production/index.mjs +1 -0
- package/dist/esm/production/runtime.mjs +1 -0
- package/dist/types/hidden-imports.d.ts +4 -0
- package/dist/types/hidden-imports.d.ts.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/plugin.d.ts +8 -0
- package/dist/types/plugin.d.ts.map +1 -0
- package/dist/types/split.d.ts +7 -0
- package/dist/types/split.d.ts.map +1 -0
- package/dist/types/transform-block.d.ts +5 -0
- package/dist/types/transform-block.d.ts.map +1 -0
- package/dist/types/transform-call.d.ts +5 -0
- package/dist/types/transform-call.d.ts.map +1 -0
- package/dist/types/types.d.ts +62 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/utils/assert.d.ts +2 -0
- package/dist/types/utils/assert.d.ts.map +1 -0
- package/dist/types/utils/generator-shim.d.ts +4 -0
- package/dist/types/utils/generator-shim.d.ts.map +1 -0
- package/dist/types/utils/get-descriptive-name.d.ts +3 -0
- package/dist/types/utils/get-descriptive-name.d.ts.map +1 -0
- package/dist/types/utils/get-foreign-bindings.d.ts +3 -0
- package/dist/types/utils/get-foreign-bindings.d.ts.map +1 -0
- package/dist/types/utils/get-identifiers-from-lval.d.ts +3 -0
- package/dist/types/utils/get-identifiers-from-lval.d.ts.map +1 -0
- package/dist/types/utils/get-import-identifier.d.ts +5 -0
- package/dist/types/utils/get-import-identifier.d.ts.map +1 -0
- package/dist/types/utils/get-import-specifier-name.d.ts +3 -0
- package/dist/types/utils/get-import-specifier-name.d.ts.map +1 -0
- package/dist/types/utils/get-module-definition.d.ts +4 -0
- package/dist/types/utils/get-module-definition.d.ts.map +1 -0
- package/dist/types/utils/get-root-statement-path.d.ts +3 -0
- package/dist/types/utils/get-root-statement-path.d.ts.map +1 -0
- package/dist/types/utils/register-import-specifiers.d.ts +5 -0
- package/dist/types/utils/register-import-specifiers.d.ts.map +1 -0
- package/dist/types/utils/unwrap.d.ts +11 -0
- package/dist/types/utils/unwrap.d.ts.map +1 -0
- package/dist/types/utils/xxhash32.d.ts +7 -0
- package/dist/types/utils/xxhash32.d.ts.map +1 -0
- package/package.json +90 -0
- package/src/hidden-imports.ts +13 -0
- package/src/index.ts +90 -0
- package/src/plugin.ts +26 -0
- package/src/split.ts +1074 -0
- package/src/transform-block.ts +115 -0
- package/src/transform-call.ts +100 -0
- package/src/types.ts +73 -0
- package/src/utils/assert.ts +5 -0
- package/src/utils/errors.ts +21 -0
- package/src/utils/generator-shim.ts +22 -0
- package/src/utils/get-descriptive-name.ts +42 -0
- package/src/utils/get-foreign-bindings.ts +60 -0
- package/src/utils/get-identifiers-from-lval.ts +44 -0
- package/src/utils/get-import-identifier.ts +23 -0
- package/src/utils/get-import-specifier-name.ts +10 -0
- package/src/utils/get-module-definition.ts +53 -0
- package/src/utils/get-root-statement-path.ts +14 -0
- package/src/utils/register-import-specifiers.ts +77 -0
- package/src/utils/unwrap.ts +63 -0
- package/src/utils/xxhash32.ts +214 -0
|
@@ -0,0 +1,1484 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
compile: () => compile
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
var babel = __toESM(require("@babel/core"), 1);
|
|
37
|
+
var import_node_path = __toESM(require("node:path"), 1);
|
|
38
|
+
|
|
39
|
+
// src/split.ts
|
|
40
|
+
var t5 = __toESM(require("@babel/types"), 1);
|
|
41
|
+
|
|
42
|
+
// src/hidden-imports.ts
|
|
43
|
+
var HIDDEN_FUNC = {
|
|
44
|
+
kind: "named",
|
|
45
|
+
source: "dismantle/runtime",
|
|
46
|
+
name: "$$func"
|
|
47
|
+
};
|
|
48
|
+
var HIDDEN_GENERATOR = {
|
|
49
|
+
kind: "named",
|
|
50
|
+
source: "dismantle/runtime",
|
|
51
|
+
name: "$$gen"
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// src/utils/assert.ts
|
|
55
|
+
function assert(cond, error) {
|
|
56
|
+
if (!cond) {
|
|
57
|
+
throw new Error(error);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/utils/generator-shim.ts
|
|
62
|
+
var import_generator = __toESM(require("@babel/generator"), 1);
|
|
63
|
+
var generator = typeof import_generator.default !== "function" ? import_generator.default.default : import_generator.default;
|
|
64
|
+
function generateCode(id, node) {
|
|
65
|
+
const result = generator(node, {
|
|
66
|
+
sourceMaps: true,
|
|
67
|
+
sourceFileName: id
|
|
68
|
+
});
|
|
69
|
+
return {
|
|
70
|
+
code: result.code,
|
|
71
|
+
map: result.map
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/utils/get-descriptive-name.ts
|
|
76
|
+
function getDescriptiveName(path2, defaultName) {
|
|
77
|
+
let current = path2;
|
|
78
|
+
while (current) {
|
|
79
|
+
switch (current.node.type) {
|
|
80
|
+
case "FunctionDeclaration":
|
|
81
|
+
case "FunctionExpression": {
|
|
82
|
+
if (current.node.id) {
|
|
83
|
+
return current.node.id.name;
|
|
84
|
+
}
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
case "VariableDeclarator": {
|
|
88
|
+
if (current.node.id.type === "Identifier") {
|
|
89
|
+
return current.node.id.name;
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
case "ClassPrivateMethod":
|
|
94
|
+
case "ClassMethod":
|
|
95
|
+
case "ObjectMethod": {
|
|
96
|
+
switch (current.node.key.type) {
|
|
97
|
+
case "Identifier":
|
|
98
|
+
return current.node.key.name;
|
|
99
|
+
case "PrivateName":
|
|
100
|
+
return current.node.key.id.name;
|
|
101
|
+
default:
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
default:
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
current = current.parentPath;
|
|
110
|
+
}
|
|
111
|
+
return defaultName;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/utils/get-foreign-bindings.ts
|
|
115
|
+
var t = __toESM(require("@babel/types"), 1);
|
|
116
|
+
function isForeignBinding(source, current, name, mode) {
|
|
117
|
+
if (current.scope.hasGlobal(name)) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
if (source === current) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
if (current.scope.hasOwnBinding(name)) {
|
|
124
|
+
if (mode === "block") {
|
|
125
|
+
const binding = current.scope.getBinding(name);
|
|
126
|
+
return !!binding && binding.kind === "param";
|
|
127
|
+
}
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
if (current.parentPath) {
|
|
131
|
+
return isForeignBinding(source, current.parentPath, name, mode);
|
|
132
|
+
}
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
function isInTypescript(path2) {
|
|
136
|
+
let parent = path2.parentPath;
|
|
137
|
+
while (parent) {
|
|
138
|
+
if (t.isTypeScript(parent.node) && !t.isExpression(parent.node)) {
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
parent = parent.parentPath;
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
function getForeignBindings(path2, mode) {
|
|
146
|
+
const identifiers = /* @__PURE__ */ new Set();
|
|
147
|
+
path2.traverse({
|
|
148
|
+
ReferencedIdentifier(p) {
|
|
149
|
+
if (!isInTypescript(p) && (mode === "expression" ? !path2.scope.hasGlobal(p.node.name) : isForeignBinding(path2, p, p.node.name, mode))) {
|
|
150
|
+
identifiers.add(p.node.name);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
return identifiers;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/utils/get-identifiers-from-lval.ts
|
|
158
|
+
var t2 = __toESM(require("@babel/types"), 1);
|
|
159
|
+
function getIdentifiersFromArrayPattern(node) {
|
|
160
|
+
const ids = [];
|
|
161
|
+
for (let i = 0, len = node.elements.length; i < len; i++) {
|
|
162
|
+
const el = node.elements[i];
|
|
163
|
+
if (el) {
|
|
164
|
+
ids.push(...getIdentifiersFromLVal(el));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return ids;
|
|
168
|
+
}
|
|
169
|
+
function getIdentifiersFromObjectPattern(node) {
|
|
170
|
+
const ids = [];
|
|
171
|
+
for (let i = 0, len = node.properties.length; i < len; i++) {
|
|
172
|
+
const el = node.properties[i];
|
|
173
|
+
if (el) {
|
|
174
|
+
if (el.type === "RestElement") {
|
|
175
|
+
ids.push(...getIdentifiersFromLVal(el));
|
|
176
|
+
} else if (t2.isLVal(el.value)) {
|
|
177
|
+
ids.push(...getIdentifiersFromLVal(el.value));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return ids;
|
|
182
|
+
}
|
|
183
|
+
function getIdentifiersFromLVal(node) {
|
|
184
|
+
switch (node.type) {
|
|
185
|
+
case "Identifier":
|
|
186
|
+
return [node.name];
|
|
187
|
+
case "ArrayPattern":
|
|
188
|
+
return getIdentifiersFromArrayPattern(node);
|
|
189
|
+
case "AssignmentPattern":
|
|
190
|
+
return getIdentifiersFromLVal(node.left);
|
|
191
|
+
case "ObjectPattern":
|
|
192
|
+
return getIdentifiersFromObjectPattern(node);
|
|
193
|
+
case "RestElement":
|
|
194
|
+
return getIdentifiersFromLVal(node.argument);
|
|
195
|
+
default:
|
|
196
|
+
return [];
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/utils/get-import-identifier.ts
|
|
201
|
+
var import_helper_module_imports = require("@babel/helper-module-imports");
|
|
202
|
+
function getImportIdentifier(state, path2, registration) {
|
|
203
|
+
const name = registration.kind === "named" ? registration.name : "default";
|
|
204
|
+
const target = `${registration.source}[${name}]`;
|
|
205
|
+
const current = state.imports.get(target);
|
|
206
|
+
if (current) {
|
|
207
|
+
return current;
|
|
208
|
+
}
|
|
209
|
+
const newID = registration.kind === "named" ? (0, import_helper_module_imports.addNamed)(path2, registration.name, registration.source) : (0, import_helper_module_imports.addDefault)(path2, registration.source);
|
|
210
|
+
state.imports.set(target, newID);
|
|
211
|
+
return newID;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// src/utils/get-module-definition.ts
|
|
215
|
+
var t3 = __toESM(require("@babel/types"), 1);
|
|
216
|
+
|
|
217
|
+
// src/utils/get-import-specifier-name.ts
|
|
218
|
+
function getImportSpecifierName(node) {
|
|
219
|
+
switch (node.imported.type) {
|
|
220
|
+
case "Identifier":
|
|
221
|
+
return node.imported.name;
|
|
222
|
+
case "StringLiteral":
|
|
223
|
+
return node.imported.value;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// src/utils/unwrap.ts
|
|
228
|
+
function isPathValid(path2, key) {
|
|
229
|
+
return key(path2.node);
|
|
230
|
+
}
|
|
231
|
+
function isNestedExpression(node) {
|
|
232
|
+
switch (node.type) {
|
|
233
|
+
case "ParenthesizedExpression":
|
|
234
|
+
case "TypeCastExpression":
|
|
235
|
+
case "TSAsExpression":
|
|
236
|
+
case "TSSatisfiesExpression":
|
|
237
|
+
case "TSNonNullExpression":
|
|
238
|
+
case "TSTypeAssertion":
|
|
239
|
+
case "TSInstantiationExpression":
|
|
240
|
+
return true;
|
|
241
|
+
default:
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function unwrapNode(node, key) {
|
|
246
|
+
if (key(node)) {
|
|
247
|
+
return node;
|
|
248
|
+
}
|
|
249
|
+
if (isNestedExpression(node)) {
|
|
250
|
+
return unwrapNode(node.expression, key);
|
|
251
|
+
}
|
|
252
|
+
return void 0;
|
|
253
|
+
}
|
|
254
|
+
function unwrapPath(path2, key) {
|
|
255
|
+
if (isPathValid(path2, key)) {
|
|
256
|
+
return path2;
|
|
257
|
+
}
|
|
258
|
+
if (isPathValid(path2, isNestedExpression)) {
|
|
259
|
+
return unwrapPath(path2.get("expression"), key);
|
|
260
|
+
}
|
|
261
|
+
return void 0;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// src/utils/get-module-definition.ts
|
|
265
|
+
function getModuleDefinition(path2) {
|
|
266
|
+
if (!(isPathValid(path2, t3.isImportSpecifier) || isPathValid(path2, t3.isImportDefaultSpecifier) || isPathValid(path2, t3.isImportNamespaceSpecifier))) {
|
|
267
|
+
return void 0;
|
|
268
|
+
}
|
|
269
|
+
const parent = path2.getStatementParent();
|
|
270
|
+
const source = parent.node.source.value;
|
|
271
|
+
switch (path2.node.type) {
|
|
272
|
+
case "ImportDefaultSpecifier":
|
|
273
|
+
return {
|
|
274
|
+
source,
|
|
275
|
+
kind: "default",
|
|
276
|
+
local: path2.node.local.name
|
|
277
|
+
};
|
|
278
|
+
case "ImportNamespaceSpecifier":
|
|
279
|
+
return {
|
|
280
|
+
source,
|
|
281
|
+
kind: "namespace",
|
|
282
|
+
local: path2.node.local.name
|
|
283
|
+
};
|
|
284
|
+
case "ImportSpecifier": {
|
|
285
|
+
const imported = getImportSpecifierName(path2.node);
|
|
286
|
+
if (imported === "default") {
|
|
287
|
+
return {
|
|
288
|
+
source,
|
|
289
|
+
kind: "default",
|
|
290
|
+
local: path2.node.local.name,
|
|
291
|
+
imported: ""
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
return {
|
|
295
|
+
source,
|
|
296
|
+
kind: "named",
|
|
297
|
+
local: path2.node.local.name,
|
|
298
|
+
imported
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// src/utils/get-root-statement-path.ts
|
|
305
|
+
var t4 = __toESM(require("@babel/types"), 1);
|
|
306
|
+
function getRootStatementPath(path2) {
|
|
307
|
+
let current = path2.parentPath;
|
|
308
|
+
while (current) {
|
|
309
|
+
const next = current.parentPath;
|
|
310
|
+
if (next && t4.isProgram(next.node)) {
|
|
311
|
+
return current;
|
|
312
|
+
}
|
|
313
|
+
current = next;
|
|
314
|
+
}
|
|
315
|
+
return path2;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// src/split.ts
|
|
319
|
+
function moduleDefinitionToImportSpecifier(definition) {
|
|
320
|
+
switch (definition.kind) {
|
|
321
|
+
case "default":
|
|
322
|
+
return t5.importDefaultSpecifier(t5.identifier(definition.local));
|
|
323
|
+
case "named":
|
|
324
|
+
return t5.importSpecifier(
|
|
325
|
+
t5.identifier(definition.local),
|
|
326
|
+
definition.imported ? t5.identifier(definition.imported) : t5.identifier(definition.local)
|
|
327
|
+
);
|
|
328
|
+
case "namespace":
|
|
329
|
+
return t5.importNamespaceSpecifier(t5.identifier(definition.local));
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
function moduleDefinitionToImportDeclaration(definition) {
|
|
333
|
+
return t5.importDeclaration(
|
|
334
|
+
[moduleDefinitionToImportSpecifier(definition)],
|
|
335
|
+
t5.stringLiteral(definition.source)
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
function moduleDefinitionsToImportDeclarations(definitions) {
|
|
339
|
+
const declarations = [];
|
|
340
|
+
for (let i = 0, len = definitions.length; i < len; i++) {
|
|
341
|
+
declarations[i] = moduleDefinitionToImportDeclaration(definitions[i]);
|
|
342
|
+
}
|
|
343
|
+
return declarations;
|
|
344
|
+
}
|
|
345
|
+
function isMutation(kind) {
|
|
346
|
+
switch (kind) {
|
|
347
|
+
case "let":
|
|
348
|
+
case "var":
|
|
349
|
+
case "param":
|
|
350
|
+
return true;
|
|
351
|
+
case "const":
|
|
352
|
+
case "hoisted":
|
|
353
|
+
case "local":
|
|
354
|
+
case "module":
|
|
355
|
+
case "unknown":
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
function registerModuleLevelBinding(ctx, modules, binding, target) {
|
|
360
|
+
const current = ctx.bindings.get(binding);
|
|
361
|
+
if (current) {
|
|
362
|
+
modules.push(current);
|
|
363
|
+
} else if (isPathValid(target.path, t5.isVariableDeclarator)) {
|
|
364
|
+
const definitions = splitVariableDeclarator(ctx, target.path);
|
|
365
|
+
for (let k = 0, klen = definitions.length; k < klen; k++) {
|
|
366
|
+
modules.push(definitions[k]);
|
|
367
|
+
ctx.bindings.set(definitions[k].local, definitions[k]);
|
|
368
|
+
}
|
|
369
|
+
} else if (isPathValid(target.path, t5.isFunctionDeclaration)) {
|
|
370
|
+
const definition = splitFunctionDeclaration(ctx, target.path);
|
|
371
|
+
modules.push(definition);
|
|
372
|
+
ctx.bindings.set(definition.local, definition);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
function registerBinding(ctx, modules, locals, mutations, binding, target, pure) {
|
|
376
|
+
if (target.kind === "module") {
|
|
377
|
+
const result = getModuleDefinition(target.path);
|
|
378
|
+
if (result) {
|
|
379
|
+
modules.push(result);
|
|
380
|
+
}
|
|
381
|
+
} else if (target.kind === "param") {
|
|
382
|
+
locals.push(target.identifier);
|
|
383
|
+
} else {
|
|
384
|
+
let blockParent = target.path.scope.getBlockParent();
|
|
385
|
+
const programParent = target.path.scope.getProgramParent();
|
|
386
|
+
if (blockParent.path === target.path) {
|
|
387
|
+
blockParent = blockParent.parent;
|
|
388
|
+
}
|
|
389
|
+
if (blockParent === programParent) {
|
|
390
|
+
registerModuleLevelBinding(ctx, modules, binding, target);
|
|
391
|
+
} else if (!pure) {
|
|
392
|
+
locals.push(target.identifier);
|
|
393
|
+
if (isMutation(target.kind)) {
|
|
394
|
+
mutations.push(target.identifier);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
function extractBindings(ctx, path2, bindings, pure) {
|
|
400
|
+
const modules = [];
|
|
401
|
+
const locals = [];
|
|
402
|
+
const mutations = [];
|
|
403
|
+
for (const binding of bindings) {
|
|
404
|
+
const target = path2.scope.getBinding(binding);
|
|
405
|
+
if (target) {
|
|
406
|
+
registerBinding(ctx, modules, locals, mutations, binding, target, pure);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return {
|
|
410
|
+
modules,
|
|
411
|
+
locals,
|
|
412
|
+
mutations
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
function createVirtualFileName(ctx) {
|
|
416
|
+
return `./${ctx.path.base}?${ctx.options.key}=${ctx.virtual.count++}${ctx.path.ext}`;
|
|
417
|
+
}
|
|
418
|
+
function createRootFile(ctx, bindings, replacement) {
|
|
419
|
+
const rootFile = createVirtualFileName(ctx);
|
|
420
|
+
const rootContent = generateCode(
|
|
421
|
+
ctx.id,
|
|
422
|
+
t5.program([
|
|
423
|
+
...ctx.options.mode === "server" ? moduleDefinitionsToImportDeclarations(bindings.modules) : [],
|
|
424
|
+
t5.exportDefaultDeclaration(replacement)
|
|
425
|
+
])
|
|
426
|
+
);
|
|
427
|
+
ctx.onVirtualFile(
|
|
428
|
+
rootFile,
|
|
429
|
+
{ code: rootContent.code, map: rootContent.map },
|
|
430
|
+
"root"
|
|
431
|
+
);
|
|
432
|
+
return rootFile;
|
|
433
|
+
}
|
|
434
|
+
function createEntryFile(ctx, path2, rootFile, imported) {
|
|
435
|
+
let id = `${ctx.blocks.hash}-${ctx.blocks.count++}`;
|
|
436
|
+
if (ctx.options.env !== "production") {
|
|
437
|
+
id += `-${getDescriptiveName(path2, "anonymous")}`;
|
|
438
|
+
}
|
|
439
|
+
const entryID = path2.scope.generateUidIdentifier("entry");
|
|
440
|
+
const entryImports = [
|
|
441
|
+
{
|
|
442
|
+
kind: imported.kind,
|
|
443
|
+
source: imported.source,
|
|
444
|
+
local: entryID.name,
|
|
445
|
+
imported: imported.kind === "named" ? imported.name : void 0
|
|
446
|
+
}
|
|
447
|
+
];
|
|
448
|
+
const args = [t5.stringLiteral(id)];
|
|
449
|
+
if (ctx.options.mode === "server") {
|
|
450
|
+
const rootID = path2.scope.generateUidIdentifier("root");
|
|
451
|
+
entryImports.push({
|
|
452
|
+
kind: "default",
|
|
453
|
+
source: rootFile,
|
|
454
|
+
local: rootID.name
|
|
455
|
+
});
|
|
456
|
+
args.push(rootID);
|
|
457
|
+
}
|
|
458
|
+
const entryFile = createVirtualFileName(ctx);
|
|
459
|
+
const entryContent = generateCode(
|
|
460
|
+
ctx.id,
|
|
461
|
+
t5.program([
|
|
462
|
+
...moduleDefinitionsToImportDeclarations(entryImports),
|
|
463
|
+
t5.exportDefaultDeclaration(t5.callExpression(entryID, args))
|
|
464
|
+
])
|
|
465
|
+
);
|
|
466
|
+
ctx.onVirtualFile(
|
|
467
|
+
entryFile,
|
|
468
|
+
{ code: entryContent.code, map: entryContent.map },
|
|
469
|
+
"entry"
|
|
470
|
+
);
|
|
471
|
+
return entryFile;
|
|
472
|
+
}
|
|
473
|
+
function splitFunctionDeclaration(ctx, path2) {
|
|
474
|
+
const bindings = getForeignBindings(path2, "function");
|
|
475
|
+
const { modules } = extractBindings(ctx, path2, bindings);
|
|
476
|
+
const file = createVirtualFileName(ctx);
|
|
477
|
+
const compiled = generateCode(
|
|
478
|
+
ctx.id,
|
|
479
|
+
t5.program([
|
|
480
|
+
...moduleDefinitionsToImportDeclarations(modules),
|
|
481
|
+
t5.exportNamedDeclaration(path2.node)
|
|
482
|
+
])
|
|
483
|
+
);
|
|
484
|
+
ctx.onVirtualFile(file, { code: compiled.code, map: compiled.map }, "none");
|
|
485
|
+
const statement = getRootStatementPath(path2);
|
|
486
|
+
const identifier2 = path2.node.id || path2.scope.generateUidIdentifier("func");
|
|
487
|
+
const definition = {
|
|
488
|
+
kind: "named",
|
|
489
|
+
local: identifier2.name,
|
|
490
|
+
source: file
|
|
491
|
+
};
|
|
492
|
+
statement.insertBefore(moduleDefinitionToImportDeclaration(definition));
|
|
493
|
+
path2.remove();
|
|
494
|
+
return definition;
|
|
495
|
+
}
|
|
496
|
+
function splitVariableDeclarator(ctx, path2) {
|
|
497
|
+
const init = unwrapPath(path2.get("init"), t5.isExpression);
|
|
498
|
+
const modules = init ? extractBindings(
|
|
499
|
+
ctx,
|
|
500
|
+
path2,
|
|
501
|
+
getForeignBindings(
|
|
502
|
+
path2,
|
|
503
|
+
isPathValid(init, t5.isArrowFunctionExpression) || isPathValid(init, t5.isFunctionExpression) ? "function" : "expression"
|
|
504
|
+
)
|
|
505
|
+
).modules : [];
|
|
506
|
+
const file = createVirtualFileName(ctx);
|
|
507
|
+
const parent = path2.parentPath.node;
|
|
508
|
+
const compiled = generateCode(
|
|
509
|
+
ctx.id,
|
|
510
|
+
t5.program([
|
|
511
|
+
...moduleDefinitionsToImportDeclarations(modules),
|
|
512
|
+
t5.exportNamedDeclaration(t5.variableDeclaration(parent.kind, [path2.node]))
|
|
513
|
+
])
|
|
514
|
+
);
|
|
515
|
+
ctx.onVirtualFile(file, { code: compiled.code, map: compiled.map }, "none");
|
|
516
|
+
const definitions = getIdentifiersFromLVal(
|
|
517
|
+
path2.node.id
|
|
518
|
+
).map((name) => ({
|
|
519
|
+
kind: "named",
|
|
520
|
+
local: name,
|
|
521
|
+
source: file
|
|
522
|
+
}));
|
|
523
|
+
const statement = getRootStatementPath(path2);
|
|
524
|
+
if (statement) {
|
|
525
|
+
statement.insertBefore(moduleDefinitionsToImportDeclarations(definitions));
|
|
526
|
+
}
|
|
527
|
+
path2.remove();
|
|
528
|
+
return definitions;
|
|
529
|
+
}
|
|
530
|
+
var BREAK_KEY = t5.numericLiteral(0);
|
|
531
|
+
var CONTINUE_KEY = t5.numericLiteral(1);
|
|
532
|
+
var RETURN_KEY = t5.numericLiteral(2);
|
|
533
|
+
var NO_HALT_KEY = t5.numericLiteral(3);
|
|
534
|
+
var THROW_KEY = t5.numericLiteral(4);
|
|
535
|
+
var YIELD_KEY = t5.numericLiteral(5);
|
|
536
|
+
function transformBlockContent(path2, mutations) {
|
|
537
|
+
const target = path2.scope.getFunctionParent() || path2.scope.getProgramParent();
|
|
538
|
+
const breaks = [];
|
|
539
|
+
let breakCount = 0;
|
|
540
|
+
const continues = [];
|
|
541
|
+
let continueCount = 0;
|
|
542
|
+
let hasReturn = false;
|
|
543
|
+
let hasYield = false;
|
|
544
|
+
const applyMutations = mutations.length ? path2.scope.generateUidIdentifier("mutate") : void 0;
|
|
545
|
+
path2.traverse({
|
|
546
|
+
BreakStatement(child) {
|
|
547
|
+
const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
|
|
548
|
+
if (parent === target) {
|
|
549
|
+
const replacement = [BREAK_KEY];
|
|
550
|
+
breakCount++;
|
|
551
|
+
if (child.node.label) {
|
|
552
|
+
const targetName = child.node.label.name;
|
|
553
|
+
breaks.push(targetName);
|
|
554
|
+
replacement.push(t5.stringLiteral(targetName));
|
|
555
|
+
} else {
|
|
556
|
+
replacement.push(t5.nullLiteral());
|
|
557
|
+
}
|
|
558
|
+
if (applyMutations) {
|
|
559
|
+
replacement.push(t5.callExpression(applyMutations, []));
|
|
560
|
+
}
|
|
561
|
+
child.replaceWith(t5.returnStatement(t5.arrayExpression(replacement)));
|
|
562
|
+
child.skip();
|
|
563
|
+
}
|
|
564
|
+
},
|
|
565
|
+
ContinueStatement(child) {
|
|
566
|
+
const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
|
|
567
|
+
if (parent === target) {
|
|
568
|
+
const replacement = [CONTINUE_KEY];
|
|
569
|
+
continueCount++;
|
|
570
|
+
if (child.node.label) {
|
|
571
|
+
const targetName = child.node.label.name;
|
|
572
|
+
continues.push(targetName);
|
|
573
|
+
replacement.push(t5.stringLiteral(targetName));
|
|
574
|
+
} else {
|
|
575
|
+
replacement.push(t5.nullLiteral());
|
|
576
|
+
}
|
|
577
|
+
if (applyMutations) {
|
|
578
|
+
replacement.push(t5.callExpression(applyMutations, []));
|
|
579
|
+
}
|
|
580
|
+
child.replaceWith(t5.returnStatement(t5.arrayExpression(replacement)));
|
|
581
|
+
child.skip();
|
|
582
|
+
}
|
|
583
|
+
},
|
|
584
|
+
ReturnStatement(child) {
|
|
585
|
+
const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
|
|
586
|
+
if (parent === target) {
|
|
587
|
+
hasReturn = true;
|
|
588
|
+
const arg = child.get("argument");
|
|
589
|
+
arg.replaceWith(
|
|
590
|
+
t5.arrayExpression([
|
|
591
|
+
RETURN_KEY,
|
|
592
|
+
arg.node ? arg.node : t5.nullLiteral(),
|
|
593
|
+
applyMutations ? t5.callExpression(applyMutations, []) : t5.nullLiteral()
|
|
594
|
+
])
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
},
|
|
598
|
+
YieldExpression(child) {
|
|
599
|
+
const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
|
|
600
|
+
if (parent === target) {
|
|
601
|
+
hasYield = true;
|
|
602
|
+
if (child.node.delegate) {
|
|
603
|
+
} else {
|
|
604
|
+
const arg = child.get("argument");
|
|
605
|
+
arg.replaceWith(
|
|
606
|
+
t5.arrayExpression([
|
|
607
|
+
YIELD_KEY,
|
|
608
|
+
arg.node ? arg.node : t5.nullLiteral(),
|
|
609
|
+
applyMutations ? t5.callExpression(applyMutations, []) : t5.nullLiteral()
|
|
610
|
+
])
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
const error = path2.scope.generateUidIdentifier("error");
|
|
617
|
+
const throwResult = [THROW_KEY, error];
|
|
618
|
+
const haltResult = [NO_HALT_KEY];
|
|
619
|
+
if (applyMutations) {
|
|
620
|
+
throwResult.push(t5.callExpression(applyMutations, []));
|
|
621
|
+
haltResult.push(t5.nullLiteral());
|
|
622
|
+
haltResult.push(t5.callExpression(applyMutations, []));
|
|
623
|
+
}
|
|
624
|
+
const statements = [
|
|
625
|
+
t5.tryStatement(
|
|
626
|
+
t5.blockStatement(path2.node.body),
|
|
627
|
+
t5.catchClause(
|
|
628
|
+
error,
|
|
629
|
+
t5.blockStatement([t5.returnStatement(t5.arrayExpression(throwResult))])
|
|
630
|
+
)
|
|
631
|
+
),
|
|
632
|
+
t5.returnStatement(t5.arrayExpression(haltResult))
|
|
633
|
+
];
|
|
634
|
+
if (applyMutations) {
|
|
635
|
+
statements.unshift(
|
|
636
|
+
t5.variableDeclaration("const", [
|
|
637
|
+
t5.variableDeclarator(
|
|
638
|
+
applyMutations,
|
|
639
|
+
t5.arrowFunctionExpression(
|
|
640
|
+
[],
|
|
641
|
+
t5.objectExpression(
|
|
642
|
+
mutations.map((item) => t5.objectProperty(item, item, false, true))
|
|
643
|
+
)
|
|
644
|
+
)
|
|
645
|
+
)
|
|
646
|
+
])
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
path2.node.body = statements;
|
|
650
|
+
return { breaks, continues, hasReturn, hasYield, breakCount, continueCount };
|
|
651
|
+
}
|
|
652
|
+
function getBreakCheck(returnType, returnResult, breakCount, breaks, check) {
|
|
653
|
+
let current;
|
|
654
|
+
if (breakCount !== breaks.length) {
|
|
655
|
+
current = t5.blockStatement([t5.breakStatement()]);
|
|
656
|
+
}
|
|
657
|
+
for (let i = 0, len = breaks.length; i < len; i++) {
|
|
658
|
+
const target = breaks[i];
|
|
659
|
+
current = t5.blockStatement([
|
|
660
|
+
t5.ifStatement(
|
|
661
|
+
t5.binaryExpression("===", returnResult, t5.stringLiteral(target)),
|
|
662
|
+
t5.blockStatement([t5.breakStatement(t5.identifier(target))]),
|
|
663
|
+
current
|
|
664
|
+
)
|
|
665
|
+
]);
|
|
666
|
+
}
|
|
667
|
+
if (current) {
|
|
668
|
+
return t5.ifStatement(
|
|
669
|
+
t5.binaryExpression("===", returnType, BREAK_KEY),
|
|
670
|
+
current,
|
|
671
|
+
check
|
|
672
|
+
);
|
|
673
|
+
}
|
|
674
|
+
return check;
|
|
675
|
+
}
|
|
676
|
+
function getContinueCheck(returnType, returnResult, continueCount, continues, check) {
|
|
677
|
+
let current;
|
|
678
|
+
if (continueCount !== continues.length) {
|
|
679
|
+
current = t5.blockStatement([t5.continueStatement()]);
|
|
680
|
+
}
|
|
681
|
+
for (let i = 0, len = continues.length; i < len; i++) {
|
|
682
|
+
const target = continues[i];
|
|
683
|
+
current = t5.blockStatement([
|
|
684
|
+
t5.ifStatement(
|
|
685
|
+
t5.binaryExpression("===", returnResult, t5.stringLiteral(target)),
|
|
686
|
+
t5.blockStatement([t5.continueStatement(t5.identifier(target))]),
|
|
687
|
+
current
|
|
688
|
+
)
|
|
689
|
+
]);
|
|
690
|
+
}
|
|
691
|
+
if (current) {
|
|
692
|
+
return t5.ifStatement(
|
|
693
|
+
t5.binaryExpression("===", returnType, CONTINUE_KEY),
|
|
694
|
+
current,
|
|
695
|
+
check
|
|
696
|
+
);
|
|
697
|
+
}
|
|
698
|
+
return check;
|
|
699
|
+
}
|
|
700
|
+
function getGeneratorReplacementForServerBlock(path2, registerID, args) {
|
|
701
|
+
const iterator = path2.scope.generateUidIdentifier("iterator");
|
|
702
|
+
const step = path2.scope.generateUidIdentifier("step");
|
|
703
|
+
const replacement = [
|
|
704
|
+
t5.variableDeclaration("let", [
|
|
705
|
+
t5.variableDeclarator(step),
|
|
706
|
+
// First, get the iterator by calling the generator
|
|
707
|
+
t5.variableDeclarator(
|
|
708
|
+
iterator,
|
|
709
|
+
t5.awaitExpression(t5.callExpression(registerID, args))
|
|
710
|
+
)
|
|
711
|
+
]),
|
|
712
|
+
// Create a while statement, the intent is to
|
|
713
|
+
// repeatedly iterate the generator
|
|
714
|
+
t5.whileStatement(
|
|
715
|
+
t5.booleanLiteral(true),
|
|
716
|
+
t5.blockStatement([
|
|
717
|
+
// Get the next value
|
|
718
|
+
t5.expressionStatement(
|
|
719
|
+
t5.assignmentExpression(
|
|
720
|
+
"=",
|
|
721
|
+
step,
|
|
722
|
+
t5.awaitExpression(
|
|
723
|
+
t5.callExpression(
|
|
724
|
+
t5.memberExpression(iterator, t5.identifier("next")),
|
|
725
|
+
[]
|
|
726
|
+
)
|
|
727
|
+
)
|
|
728
|
+
)
|
|
729
|
+
),
|
|
730
|
+
// Check if the step is done
|
|
731
|
+
t5.ifStatement(
|
|
732
|
+
t5.memberExpression(step, t5.identifier("done")),
|
|
733
|
+
t5.blockStatement([
|
|
734
|
+
// exit the loop
|
|
735
|
+
t5.breakStatement()
|
|
736
|
+
]),
|
|
737
|
+
// Otherwise, yield the value
|
|
738
|
+
t5.blockStatement([
|
|
739
|
+
t5.expressionStatement(
|
|
740
|
+
t5.yieldExpression(
|
|
741
|
+
t5.memberExpression(step, t5.identifier("value"))
|
|
742
|
+
)
|
|
743
|
+
)
|
|
744
|
+
])
|
|
745
|
+
)
|
|
746
|
+
])
|
|
747
|
+
)
|
|
748
|
+
];
|
|
749
|
+
return [replacement, step];
|
|
750
|
+
}
|
|
751
|
+
function getBlockReplacement(ctx, path2, entryFile, bindings, halting) {
|
|
752
|
+
const returnType = path2.scope.generateUidIdentifier("type");
|
|
753
|
+
const returnResult = path2.scope.generateUidIdentifier("result");
|
|
754
|
+
const returnMutations = path2.scope.generateUidIdentifier("mutations");
|
|
755
|
+
let check;
|
|
756
|
+
if (halting.hasReturn) {
|
|
757
|
+
check = t5.ifStatement(
|
|
758
|
+
t5.binaryExpression("===", returnType, RETURN_KEY),
|
|
759
|
+
t5.blockStatement([t5.returnStatement(returnResult)]),
|
|
760
|
+
check
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
if (halting.breakCount > 0) {
|
|
764
|
+
check = getBreakCheck(
|
|
765
|
+
returnType,
|
|
766
|
+
returnResult,
|
|
767
|
+
halting.breakCount,
|
|
768
|
+
halting.breaks,
|
|
769
|
+
check
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
if (halting.continueCount > 0) {
|
|
773
|
+
check = getContinueCheck(
|
|
774
|
+
returnType,
|
|
775
|
+
returnResult,
|
|
776
|
+
halting.continueCount,
|
|
777
|
+
halting.continues,
|
|
778
|
+
check
|
|
779
|
+
);
|
|
780
|
+
}
|
|
781
|
+
const replacement = [];
|
|
782
|
+
if (halting.hasYield) {
|
|
783
|
+
const blockID = path2.scope.generateUidIdentifier("block");
|
|
784
|
+
replacement.push(
|
|
785
|
+
t5.variableDeclaration("const", [
|
|
786
|
+
t5.variableDeclarator(
|
|
787
|
+
blockID,
|
|
788
|
+
t5.callExpression(getImportIdentifier(ctx, path2, HIDDEN_GENERATOR), [
|
|
789
|
+
t5.memberExpression(
|
|
790
|
+
t5.awaitExpression(t5.importExpression(t5.stringLiteral(entryFile))),
|
|
791
|
+
t5.identifier("default")
|
|
792
|
+
),
|
|
793
|
+
bindings.mutations.length ? t5.arrowFunctionExpression(
|
|
794
|
+
[returnMutations],
|
|
795
|
+
t5.assignmentExpression(
|
|
796
|
+
"=",
|
|
797
|
+
t5.objectPattern(
|
|
798
|
+
bindings.mutations.map(
|
|
799
|
+
(item) => t5.objectProperty(item, item, false, true)
|
|
800
|
+
)
|
|
801
|
+
),
|
|
802
|
+
returnMutations
|
|
803
|
+
)
|
|
804
|
+
) : t5.nullLiteral()
|
|
805
|
+
])
|
|
806
|
+
)
|
|
807
|
+
])
|
|
808
|
+
);
|
|
809
|
+
const [reps, step] = getGeneratorReplacementForServerBlock(
|
|
810
|
+
path2,
|
|
811
|
+
blockID,
|
|
812
|
+
bindings.locals
|
|
813
|
+
);
|
|
814
|
+
for (let i = 0, len = reps.length; i < len; i++) {
|
|
815
|
+
replacement.push(reps[i]);
|
|
816
|
+
}
|
|
817
|
+
replacement.push(
|
|
818
|
+
t5.variableDeclaration("const", [
|
|
819
|
+
t5.variableDeclarator(
|
|
820
|
+
t5.arrayPattern([returnType, returnResult]),
|
|
821
|
+
t5.memberExpression(step, t5.identifier("value"))
|
|
822
|
+
)
|
|
823
|
+
])
|
|
824
|
+
);
|
|
825
|
+
} else {
|
|
826
|
+
replacement.push(
|
|
827
|
+
t5.variableDeclaration("const", [
|
|
828
|
+
t5.variableDeclarator(
|
|
829
|
+
t5.arrayPattern([returnType, returnResult]),
|
|
830
|
+
t5.awaitExpression(
|
|
831
|
+
t5.callExpression(
|
|
832
|
+
t5.callExpression(getImportIdentifier(ctx, path2, HIDDEN_FUNC), [
|
|
833
|
+
t5.memberExpression(
|
|
834
|
+
t5.awaitExpression(
|
|
835
|
+
t5.importExpression(t5.stringLiteral(entryFile))
|
|
836
|
+
),
|
|
837
|
+
t5.identifier("default")
|
|
838
|
+
),
|
|
839
|
+
bindings.mutations.length ? t5.arrowFunctionExpression(
|
|
840
|
+
[returnMutations],
|
|
841
|
+
t5.assignmentExpression(
|
|
842
|
+
"=",
|
|
843
|
+
t5.objectPattern(
|
|
844
|
+
bindings.mutations.map(
|
|
845
|
+
(item) => t5.objectProperty(item, item, false, true)
|
|
846
|
+
)
|
|
847
|
+
),
|
|
848
|
+
returnMutations
|
|
849
|
+
)
|
|
850
|
+
) : t5.nullLiteral()
|
|
851
|
+
]),
|
|
852
|
+
bindings.locals
|
|
853
|
+
)
|
|
854
|
+
)
|
|
855
|
+
)
|
|
856
|
+
])
|
|
857
|
+
);
|
|
858
|
+
}
|
|
859
|
+
if (check) {
|
|
860
|
+
replacement.push(check);
|
|
861
|
+
}
|
|
862
|
+
return t5.blockStatement(replacement);
|
|
863
|
+
}
|
|
864
|
+
function replaceBlock(ctx, path2, directive, bindings) {
|
|
865
|
+
const halting = transformBlockContent(path2, bindings.mutations);
|
|
866
|
+
const rootFile = createRootFile(
|
|
867
|
+
ctx,
|
|
868
|
+
bindings,
|
|
869
|
+
t5.functionExpression(
|
|
870
|
+
void 0,
|
|
871
|
+
bindings.locals,
|
|
872
|
+
t5.blockStatement(path2.node.body),
|
|
873
|
+
halting.hasYield,
|
|
874
|
+
true
|
|
875
|
+
)
|
|
876
|
+
);
|
|
877
|
+
const entryFile = createEntryFile(ctx, path2, rootFile, directive.target);
|
|
878
|
+
path2.replaceWith(
|
|
879
|
+
getBlockReplacement(ctx, path2, entryFile, bindings, halting)
|
|
880
|
+
);
|
|
881
|
+
}
|
|
882
|
+
function splitBlock(ctx, path2, directive) {
|
|
883
|
+
replaceBlock(
|
|
884
|
+
ctx,
|
|
885
|
+
path2,
|
|
886
|
+
directive,
|
|
887
|
+
extractBindings(
|
|
888
|
+
ctx,
|
|
889
|
+
path2,
|
|
890
|
+
getForeignBindings(path2, "block"),
|
|
891
|
+
directive.pure
|
|
892
|
+
)
|
|
893
|
+
);
|
|
894
|
+
}
|
|
895
|
+
function transformFunctionContent(path2, mutations) {
|
|
896
|
+
const target = path2.scope.getFunctionParent() || path2.scope.getProgramParent();
|
|
897
|
+
const applyMutations = mutations.length ? path2.scope.generateUidIdentifier("mutate") : void 0;
|
|
898
|
+
let hasReturn = false;
|
|
899
|
+
let hasYield = false;
|
|
900
|
+
path2.traverse({
|
|
901
|
+
ReturnStatement(child) {
|
|
902
|
+
const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
|
|
903
|
+
if (parent === target) {
|
|
904
|
+
hasReturn = true;
|
|
905
|
+
const replacement = [RETURN_KEY];
|
|
906
|
+
if (child.node.argument) {
|
|
907
|
+
replacement.push(child.node.argument);
|
|
908
|
+
} else {
|
|
909
|
+
replacement.push(t5.nullLiteral());
|
|
910
|
+
}
|
|
911
|
+
if (applyMutations) {
|
|
912
|
+
replacement.push(t5.callExpression(applyMutations, []));
|
|
913
|
+
}
|
|
914
|
+
child.replaceWith(t5.returnStatement(t5.arrayExpression(replacement)));
|
|
915
|
+
child.skip();
|
|
916
|
+
}
|
|
917
|
+
},
|
|
918
|
+
YieldExpression(child) {
|
|
919
|
+
const parent = child.scope.getFunctionParent() || child.scope.getProgramParent();
|
|
920
|
+
if (parent === target) {
|
|
921
|
+
hasYield = true;
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
});
|
|
925
|
+
const error = path2.scope.generateUidIdentifier("error");
|
|
926
|
+
const throwResult = [THROW_KEY, error];
|
|
927
|
+
const haltResult = [NO_HALT_KEY];
|
|
928
|
+
if (applyMutations) {
|
|
929
|
+
throwResult.push(t5.callExpression(applyMutations, []));
|
|
930
|
+
haltResult.push(t5.nullLiteral());
|
|
931
|
+
haltResult.push(t5.callExpression(applyMutations, []));
|
|
932
|
+
}
|
|
933
|
+
const statements = [
|
|
934
|
+
t5.tryStatement(
|
|
935
|
+
t5.blockStatement(path2.node.body),
|
|
936
|
+
t5.catchClause(
|
|
937
|
+
error,
|
|
938
|
+
t5.blockStatement([t5.returnStatement(t5.arrayExpression(throwResult))])
|
|
939
|
+
)
|
|
940
|
+
),
|
|
941
|
+
t5.returnStatement(t5.arrayExpression(haltResult))
|
|
942
|
+
];
|
|
943
|
+
if (applyMutations) {
|
|
944
|
+
statements.unshift(
|
|
945
|
+
t5.variableDeclaration("const", [
|
|
946
|
+
t5.variableDeclarator(
|
|
947
|
+
applyMutations,
|
|
948
|
+
t5.arrowFunctionExpression(
|
|
949
|
+
[],
|
|
950
|
+
t5.objectExpression(
|
|
951
|
+
mutations.map((item) => t5.objectProperty(item, item, false, true))
|
|
952
|
+
)
|
|
953
|
+
)
|
|
954
|
+
)
|
|
955
|
+
])
|
|
956
|
+
);
|
|
957
|
+
}
|
|
958
|
+
path2.node.body = statements;
|
|
959
|
+
return {
|
|
960
|
+
hasReturn,
|
|
961
|
+
hasYield
|
|
962
|
+
};
|
|
963
|
+
}
|
|
964
|
+
function getFunctionReplacement(ctx, path2, entryFile, bindings, halting) {
|
|
965
|
+
const rest = path2.scope.generateUidIdentifier("rest");
|
|
966
|
+
const returnType = path2.scope.generateUidIdentifier("type");
|
|
967
|
+
const returnResult = path2.scope.generateUidIdentifier("result");
|
|
968
|
+
const returnMutations = path2.scope.generateUidIdentifier("mutations");
|
|
969
|
+
const replacement = [];
|
|
970
|
+
if (halting.hasYield) {
|
|
971
|
+
const funcID = path2.scope.generateUidIdentifier("fn");
|
|
972
|
+
replacement.push(
|
|
973
|
+
t5.variableDeclaration("const", [
|
|
974
|
+
t5.variableDeclarator(
|
|
975
|
+
funcID,
|
|
976
|
+
t5.callExpression(getImportIdentifier(ctx, path2, HIDDEN_GENERATOR), [
|
|
977
|
+
t5.memberExpression(
|
|
978
|
+
t5.awaitExpression(t5.importExpression(t5.stringLiteral(entryFile))),
|
|
979
|
+
t5.identifier("default")
|
|
980
|
+
),
|
|
981
|
+
bindings.mutations.length ? t5.arrowFunctionExpression(
|
|
982
|
+
[returnMutations],
|
|
983
|
+
t5.assignmentExpression(
|
|
984
|
+
"=",
|
|
985
|
+
t5.objectPattern(
|
|
986
|
+
bindings.mutations.map(
|
|
987
|
+
(item) => t5.objectProperty(item, item, false, true)
|
|
988
|
+
)
|
|
989
|
+
),
|
|
990
|
+
returnMutations
|
|
991
|
+
)
|
|
992
|
+
) : t5.nullLiteral()
|
|
993
|
+
])
|
|
994
|
+
)
|
|
995
|
+
])
|
|
996
|
+
);
|
|
997
|
+
const [reps, step] = getGeneratorReplacementForServerBlock(path2, funcID, [
|
|
998
|
+
t5.arrayExpression(bindings.locals),
|
|
999
|
+
t5.spreadElement(rest)
|
|
1000
|
+
]);
|
|
1001
|
+
for (let i = 0, len = reps.length; i < len; i++) {
|
|
1002
|
+
replacement.push(reps[i]);
|
|
1003
|
+
}
|
|
1004
|
+
replacement.push(
|
|
1005
|
+
t5.variableDeclaration("const", [
|
|
1006
|
+
t5.variableDeclarator(
|
|
1007
|
+
t5.arrayPattern([returnType, returnResult]),
|
|
1008
|
+
t5.memberExpression(step, t5.identifier("value"))
|
|
1009
|
+
)
|
|
1010
|
+
])
|
|
1011
|
+
);
|
|
1012
|
+
} else {
|
|
1013
|
+
replacement.push(
|
|
1014
|
+
t5.variableDeclaration("const", [
|
|
1015
|
+
t5.variableDeclarator(
|
|
1016
|
+
t5.arrayPattern([returnType, returnResult]),
|
|
1017
|
+
t5.awaitExpression(
|
|
1018
|
+
t5.callExpression(
|
|
1019
|
+
t5.callExpression(getImportIdentifier(ctx, path2, HIDDEN_FUNC), [
|
|
1020
|
+
t5.memberExpression(
|
|
1021
|
+
t5.awaitExpression(
|
|
1022
|
+
t5.importExpression(t5.stringLiteral(entryFile))
|
|
1023
|
+
),
|
|
1024
|
+
t5.identifier("default")
|
|
1025
|
+
),
|
|
1026
|
+
bindings.mutations.length ? t5.arrowFunctionExpression(
|
|
1027
|
+
[returnMutations],
|
|
1028
|
+
t5.assignmentExpression(
|
|
1029
|
+
"=",
|
|
1030
|
+
t5.objectPattern(
|
|
1031
|
+
bindings.mutations.map(
|
|
1032
|
+
(item) => t5.objectProperty(item, item, false, true)
|
|
1033
|
+
)
|
|
1034
|
+
),
|
|
1035
|
+
returnMutations
|
|
1036
|
+
)
|
|
1037
|
+
) : t5.nullLiteral()
|
|
1038
|
+
]),
|
|
1039
|
+
[t5.arrayExpression(bindings.locals), t5.spreadElement(rest)]
|
|
1040
|
+
)
|
|
1041
|
+
)
|
|
1042
|
+
)
|
|
1043
|
+
])
|
|
1044
|
+
);
|
|
1045
|
+
}
|
|
1046
|
+
replacement.push(t5.returnStatement(returnResult));
|
|
1047
|
+
if (isPathValid(path2, t5.isFunctionExpression)) {
|
|
1048
|
+
return t5.functionExpression(
|
|
1049
|
+
path2.node.id,
|
|
1050
|
+
[t5.restElement(rest)],
|
|
1051
|
+
t5.blockStatement(replacement),
|
|
1052
|
+
true,
|
|
1053
|
+
halting.hasYield
|
|
1054
|
+
);
|
|
1055
|
+
}
|
|
1056
|
+
return t5.arrowFunctionExpression(
|
|
1057
|
+
[t5.restElement(rest)],
|
|
1058
|
+
t5.blockStatement(replacement),
|
|
1059
|
+
true
|
|
1060
|
+
);
|
|
1061
|
+
}
|
|
1062
|
+
function replaceFunction(ctx, path2, func, bindings) {
|
|
1063
|
+
const body = path2.get("body");
|
|
1064
|
+
if (isPathValid(body, t5.isExpression)) {
|
|
1065
|
+
body.replaceWith(t5.blockStatement([t5.returnStatement(body.node)]));
|
|
1066
|
+
}
|
|
1067
|
+
assert(isPathValid(body, t5.isBlockStatement), "invariant");
|
|
1068
|
+
const halting = transformFunctionContent(body, bindings.mutations);
|
|
1069
|
+
const rootFile = createRootFile(
|
|
1070
|
+
ctx,
|
|
1071
|
+
bindings,
|
|
1072
|
+
t5.isFunctionExpression(path2.node) ? t5.functionExpression(
|
|
1073
|
+
path2.node.id,
|
|
1074
|
+
[t5.arrayPattern(bindings.locals), ...path2.node.params],
|
|
1075
|
+
path2.node.body,
|
|
1076
|
+
path2.node.async,
|
|
1077
|
+
path2.node.generator
|
|
1078
|
+
) : t5.arrowFunctionExpression(
|
|
1079
|
+
[t5.arrayPattern(bindings.locals), ...path2.node.params],
|
|
1080
|
+
path2.node.body,
|
|
1081
|
+
path2.node.async
|
|
1082
|
+
)
|
|
1083
|
+
);
|
|
1084
|
+
const entryFile = createEntryFile(ctx, path2, rootFile, func.target);
|
|
1085
|
+
return getFunctionReplacement(ctx, path2, entryFile, bindings, halting);
|
|
1086
|
+
}
|
|
1087
|
+
function splitFunction(ctx, path2, func) {
|
|
1088
|
+
return replaceFunction(
|
|
1089
|
+
ctx,
|
|
1090
|
+
path2,
|
|
1091
|
+
func,
|
|
1092
|
+
extractBindings(ctx, path2, getForeignBindings(path2, "function"), func.pure)
|
|
1093
|
+
);
|
|
1094
|
+
}
|
|
1095
|
+
function replaceExpression(ctx, path2, func, bindings) {
|
|
1096
|
+
const rootFile = createRootFile(ctx, bindings, path2.node);
|
|
1097
|
+
const entryFile = createEntryFile(ctx, path2, rootFile, func.target);
|
|
1098
|
+
const rest = path2.scope.generateUidIdentifier("rest");
|
|
1099
|
+
return t5.arrowFunctionExpression(
|
|
1100
|
+
[t5.restElement(rest)],
|
|
1101
|
+
t5.callExpression(
|
|
1102
|
+
t5.memberExpression(
|
|
1103
|
+
t5.awaitExpression(t5.importExpression(t5.stringLiteral(entryFile))),
|
|
1104
|
+
t5.identifier("default")
|
|
1105
|
+
),
|
|
1106
|
+
[t5.spreadElement(rest)]
|
|
1107
|
+
),
|
|
1108
|
+
true
|
|
1109
|
+
);
|
|
1110
|
+
}
|
|
1111
|
+
function splitExpression(ctx, path2, func) {
|
|
1112
|
+
return replaceExpression(
|
|
1113
|
+
ctx,
|
|
1114
|
+
path2,
|
|
1115
|
+
func,
|
|
1116
|
+
extractBindings(
|
|
1117
|
+
ctx,
|
|
1118
|
+
path2,
|
|
1119
|
+
getForeignBindings(path2, "expression"),
|
|
1120
|
+
func.pure
|
|
1121
|
+
)
|
|
1122
|
+
);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
// src/transform-block.ts
|
|
1126
|
+
function getValidDirectiveFromString(ctx, string) {
|
|
1127
|
+
for (let i = 0, len = ctx.options.directives.length; i < len; i++) {
|
|
1128
|
+
const current = ctx.options.directives[i];
|
|
1129
|
+
if (current.value === string) {
|
|
1130
|
+
return current;
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
return void 0;
|
|
1134
|
+
}
|
|
1135
|
+
function getDefinitionFromDirectives(ctx, path2) {
|
|
1136
|
+
for (let i = 0, len = path2.node.directives.length; i < len; i++) {
|
|
1137
|
+
const statement = path2.node.directives[i].value.value;
|
|
1138
|
+
const directive = getValidDirectiveFromString(ctx, statement);
|
|
1139
|
+
if (directive) {
|
|
1140
|
+
return directive;
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
return void 0;
|
|
1144
|
+
}
|
|
1145
|
+
function getDefinitionFromFauxDirectives(ctx, path2) {
|
|
1146
|
+
for (let i = 0, len = path2.node.body.length; i < len; i++) {
|
|
1147
|
+
const statement = path2.node.body[i];
|
|
1148
|
+
if (statement.type === "ExpressionStatement" && statement.expression.type === "StringLiteral") {
|
|
1149
|
+
const directive = getValidDirectiveFromString(
|
|
1150
|
+
ctx,
|
|
1151
|
+
statement.expression.value
|
|
1152
|
+
);
|
|
1153
|
+
if (directive) {
|
|
1154
|
+
return directive;
|
|
1155
|
+
}
|
|
1156
|
+
} else {
|
|
1157
|
+
break;
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
return void 0;
|
|
1161
|
+
}
|
|
1162
|
+
function getDirectiveDefinitionFromBlock(ctx, path2) {
|
|
1163
|
+
const parent = path2.getFunctionParent();
|
|
1164
|
+
if (parent && !parent.node.async) {
|
|
1165
|
+
return void 0;
|
|
1166
|
+
}
|
|
1167
|
+
return getDefinitionFromDirectives(ctx, path2) || getDefinitionFromFauxDirectives(ctx, path2);
|
|
1168
|
+
}
|
|
1169
|
+
function cleanBlockForDirectives(path2, definition) {
|
|
1170
|
+
const newDirectives = [];
|
|
1171
|
+
for (let i = 0, len = path2.node.directives.length; i < len; i++) {
|
|
1172
|
+
const current = path2.node.directives[i];
|
|
1173
|
+
if (current.value.value !== definition.value) {
|
|
1174
|
+
newDirectives.push(current);
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
path2.node.directives = newDirectives;
|
|
1178
|
+
}
|
|
1179
|
+
function cleanBlockForFauxDirectives(path2, definition) {
|
|
1180
|
+
const body = path2.get("body");
|
|
1181
|
+
for (let i = 0, len = body.length; i < len; i++) {
|
|
1182
|
+
const statement = body[i];
|
|
1183
|
+
if (statement.node.type === "ExpressionStatement" && statement.node.expression.type === "StringLiteral") {
|
|
1184
|
+
if (statement.node.expression.value === definition.value) {
|
|
1185
|
+
statement.remove();
|
|
1186
|
+
return;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
function transformBlock(ctx, path2) {
|
|
1192
|
+
const definition = getDirectiveDefinitionFromBlock(ctx, path2);
|
|
1193
|
+
if (!definition) {
|
|
1194
|
+
return;
|
|
1195
|
+
}
|
|
1196
|
+
cleanBlockForDirectives(path2, definition);
|
|
1197
|
+
cleanBlockForFauxDirectives(path2, definition);
|
|
1198
|
+
splitBlock(ctx, path2, definition);
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
// src/transform-call.ts
|
|
1202
|
+
var t6 = __toESM(require("@babel/types"), 1);
|
|
1203
|
+
function getFunctionDefinitionFromPropName(definitions, propName) {
|
|
1204
|
+
for (let i = 0, len = definitions.length; i < len; i++) {
|
|
1205
|
+
const def = definitions[i];
|
|
1206
|
+
if (def.source.kind === "default" && propName === "default") {
|
|
1207
|
+
return def;
|
|
1208
|
+
}
|
|
1209
|
+
if (def.source.kind === "named" && propName === def.source.name) {
|
|
1210
|
+
return def;
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
return void 0;
|
|
1214
|
+
}
|
|
1215
|
+
function getFunctionDefinitionFromCallee(ctx, path2) {
|
|
1216
|
+
const callee = path2.node.callee;
|
|
1217
|
+
const id = unwrapNode(callee, t6.isIdentifier);
|
|
1218
|
+
if (id) {
|
|
1219
|
+
const binding = path2.scope.getBindingIdentifier(id.name);
|
|
1220
|
+
if (binding) {
|
|
1221
|
+
return ctx.registrations.identifiers.get(binding);
|
|
1222
|
+
}
|
|
1223
|
+
return void 0;
|
|
1224
|
+
}
|
|
1225
|
+
const memberExpr = unwrapNode(callee, t6.isMemberExpression);
|
|
1226
|
+
if (memberExpr && !memberExpr.computed && t6.isIdentifier(memberExpr.property)) {
|
|
1227
|
+
const object = unwrapNode(memberExpr.object, t6.isIdentifier);
|
|
1228
|
+
if (object) {
|
|
1229
|
+
const binding = path2.scope.getBindingIdentifier(object.name);
|
|
1230
|
+
if (binding) {
|
|
1231
|
+
const definitions = ctx.registrations.namespaces.get(binding);
|
|
1232
|
+
if (definitions) {
|
|
1233
|
+
return getFunctionDefinitionFromPropName(
|
|
1234
|
+
definitions,
|
|
1235
|
+
memberExpr.property.name
|
|
1236
|
+
);
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
return void 0;
|
|
1242
|
+
}
|
|
1243
|
+
function isValidFunction(node) {
|
|
1244
|
+
return t6.isArrowFunctionExpression(node) || t6.isFunctionExpression(node);
|
|
1245
|
+
}
|
|
1246
|
+
function isSkippableFunction(node) {
|
|
1247
|
+
if (node.leadingComments) {
|
|
1248
|
+
for (let i = 0, len = node.leadingComments.length; i < len; i++) {
|
|
1249
|
+
if (/^@dismantle skip$/.test(node.leadingComments[i].value)) {
|
|
1250
|
+
return true;
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
return false;
|
|
1255
|
+
}
|
|
1256
|
+
function transformCall(ctx, path2) {
|
|
1257
|
+
const definition = getFunctionDefinitionFromCallee(ctx, path2);
|
|
1258
|
+
if (!definition) {
|
|
1259
|
+
return;
|
|
1260
|
+
}
|
|
1261
|
+
const args = path2.get("arguments");
|
|
1262
|
+
const expr = args[0];
|
|
1263
|
+
if (isPathValid(expr, t6.isExpression)) {
|
|
1264
|
+
if (isSkippableFunction(expr.node)) {
|
|
1265
|
+
return;
|
|
1266
|
+
}
|
|
1267
|
+
const replacement = isPathValid(expr, isValidFunction) ? splitFunction(ctx, expr, definition) : splitExpression(ctx, expr, definition);
|
|
1268
|
+
if (definition.preserve) {
|
|
1269
|
+
expr.replaceWith(t6.addComment(replacement, "leading", "@dismantle skip"));
|
|
1270
|
+
} else {
|
|
1271
|
+
path2.replaceWith(replacement);
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
// src/utils/register-import-specifiers.ts
|
|
1277
|
+
var t7 = __toESM(require("@babel/types"), 1);
|
|
1278
|
+
function registerImportSpecifier(ctx, node, definition) {
|
|
1279
|
+
if (t7.isImportSpecifier(node)) {
|
|
1280
|
+
if (node.importKind === "type" || node.importKind === "typeof") {
|
|
1281
|
+
return;
|
|
1282
|
+
}
|
|
1283
|
+
const key = getImportSpecifierName(node);
|
|
1284
|
+
if (definition.source.kind === "named" && key === definition.source.name || definition.source.kind === "default" && key === "default") {
|
|
1285
|
+
ctx.registrations.identifiers.set(node.local, definition);
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
if (t7.isImportDefaultSpecifier(node) && definition.source.kind === "default") {
|
|
1289
|
+
ctx.registrations.identifiers.set(node.local, definition);
|
|
1290
|
+
}
|
|
1291
|
+
if (t7.isImportNamespaceSpecifier(node)) {
|
|
1292
|
+
let current = ctx.registrations.namespaces.get(node.local);
|
|
1293
|
+
if (!current) {
|
|
1294
|
+
current = [];
|
|
1295
|
+
}
|
|
1296
|
+
current.push(definition);
|
|
1297
|
+
ctx.registrations.namespaces.set(node.local, current);
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
function registerImportDeclarationByDefinition(ctx, path2, definition) {
|
|
1301
|
+
for (let i = 0, len = path2.node.specifiers.length; i < len; i++) {
|
|
1302
|
+
const specifier = path2.node.specifiers[i];
|
|
1303
|
+
registerImportSpecifier(ctx, specifier, definition);
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
function registerImportSpecifiers(ctx, programPath) {
|
|
1307
|
+
const len = ctx.options.functions.length;
|
|
1308
|
+
if (!len) {
|
|
1309
|
+
return;
|
|
1310
|
+
}
|
|
1311
|
+
programPath.traverse({
|
|
1312
|
+
ImportDeclaration(path2) {
|
|
1313
|
+
if (path2.node.importKind === "type" || path2.node.importKind === "typeof") {
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
for (let i = 0; i < len; i++) {
|
|
1317
|
+
const func = ctx.options.functions[i];
|
|
1318
|
+
if (func.source.source === path2.node.source.value) {
|
|
1319
|
+
registerImportDeclarationByDefinition(ctx, path2, func);
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
});
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
// src/plugin.ts
|
|
1327
|
+
function plugin() {
|
|
1328
|
+
return {
|
|
1329
|
+
name: "dismantle",
|
|
1330
|
+
visitor: {
|
|
1331
|
+
Program(path2, ctx) {
|
|
1332
|
+
registerImportSpecifiers(ctx.opts, path2);
|
|
1333
|
+
},
|
|
1334
|
+
BlockStatement(path2, ctx) {
|
|
1335
|
+
transformBlock(ctx.opts, path2);
|
|
1336
|
+
},
|
|
1337
|
+
CallExpression(path2, ctx) {
|
|
1338
|
+
transformCall(ctx.opts, path2);
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
};
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
// src/utils/xxhash32.ts
|
|
1345
|
+
var PRIME32_1 = 2654435761;
|
|
1346
|
+
var PRIME32_2 = 2246822519;
|
|
1347
|
+
var PRIME32_3 = 3266489917;
|
|
1348
|
+
var PRIME32_4 = 668265263;
|
|
1349
|
+
var PRIME32_5 = 374761393;
|
|
1350
|
+
function toUtf8(text) {
|
|
1351
|
+
const bytes = [];
|
|
1352
|
+
for (let i = 0, n = text.length; i < n; ++i) {
|
|
1353
|
+
const c = text.charCodeAt(i);
|
|
1354
|
+
if (c < 128) {
|
|
1355
|
+
bytes.push(c);
|
|
1356
|
+
} else if (c < 2048) {
|
|
1357
|
+
bytes.push(192 | c >> 6, 128 | c & 63);
|
|
1358
|
+
} else if (c < 55296 || c >= 57344) {
|
|
1359
|
+
bytes.push(224 | c >> 12, 128 | c >> 6 & 63, 128 | c & 63);
|
|
1360
|
+
} else {
|
|
1361
|
+
const cp = 65536 + ((c & 1023) << 10 | text.charCodeAt(++i) & 1023);
|
|
1362
|
+
bytes.push(
|
|
1363
|
+
240 | cp >> 18 & 7,
|
|
1364
|
+
128 | cp >> 12 & 63,
|
|
1365
|
+
128 | cp >> 6 & 63,
|
|
1366
|
+
128 | cp & 63
|
|
1367
|
+
);
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
return new Uint8Array(bytes);
|
|
1371
|
+
}
|
|
1372
|
+
function xxHash32(buffer, seed = 0) {
|
|
1373
|
+
buffer = typeof buffer === "string" ? toUtf8(buffer) : buffer;
|
|
1374
|
+
const b = buffer;
|
|
1375
|
+
let acc = seed + PRIME32_5 & 4294967295;
|
|
1376
|
+
let offset = 0;
|
|
1377
|
+
if (b.length >= 16) {
|
|
1378
|
+
const accN = [
|
|
1379
|
+
seed + PRIME32_1 + PRIME32_2 & 4294967295,
|
|
1380
|
+
seed + PRIME32_2 & 4294967295,
|
|
1381
|
+
seed + 0 & 4294967295,
|
|
1382
|
+
seed - PRIME32_1 & 4294967295
|
|
1383
|
+
];
|
|
1384
|
+
const b2 = buffer;
|
|
1385
|
+
const limit2 = b2.length - 16;
|
|
1386
|
+
let lane = 0;
|
|
1387
|
+
for (offset = 0; (offset & 4294967280) <= limit2; offset += 4) {
|
|
1388
|
+
const i = offset;
|
|
1389
|
+
const laneN0 = b2[i + 0] + (b2[i + 1] << 8);
|
|
1390
|
+
const laneN1 = b2[i + 2] + (b2[i + 3] << 8);
|
|
1391
|
+
const laneNP = laneN0 * PRIME32_2 + (laneN1 * PRIME32_2 << 16);
|
|
1392
|
+
let acc2 = accN[lane] + laneNP & 4294967295;
|
|
1393
|
+
acc2 = acc2 << 13 | acc2 >>> 19;
|
|
1394
|
+
const acc0 = acc2 & 65535;
|
|
1395
|
+
const acc1 = acc2 >>> 16;
|
|
1396
|
+
accN[lane] = acc0 * PRIME32_1 + (acc1 * PRIME32_1 << 16) & 4294967295;
|
|
1397
|
+
lane = lane + 1 & 3;
|
|
1398
|
+
}
|
|
1399
|
+
acc = (accN[0] << 1 | accN[0] >>> 31) + (accN[1] << 7 | accN[1] >>> 25) + (accN[2] << 12 | accN[2] >>> 20) + (accN[3] << 18 | accN[3] >>> 14) & 4294967295;
|
|
1400
|
+
}
|
|
1401
|
+
acc = acc + buffer.length & 4294967295;
|
|
1402
|
+
const limit = buffer.length - 4;
|
|
1403
|
+
for (; offset <= limit; offset += 4) {
|
|
1404
|
+
const i = offset;
|
|
1405
|
+
const laneN0 = b[i + 0] + (b[i + 1] << 8);
|
|
1406
|
+
const laneN1 = b[i + 2] + (b[i + 3] << 8);
|
|
1407
|
+
const laneP = laneN0 * PRIME32_3 + (laneN1 * PRIME32_3 << 16);
|
|
1408
|
+
acc = acc + laneP & 4294967295;
|
|
1409
|
+
acc = acc << 17 | acc >>> 15;
|
|
1410
|
+
acc = (acc & 65535) * PRIME32_4 + ((acc >>> 16) * PRIME32_4 << 16) & 4294967295;
|
|
1411
|
+
}
|
|
1412
|
+
for (; offset < b.length; ++offset) {
|
|
1413
|
+
const lane = b[offset];
|
|
1414
|
+
acc += lane * PRIME32_5;
|
|
1415
|
+
acc = acc << 11 | acc >>> 21;
|
|
1416
|
+
acc = (acc & 65535) * PRIME32_1 + ((acc >>> 16) * PRIME32_1 << 16) & 4294967295;
|
|
1417
|
+
}
|
|
1418
|
+
acc ^= acc >>> 15;
|
|
1419
|
+
acc = ((acc & 65535) * PRIME32_2 & 4294967295) + ((acc >>> 16) * PRIME32_2 << 16);
|
|
1420
|
+
acc ^= acc >>> 13;
|
|
1421
|
+
acc = ((acc & 65535) * PRIME32_3 & 4294967295) + ((acc >>> 16) * PRIME32_3 << 16);
|
|
1422
|
+
acc ^= acc >>> 16;
|
|
1423
|
+
return acc < 0 ? acc + 4294967296 : acc;
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
// src/index.ts
|
|
1427
|
+
async function compile(id, code, options) {
|
|
1428
|
+
const parsedPath = import_node_path.default.parse(id);
|
|
1429
|
+
const entries = [];
|
|
1430
|
+
const roots = [];
|
|
1431
|
+
const files = /* @__PURE__ */ new Map();
|
|
1432
|
+
const ctx = {
|
|
1433
|
+
id,
|
|
1434
|
+
path: parsedPath,
|
|
1435
|
+
imports: /* @__PURE__ */ new Map(),
|
|
1436
|
+
virtual: {
|
|
1437
|
+
count: 0
|
|
1438
|
+
},
|
|
1439
|
+
options,
|
|
1440
|
+
bindings: /* @__PURE__ */ new Map(),
|
|
1441
|
+
blocks: {
|
|
1442
|
+
hash: xxHash32(id).toString(16),
|
|
1443
|
+
count: 0
|
|
1444
|
+
},
|
|
1445
|
+
onVirtualFile(current, content, mode) {
|
|
1446
|
+
const filePath = import_node_path.default.join(parsedPath.dir, current);
|
|
1447
|
+
files.set(import_node_path.default.join(parsedPath.dir, current), content);
|
|
1448
|
+
if (mode === "entry") {
|
|
1449
|
+
entries.push(filePath);
|
|
1450
|
+
} else if (mode === "root") {
|
|
1451
|
+
roots.push(filePath);
|
|
1452
|
+
}
|
|
1453
|
+
},
|
|
1454
|
+
registrations: {
|
|
1455
|
+
identifiers: /* @__PURE__ */ new Map(),
|
|
1456
|
+
namespaces: /* @__PURE__ */ new Map()
|
|
1457
|
+
}
|
|
1458
|
+
};
|
|
1459
|
+
const plugins = ["jsx"];
|
|
1460
|
+
if (/\.[mc]?tsx?$/i.test(id)) {
|
|
1461
|
+
plugins.push("typescript");
|
|
1462
|
+
}
|
|
1463
|
+
const result = await babel.transformAsync(code, {
|
|
1464
|
+
plugins: [[plugin, ctx]],
|
|
1465
|
+
parserOpts: {
|
|
1466
|
+
plugins
|
|
1467
|
+
},
|
|
1468
|
+
filename: parsedPath.base,
|
|
1469
|
+
ast: false,
|
|
1470
|
+
sourceFileName: id,
|
|
1471
|
+
sourceMaps: true,
|
|
1472
|
+
configFile: false,
|
|
1473
|
+
babelrc: false
|
|
1474
|
+
});
|
|
1475
|
+
assert(result, "invariant");
|
|
1476
|
+
return {
|
|
1477
|
+
code: result.code,
|
|
1478
|
+
map: result.map,
|
|
1479
|
+
files,
|
|
1480
|
+
entries,
|
|
1481
|
+
roots
|
|
1482
|
+
};
|
|
1483
|
+
}
|
|
1484
|
+
//# sourceMappingURL=index.cjs.map
|