-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.js
294 lines (248 loc) 路 10.2 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
/* eslint max-len: 0 */
import hoistVariables from "babel-helper-hoist-variables";
import template from "babel-template";
let buildTemplate = template(`
SYSTEM_REGISTER(MODULE_NAME, [SOURCES], function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
"use strict";
BEFORE_BODY;
return {
setters: [SETTERS],
execute: function () {
BODY;
}
};
});
`);
let buildExportAll = template(`
for (var KEY in TARGET) {
if (KEY !== "default" && KEY !== "__esModule") EXPORT_OBJ[KEY] = TARGET[KEY];
}
`);
export default function ({ types: t }) {
let IGNORE_REASSIGNMENT_SYMBOL = Symbol();
let reassignmentVisitor = {
"AssignmentExpression|UpdateExpression"(path) {
if (path.node[IGNORE_REASSIGNMENT_SYMBOL]) return;
path.node[IGNORE_REASSIGNMENT_SYMBOL] = true;
let arg = path.get(path.isAssignmentExpression() ? "left" : "argument");
if (!arg.isIdentifier()) return;
let name = arg.node.name;
// redeclared in this scope
if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
let exportedNames = this.exports[name];
if (!exportedNames) return;
let node = path.node;
// if it is a non-prefix update expression (x++ etc)
// then we must replace with the expression (_export('x', x + 1), x++)
// in order to ensure the same update expression value
let isPostUpdateExpression = path.isUpdateExpression() && !node.prefix;
if (isPostUpdateExpression) {
if (node.operator === "++")
node = t.binaryExpression("+", node.argument, t.numericLiteral(1));
else if (node.operator === "--")
node = t.binaryExpression("-", node.argument, t.numericLiteral(1));
else
isPostUpdateExpression = false;
}
for (let exportedName of exportedNames) {
node = this.buildCall(exportedName, node).expression;
}
if (isPostUpdateExpression)
node = t.sequenceExpression([node, path.node]);
path.replaceWith(node);
}
};
return {
visitor: {
ReferencedIdentifier(path, state) {
if (path.node.name == "__moduleName" && !path.scope.hasBinding("__moduleName")) {
path.replaceWith(t.memberExpression(state.contextIdent, t.identifier("id")));
}
},
Program: {
enter(path, state) {
state.contextIdent = path.scope.generateUidIdentifier("context");
},
exit(path, state) {
let exportIdent = path.scope.generateUidIdentifier("export");
let contextIdent = state.contextIdent;
let exportNames = Object.create(null);
let modules = Object.create(null);
let beforeBody = [];
let setters = [];
let sources = [];
let variableIds = [];
let removedPaths = [];
function addExportName(key, val) {
exportNames[key] = exportNames[key] || [];
exportNames[key].push(val);
}
function pushModule(source, key, specifiers) {
let _modules = modules[source] = modules[source] || { imports: [], exports: [] };
_modules[key] = _modules[key].concat(specifiers);
}
function buildExportCall(name, val) {
return t.expressionStatement(
t.callExpression(exportIdent, [t.stringLiteral(name), val])
);
}
let body: Array<Object> = path.get("body");
let canHoist = true;
for (let path of body) {
if (path.isExportDeclaration()) path = path.get("declaration");
if (path.isVariableDeclaration() && path.node.kind !== "var") {
canHoist = false;
break;
}
}
for (let path of body) {
if (canHoist && path.isFunctionDeclaration()) {
beforeBody.push(path.node);
removedPaths.push(path);
} else if (path.isImportDeclaration()) {
let source = path.node.source.value;
pushModule(source, "imports", path.node.specifiers);
for (let name in path.getBindingIdentifiers()) {
path.scope.removeBinding(name);
variableIds.push(t.identifier(name));
}
path.remove();
} else if (path.isExportAllDeclaration()) {
pushModule(path.node.source.value, "exports", path.node);
path.remove();
} else if (path.isExportDefaultDeclaration()) {
let declar = path.get("declaration");
if (declar.isClassDeclaration() || declar.isFunctionDeclaration()) {
let id = declar.node.id;
let nodes = [];
if (id) {
nodes.push(declar.node);
nodes.push(buildExportCall("default", id));
addExportName(id.name, "default");
} else {
nodes.push(buildExportCall("default", t.toExpression(declar.node)));
}
if (!canHoist || declar.isClassDeclaration()) {
path.replaceWithMultiple(nodes);
} else {
beforeBody = beforeBody.concat(nodes);
removedPaths.push(path);
}
} else {
path.replaceWith(buildExportCall("default", declar.node));
}
} else if (path.isExportNamedDeclaration()) {
let declar = path.get("declaration");
if (declar.node) {
path.replaceWith(declar);
let nodes = [];
let bindingIdentifiers;
if (path.isFunction()) {
let node = declar.node;
let name = node.id.name;
if (canHoist) {
addExportName(name, name);
beforeBody.push(node);
beforeBody.push(buildExportCall(name, node.id));
removedPaths.push(path);
} else {
bindingIdentifiers = { [name]: node.id };
}
} else {
bindingIdentifiers = declar.getBindingIdentifiers();
}
for (let name in bindingIdentifiers) {
addExportName(name, name);
nodes.push(buildExportCall(name, t.identifier(name)));
}
path.insertAfter(nodes);
} else {
let specifiers = path.node.specifiers;
if (specifiers && specifiers.length) {
if (path.node.source) {
pushModule(path.node.source.value, "exports", specifiers);
path.remove();
} else {
let nodes = [];
for (let specifier of specifiers) {
nodes.push(buildExportCall(specifier.exported.name, specifier.local));
addExportName(specifier.local.name, specifier.exported.name);
}
path.replaceWithMultiple(nodes);
}
}
}
}
}
for (let source in modules) {
let specifiers = modules[source];
let setterBody = [];
let target = path.scope.generateUidIdentifier(source);
for (let specifier of specifiers.imports) {
if (t.isImportNamespaceSpecifier(specifier)) {
setterBody.push(t.expressionStatement(t.assignmentExpression("=", specifier.local, target)));
} else if (t.isImportDefaultSpecifier(specifier)) {
specifier = t.importSpecifier(specifier.local, t.identifier("default"));
}
if (t.isImportSpecifier(specifier)) {
setterBody.push(t.expressionStatement(t.assignmentExpression("=", specifier.local, t.memberExpression(target, specifier.imported))));
}
}
if (specifiers.exports.length) {
let exportObjRef = path.scope.generateUidIdentifier("exportObj");
setterBody.push(t.variableDeclaration("var", [
t.variableDeclarator(exportObjRef, t.objectExpression([]))
]));
for (let node of specifiers.exports) {
if (t.isExportAllDeclaration(node)) {
setterBody.push(buildExportAll({
KEY: path.scope.generateUidIdentifier("key"),
EXPORT_OBJ: exportObjRef,
TARGET: target
}));
} else if (t.isExportSpecifier(node)) {
setterBody.push(t.expressionStatement(
t.assignmentExpression("=", t.memberExpression(exportObjRef, node.exported), t.memberExpression(target, node.local))
));
} else {
// todo
}
}
setterBody.push(t.expressionStatement(t.callExpression(exportIdent, [exportObjRef])));
}
sources.push(t.stringLiteral(source));
setters.push(t.functionExpression(null, [target], t.blockStatement(setterBody)));
}
let moduleName = this.getModuleName();
if (moduleName) moduleName = t.stringLiteral(moduleName);
if (canHoist) {
hoistVariables(path, (id) => variableIds.push(id));
}
if (variableIds.length) {
beforeBody.unshift(t.variableDeclaration("var", variableIds.map((id) => t.variableDeclarator(id))));
}
path.traverse(reassignmentVisitor, {
exports: exportNames,
buildCall: buildExportCall,
scope: path.scope
});
for (let path of removedPaths) {
path.remove();
}
path.node.body = [
buildTemplate({
SYSTEM_REGISTER: t.memberExpression(t.identifier(state.opts.systemGlobal || "System"), t.identifier("register")),
BEFORE_BODY: beforeBody,
MODULE_NAME: moduleName,
SETTERS: setters,
SOURCES: sources,
BODY: path.node.body,
EXPORT_IDENTIFIER: exportIdent,
CONTEXT_IDENTIFIER: contextIdent
})
];
}
}
}
};
}