index.js
14.6 KB
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
"use strict";
exports.__esModule = true;
var _create = require("babel-runtime/core-js/object/create");
var _create2 = _interopRequireDefault(_create);
var _getIterator2 = require("babel-runtime/core-js/get-iterator");
var _getIterator3 = _interopRequireDefault(_getIterator2);
var _symbol = require("babel-runtime/core-js/symbol");
var _symbol2 = _interopRequireDefault(_symbol);
exports.default = function (_ref) {
var t = _ref.types;
var IGNORE_REASSIGNMENT_SYMBOL = (0, _symbol2.default)();
var reassignmentVisitor = {
"AssignmentExpression|UpdateExpression": function AssignmentExpressionUpdateExpression(path) {
if (path.node[IGNORE_REASSIGNMENT_SYMBOL]) return;
path.node[IGNORE_REASSIGNMENT_SYMBOL] = true;
var arg = path.get(path.isAssignmentExpression() ? "left" : "argument");
if (!arg.isIdentifier()) return;
var name = arg.node.name;
if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
var exportedNames = this.exports[name];
if (!exportedNames) return;
var node = path.node;
var 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 (var _iterator = exportedNames, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
var _ref2;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref2 = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref2 = _i.value;
}
var exportedName = _ref2;
node = this.buildCall(exportedName, node).expression;
}
if (isPostUpdateExpression) node = t.sequenceExpression([node, path.node]);
path.replaceWith(node);
}
};
return {
visitor: {
CallExpression: function CallExpression(path, state) {
if (path.node.callee.type === TYPE_IMPORT) {
var contextIdent = state.contextIdent;
path.replaceWith(t.callExpression(t.memberExpression(contextIdent, t.identifier("import")), path.node.arguments));
}
},
ReferencedIdentifier: function ReferencedIdentifier(path, state) {
if (path.node.name == "__moduleName" && !path.scope.hasBinding("__moduleName")) {
path.replaceWith(t.memberExpression(state.contextIdent, t.identifier("id")));
}
},
Program: {
enter: function enter(path, state) {
state.contextIdent = path.scope.generateUidIdentifier("context");
},
exit: function exit(path, state) {
var exportIdent = path.scope.generateUidIdentifier("export");
var contextIdent = state.contextIdent;
var exportNames = (0, _create2.default)(null);
var modules = [];
var beforeBody = [];
var setters = [];
var sources = [];
var variableIds = [];
var removedPaths = [];
function addExportName(key, val) {
exportNames[key] = exportNames[key] || [];
exportNames[key].push(val);
}
function pushModule(source, key, specifiers) {
var module = void 0;
modules.forEach(function (m) {
if (m.key === source) {
module = m;
}
});
if (!module) {
modules.push(module = { key: source, imports: [], exports: [] });
}
module[key] = module[key].concat(specifiers);
}
function buildExportCall(name, val) {
return t.expressionStatement(t.callExpression(exportIdent, [t.stringLiteral(name), val]));
}
var body = path.get("body");
var canHoist = true;
for (var _iterator2 = body, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
var _ref3;
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref3 = _iterator2[_i2++];
} else {
_i2 = _iterator2.next();
if (_i2.done) break;
_ref3 = _i2.value;
}
var _path = _ref3;
if (_path.isExportDeclaration()) _path = _path.get("declaration");
if (_path.isVariableDeclaration() && _path.node.kind !== "var") {
canHoist = false;
break;
}
}
for (var _iterator3 = body, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
var _ref4;
if (_isArray3) {
if (_i3 >= _iterator3.length) break;
_ref4 = _iterator3[_i3++];
} else {
_i3 = _iterator3.next();
if (_i3.done) break;
_ref4 = _i3.value;
}
var _path2 = _ref4;
if (canHoist && _path2.isFunctionDeclaration()) {
beforeBody.push(_path2.node);
removedPaths.push(_path2);
} else if (_path2.isImportDeclaration()) {
var source = _path2.node.source.value;
pushModule(source, "imports", _path2.node.specifiers);
for (var name in _path2.getBindingIdentifiers()) {
_path2.scope.removeBinding(name);
variableIds.push(t.identifier(name));
}
_path2.remove();
} else if (_path2.isExportAllDeclaration()) {
pushModule(_path2.node.source.value, "exports", _path2.node);
_path2.remove();
} else if (_path2.isExportDefaultDeclaration()) {
var declar = _path2.get("declaration");
if (declar.isClassDeclaration() || declar.isFunctionDeclaration()) {
var id = declar.node.id;
var 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()) {
_path2.replaceWithMultiple(nodes);
} else {
beforeBody = beforeBody.concat(nodes);
removedPaths.push(_path2);
}
} else {
_path2.replaceWith(buildExportCall("default", declar.node));
}
} else if (_path2.isExportNamedDeclaration()) {
var _declar = _path2.get("declaration");
if (_declar.node) {
_path2.replaceWith(_declar);
var _nodes = [];
var bindingIdentifiers = void 0;
if (_path2.isFunction()) {
var node = _declar.node;
var _name = node.id.name;
if (canHoist) {
addExportName(_name, _name);
beforeBody.push(node);
beforeBody.push(buildExportCall(_name, node.id));
removedPaths.push(_path2);
} else {
var _bindingIdentifiers;
bindingIdentifiers = (_bindingIdentifiers = {}, _bindingIdentifiers[_name] = node.id, _bindingIdentifiers);
}
} else {
bindingIdentifiers = _declar.getBindingIdentifiers();
}
for (var _name2 in bindingIdentifiers) {
addExportName(_name2, _name2);
_nodes.push(buildExportCall(_name2, t.identifier(_name2)));
}
_path2.insertAfter(_nodes);
} else {
var specifiers = _path2.node.specifiers;
if (specifiers && specifiers.length) {
if (_path2.node.source) {
pushModule(_path2.node.source.value, "exports", specifiers);
_path2.remove();
} else {
var _nodes2 = [];
for (var _iterator7 = specifiers, _isArray7 = Array.isArray(_iterator7), _i7 = 0, _iterator7 = _isArray7 ? _iterator7 : (0, _getIterator3.default)(_iterator7);;) {
var _ref8;
if (_isArray7) {
if (_i7 >= _iterator7.length) break;
_ref8 = _iterator7[_i7++];
} else {
_i7 = _iterator7.next();
if (_i7.done) break;
_ref8 = _i7.value;
}
var specifier = _ref8;
_nodes2.push(buildExportCall(specifier.exported.name, specifier.local));
addExportName(specifier.local.name, specifier.exported.name);
}
_path2.replaceWithMultiple(_nodes2);
}
}
}
}
}
modules.forEach(function (specifiers) {
var setterBody = [];
var target = path.scope.generateUidIdentifier(specifiers.key);
for (var _iterator4 = specifiers.imports, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : (0, _getIterator3.default)(_iterator4);;) {
var _ref5;
if (_isArray4) {
if (_i4 >= _iterator4.length) break;
_ref5 = _iterator4[_i4++];
} else {
_i4 = _iterator4.next();
if (_i4.done) break;
_ref5 = _i4.value;
}
var specifier = _ref5;
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) {
var exportObjRef = path.scope.generateUidIdentifier("exportObj");
setterBody.push(t.variableDeclaration("var", [t.variableDeclarator(exportObjRef, t.objectExpression([]))]));
for (var _iterator5 = specifiers.exports, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : (0, _getIterator3.default)(_iterator5);;) {
var _ref6;
if (_isArray5) {
if (_i5 >= _iterator5.length) break;
_ref6 = _iterator5[_i5++];
} else {
_i5 = _iterator5.next();
if (_i5.done) break;
_ref6 = _i5.value;
}
var node = _ref6;
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 {}
}
setterBody.push(t.expressionStatement(t.callExpression(exportIdent, [exportObjRef])));
}
sources.push(t.stringLiteral(specifiers.key));
setters.push(t.functionExpression(null, [target], t.blockStatement(setterBody)));
});
var moduleName = this.getModuleName();
if (moduleName) moduleName = t.stringLiteral(moduleName);
if (canHoist) {
(0, _babelHelperHoistVariables2.default)(path, function (id) {
return variableIds.push(id);
});
}
if (variableIds.length) {
beforeBody.unshift(t.variableDeclaration("var", variableIds.map(function (id) {
return t.variableDeclarator(id);
})));
}
path.traverse(reassignmentVisitor, {
exports: exportNames,
buildCall: buildExportCall,
scope: path.scope
});
for (var _iterator6 = removedPaths, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : (0, _getIterator3.default)(_iterator6);;) {
var _ref7;
if (_isArray6) {
if (_i6 >= _iterator6.length) break;
_ref7 = _iterator6[_i6++];
} else {
_i6 = _iterator6.next();
if (_i6.done) break;
_ref7 = _i6.value;
}
var _path3 = _ref7;
_path3.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
})];
}
}
}
};
};
var _babelHelperHoistVariables = require("babel-helper-hoist-variables");
var _babelHelperHoistVariables2 = _interopRequireDefault(_babelHelperHoistVariables);
var _babelTemplate = require("babel-template");
var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var buildTemplate = (0, _babelTemplate2.default)("\n SYSTEM_REGISTER(MODULE_NAME, [SOURCES], function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {\n \"use strict\";\n BEFORE_BODY;\n return {\n setters: [SETTERS],\n execute: function () {\n BODY;\n }\n };\n });\n");
var buildExportAll = (0, _babelTemplate2.default)("\n for (var KEY in TARGET) {\n if (KEY !== \"default\" && KEY !== \"__esModule\") EXPORT_OBJ[KEY] = TARGET[KEY];\n }\n");
var TYPE_IMPORT = "Import";
module.exports = exports["default"];