-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.js
368 lines (326 loc) · 9.73 KB
/
index.js
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
import assert from "assert";
import * as t from "@babel/types";
import template from "@babel/template";
import chunk from "lodash/chunk";
import { isModule } from "@babel/helper-module-imports";
import rewriteThis from "./rewrite-this";
import rewriteLiveReferences from "./rewrite-live-references";
import normalizeAndLoadModuleMetadata, {
hasExports,
isSideEffectImport,
} from "./normalize-and-load-metadata";
export { hasExports, isSideEffectImport, isModule };
/**
* Perform all of the generic ES6 module rewriting needed to handle initial
* module processing. This function will rewrite the majority of the given
* program to reference the modules described by the returned metadata,
* and returns a list of statements for use when initializing the module.
*/
export function rewriteModuleStatementsAndPrepareHeader(
path: NodePath,
{
exportName,
strict,
allowTopLevelThis,
strictMode,
loose,
noInterop,
lazy,
esNamespaceOnly,
},
) {
assert(isModule(path), "Cannot process module statements in a script");
path.node.sourceType = "script";
const meta = normalizeAndLoadModuleMetadata(path, exportName, {
noInterop,
loose,
lazy,
esNamespaceOnly,
});
if (!allowTopLevelThis) {
rewriteThis(path);
}
rewriteLiveReferences(path, meta);
if (strictMode !== false) {
const hasStrict = path.node.directives.some(directive => {
return directive.value.value === "use strict";
});
if (!hasStrict) {
path.unshiftContainer(
"directives",
t.directive(t.directiveLiteral("use strict")),
);
}
}
const headers = [];
if (hasExports(meta) && !strict) {
headers.push(buildESModuleHeader(meta, loose /* enumerable */));
}
const nameList = buildExportNameListDeclaration(path, meta);
if (nameList) {
meta.exportNameListName = nameList.name;
headers.push(nameList.statement);
}
// Create all of the statically known named exports.
headers.push(...buildExportInitializationStatements(path, meta, loose));
return { meta, headers };
}
/**
* Flag a set of statements as hoisted above all else so that module init
* statements all run before user code.
*/
export function ensureStatementsHoisted(statements) {
// Force all of the header fields to be at the top of the file.
statements.forEach(header => {
header._blockHoist = 3;
});
}
/**
* Given an expression for a standard import object, like "require('foo')",
* wrap it in a call to the interop helpers based on the type.
*/
export function wrapInterop(
programPath: NodePath,
expr: Node,
type: InteropType,
): Node {
if (type === "none") {
return null;
}
let helper;
if (type === "default") {
helper = "interopRequireDefault";
} else if (type === "namespace") {
helper = "interopRequireWildcard";
} else {
throw new Error(`Unknown interop: ${type}`);
}
return t.callExpression(programPath.hub.addHelper(helper), [expr]);
}
/**
* Create the runtime initialization statements for a given requested source.
* These will initialize all of the runtime import/export logic that
* can't be handled statically by the statements created by
* buildExportInitializationStatements().
*/
export function buildNamespaceInitStatements(
metadata: ModuleMetadata,
sourceMetadata: SourceModuleMetadata,
loose: boolean = false,
) {
const statements = [];
let srcNamespace = t.identifier(sourceMetadata.name);
if (sourceMetadata.lazy) srcNamespace = t.callExpression(srcNamespace, []);
for (const localName of sourceMetadata.importsNamespace) {
if (localName === sourceMetadata.name) continue;
// Create and assign binding to namespace object
statements.push(
template.statement`var NAME = SOURCE;`({
NAME: localName,
SOURCE: t.cloneNode(srcNamespace),
}),
);
}
if (loose) {
statements.push(...buildReexportsFromMeta(metadata, sourceMetadata, loose));
}
for (const exportName of sourceMetadata.reexportNamespace) {
// Assign export to namespace object.
statements.push(
(sourceMetadata.lazy
? template.statement`
Object.defineProperty(EXPORTS, "NAME", {
enumerable: true,
get: function() {
return NAMESPACE;
}
});
`
: template.statement`EXPORTS.NAME = NAMESPACE;`)({
EXPORTS: metadata.exportName,
NAME: exportName,
NAMESPACE: t.cloneNode(srcNamespace),
}),
);
}
if (sourceMetadata.reexportAll) {
const statement = buildNamespaceReexport(
metadata,
t.cloneNode(srcNamespace),
loose,
);
statement.loc = sourceMetadata.reexportAll.loc;
// Iterate props creating getter for each prop.
statements.push(statement);
}
return statements;
}
const getTemplateForReexport = loose => {
return loose
? template.statement`EXPORTS.EXPORT_NAME = NAMESPACE.IMPORT_NAME;`
: template`
Object.defineProperty(EXPORTS, "EXPORT_NAME", {
enumerable: true,
get: function() {
return NAMESPACE.IMPORT_NAME;
},
});
`;
};
const buildReexportsFromMeta = (meta, metadata, loose) => {
const namespace = metadata.lazy
? t.callExpression(t.identifier(metadata.name), [])
: t.identifier(metadata.name);
const templateForCurrentMode = getTemplateForReexport(loose);
return Array.from(metadata.reexports, ([exportName, importName]) =>
templateForCurrentMode({
EXPORTS: meta.exportName,
EXPORT_NAME: exportName,
NAMESPACE: t.cloneNode(namespace),
IMPORT_NAME: importName,
}),
);
};
/**
* Build an "__esModule" header statement setting the property on a given object.
*/
function buildESModuleHeader(
metadata: ModuleMetadata,
enumerable: boolean = false,
) {
return (enumerable
? template.statement`
EXPORTS.__esModule = true;
`
: template.statement`
Object.defineProperty(EXPORTS, "__esModule", {
value: true,
});
`)({ EXPORTS: metadata.exportName });
}
/**
* Create a re-export initialization loop for a specific imported namespace.
*/
function buildNamespaceReexport(metadata, namespace, loose) {
return (loose
? template.statement`
Object.keys(NAMESPACE).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
VERIFY_NAME_LIST;
EXPORTS[key] = NAMESPACE[key];
});
`
: template.statement`
Object.keys(NAMESPACE).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
VERIFY_NAME_LIST;
Object.defineProperty(EXPORTS, key, {
enumerable: true,
get: function() {
return NAMESPACE[key];
},
});
});
`)({
NAMESPACE: namespace,
EXPORTS: metadata.exportName,
VERIFY_NAME_LIST: metadata.exportNameListName
? template`
if (Object.prototype.hasOwnProperty.call(EXPORTS_LIST, key)) return;
`({ EXPORTS_LIST: metadata.exportNameListName })
: null,
});
}
/**
* Build a statement declaring a variable that contains all of the exported
* variable names in an object so they can easily be referenced from an
* export * from statement to check for conflicts.
*/
function buildExportNameListDeclaration(
programPath: NodePath,
metadata: ModuleMetadata,
) {
const exportedVars = Object.create(null);
for (const data of metadata.local.values()) {
for (const name of data.names) {
exportedVars[name] = true;
}
}
let hasReexport = false;
for (const data of metadata.source.values()) {
for (const exportName of data.reexports.keys()) {
exportedVars[exportName] = true;
}
for (const exportName of data.reexportNamespace) {
exportedVars[exportName] = true;
}
hasReexport = hasReexport || data.reexportAll;
}
if (!hasReexport || Object.keys(exportedVars).length === 0) return null;
const name = programPath.scope.generateUidIdentifier("exportNames");
delete exportedVars.default;
return {
name: name.name,
statement: t.variableDeclaration("var", [
t.variableDeclarator(name, t.valueToNode(exportedVars)),
]),
};
}
/**
* Create a set of statements that will initialize all of the statically-known
* export names with their expected values.
*/
function buildExportInitializationStatements(
programPath: NodePath,
metadata: ModuleMetadata,
loose: boolean = false,
) {
const initStatements = [];
const exportNames = [];
for (const [localName, data] of metadata.local) {
if (data.kind === "import") {
// No-open since these are explicitly set with the "reexports" block.
} else if (data.kind === "hoisted") {
initStatements.push(
buildInitStatement(metadata, data.names, t.identifier(localName)),
);
} else {
exportNames.push(...data.names);
}
}
for (const data of metadata.source.values()) {
if (!loose) {
initStatements.push(...buildReexportsFromMeta(metadata, data, loose));
}
for (const exportName of data.reexportNamespace) {
exportNames.push(exportName);
}
}
initStatements.push(
...chunk(exportNames, 100).map(members => {
return buildInitStatement(
metadata,
members,
programPath.scope.buildUndefinedNode(),
);
}),
);
return initStatements;
}
/**
* Given a set of export names, create a set of nested assignments to
* initialize them all to a given expression.
*/
function buildInitStatement(metadata, exportNames, initExpr) {
return t.expressionStatement(
exportNames.reduce(
(acc, exportName) =>
template.expression`EXPORTS.NAME = VALUE`({
EXPORTS: metadata.exportName,
NAME: exportName,
VALUE: acc,
}),
initExpr,
),
);
}