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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _helperPluginUtils() {
const data = require("@babel/helper-plugin-utils");
_helperPluginUtils = function _helperPluginUtils() {
return data;
};
return data;
}
function _helperHoistVariables() {
const data = _interopRequireDefault(require("@babel/helper-hoist-variables"));
_helperHoistVariables = function _helperHoistVariables() {
return data;
};
return data;
}
function _core() {
const data = require("@babel/core");
_core = function _core() {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const buildTemplate = (0, _core().template)(`
SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
"use strict";
BEFORE_BODY;
return {
setters: SETTERS,
execute: function () {
BODY;
}
};
});
`);
const buildExportAll = (0, _core().template)(`
for (var KEY in TARGET) {
if (KEY !== "default" && KEY !== "__esModule") EXPORT_OBJ[KEY] = TARGET[KEY];
}
`);
const TYPE_IMPORT = "Import";
var _default = (0, _helperPluginUtils().declare)((api, options) => {
api.assertVersion(7);
const _options$systemGlobal = options.systemGlobal,
systemGlobal = _options$systemGlobal === void 0 ? "System" : _options$systemGlobal;
const IGNORE_REASSIGNMENT_SYMBOL = Symbol();
const reassignmentVisitor = {
"AssignmentExpression|UpdateExpression"(path) {
if (path.node[IGNORE_REASSIGNMENT_SYMBOL]) return;
path.node[IGNORE_REASSIGNMENT_SYMBOL] = true;
const arg = path.get(path.isAssignmentExpression() ? "left" : "argument");
if (!arg.isIdentifier()) return;
const name = arg.node.name;
if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
const exportedNames = this.exports[name];
if (!exportedNames) return;
let node = path.node;
const isPostUpdateExpression = path.isUpdateExpression({
prefix: false
});
if (isPostUpdateExpression) {
node = _core().types.binaryExpression(node.operator[0], _core().types.unaryExpression("+", _core().types.cloneNode(node.argument)), _core().types.numericLiteral(1));
}
for (var _iterator = exportedNames, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
const exportedName = _ref;
node = this.buildCall(exportedName, node).expression;
}
if (isPostUpdateExpression) {
node = _core().types.sequenceExpression([node, path.node]);
}
path.replaceWith(node);
}
};
return {
visitor: {
CallExpression(path, state) {
if (path.node.callee.type === TYPE_IMPORT) {
path.replaceWith(_core().types.callExpression(_core().types.memberExpression(_core().types.identifier(state.contextIdent), _core().types.identifier("import")), path.node.arguments));
}
},
ReferencedIdentifier(path, state) {
if (path.node.name == "__moduleName" && !path.scope.hasBinding("__moduleName")) {
path.replaceWith(_core().types.memberExpression(_core().types.identifier(state.contextIdent), _core().types.identifier("id")));
}
},
Program: {
enter(path, state) {
state.contextIdent = path.scope.generateUid("context");
},
exit(path, state) {
const exportIdent = path.scope.generateUid("export");
const contextIdent = state.contextIdent;
const exportNames = Object.create(null);
const modules = [];
let beforeBody = [];
const setters = [];
const sources = [];
const variableIds = [];
const removedPaths = [];
function addExportName(key, val) {
exportNames[key] = exportNames[key] || [];
exportNames[key].push(val);
}
function pushModule(source, key, specifiers) {
let module;
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 _core().types.expressionStatement(_core().types.callExpression(_core().types.identifier(exportIdent), [_core().types.stringLiteral(name), val]));
}
const body = path.get("body");
let canHoist = true;
for (var _iterator2 = body, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref2 = _iterator2[_i2++];
} else {
_i2 = _iterator2.next();
if (_i2.done) break;
_ref2 = _i2.value;
}
let path = _ref2;
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 : _iterator3[Symbol.iterator]();;) {
var _ref3;
if (_isArray3) {
if (_i3 >= _iterator3.length) break;
_ref3 = _iterator3[_i3++];
} else {
_i3 = _iterator3.next();
if (_i3.done) break;
_ref3 = _i3.value;
}
const path = _ref3;
if (canHoist && path.isFunctionDeclaration()) {
beforeBody.push(path.node);
removedPaths.push(path);
} else if (path.isImportDeclaration()) {
const source = path.node.source.value;
pushModule(source, "imports", path.node.specifiers);
for (const name in path.getBindingIdentifiers()) {
path.scope.removeBinding(name);
variableIds.push(_core().types.identifier(name));
}
path.remove();
} else if (path.isExportAllDeclaration()) {
pushModule(path.node.source.value, "exports", path.node);
path.remove();
} else if (path.isExportDefaultDeclaration()) {
const declar = path.get("declaration");
if (declar.isClassDeclaration() || declar.isFunctionDeclaration()) {
const id = declar.node.id;
const nodes = [];
if (id) {
nodes.push(declar.node);
nodes.push(buildExportCall("default", _core().types.cloneNode(id)));
addExportName(id.name, "default");
} else {
nodes.push(buildExportCall("default", _core().types.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()) {
const declar = path.get("declaration");
if (declar.node) {
path.replaceWith(declar);
const nodes = [];
let bindingIdentifiers;
if (path.isFunction()) {
const node = declar.node;
const name = node.id.name;
if (canHoist) {
addExportName(name, name);
beforeBody.push(node);
beforeBody.push(buildExportCall(name, _core().types.cloneNode(node.id)));
removedPaths.push(path);
} else {
bindingIdentifiers = {
[name]: node.id
};
}
} else {
bindingIdentifiers = declar.getBindingIdentifiers();
}
for (const name in bindingIdentifiers) {
addExportName(name, name);
nodes.push(buildExportCall(name, _core().types.identifier(name)));
}
path.insertAfter(nodes);
} else {
const specifiers = path.node.specifiers;
if (specifiers && specifiers.length) {
if (path.node.source) {
pushModule(path.node.source.value, "exports", specifiers);
path.remove();
} else {
const nodes = [];
for (var _iterator6 = specifiers, _isArray6 = Array.isArray(_iterator6), _i7 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
var _ref6;
if (_isArray6) {
if (_i7 >= _iterator6.length) break;
_ref6 = _iterator6[_i7++];
} else {
_i7 = _iterator6.next();
if (_i7.done) break;
_ref6 = _i7.value;
}
const specifier = _ref6;
nodes.push(buildExportCall(specifier.exported.name, specifier.local));
addExportName(specifier.local.name, specifier.exported.name);
}
path.replaceWithMultiple(nodes);
}
}
}
}
}
modules.forEach(function (specifiers) {
const setterBody = [];
const target = path.scope.generateUid(specifiers.key);
for (var _iterator4 = specifiers.imports, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
var _ref4;
if (_isArray4) {
if (_i4 >= _iterator4.length) break;
_ref4 = _iterator4[_i4++];
} else {
_i4 = _iterator4.next();
if (_i4.done) break;
_ref4 = _i4.value;
}
let specifier = _ref4;
if (_core().types.isImportNamespaceSpecifier(specifier)) {
setterBody.push(_core().types.expressionStatement(_core().types.assignmentExpression("=", specifier.local, _core().types.identifier(target))));
} else if (_core().types.isImportDefaultSpecifier(specifier)) {
specifier = _core().types.importSpecifier(specifier.local, _core().types.identifier("default"));
}
if (_core().types.isImportSpecifier(specifier)) {
setterBody.push(_core().types.expressionStatement(_core().types.assignmentExpression("=", specifier.local, _core().types.memberExpression(_core().types.identifier(target), specifier.imported))));
}
}
if (specifiers.exports.length) {
const exportObj = path.scope.generateUid("exportObj");
setterBody.push(_core().types.variableDeclaration("var", [_core().types.variableDeclarator(_core().types.identifier(exportObj), _core().types.objectExpression([]))]));
for (var _iterator5 = specifiers.exports, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
var _ref5;
if (_isArray5) {
if (_i5 >= _iterator5.length) break;
_ref5 = _iterator5[_i5++];
} else {
_i5 = _iterator5.next();
if (_i5.done) break;
_ref5 = _i5.value;
}
const node = _ref5;
if (_core().types.isExportAllDeclaration(node)) {
setterBody.push(buildExportAll({
KEY: path.scope.generateUidIdentifier("key"),
EXPORT_OBJ: _core().types.identifier(exportObj),
TARGET: _core().types.identifier(target)
}));
} else if (_core().types.isExportSpecifier(node)) {
setterBody.push(_core().types.expressionStatement(_core().types.assignmentExpression("=", _core().types.memberExpression(_core().types.identifier(exportObj), node.exported), _core().types.memberExpression(_core().types.identifier(target), node.local))));
} else {}
}
setterBody.push(_core().types.expressionStatement(_core().types.callExpression(_core().types.identifier(exportIdent), [_core().types.identifier(exportObj)])));
}
sources.push(_core().types.stringLiteral(specifiers.key));
setters.push(_core().types.functionExpression(null, [_core().types.identifier(target)], _core().types.blockStatement(setterBody)));
});
let moduleName = this.getModuleName();
if (moduleName) moduleName = _core().types.stringLiteral(moduleName);
if (canHoist) {
(0, _helperHoistVariables().default)(path, id => variableIds.push(id));
}
if (variableIds.length) {
beforeBody.unshift(_core().types.variableDeclaration("var", variableIds.map(id => _core().types.variableDeclarator(id))));
}
path.traverse(reassignmentVisitor, {
exports: exportNames,
buildCall: buildExportCall,
scope: path.scope
});
for (var _i6 = 0; _i6 < removedPaths.length; _i6++) {
const path = removedPaths[_i6];
path.remove();
}
path.node.body = [buildTemplate({
SYSTEM_REGISTER: _core().types.memberExpression(_core().types.identifier(systemGlobal), _core().types.identifier("register")),
BEFORE_BODY: beforeBody,
MODULE_NAME: moduleName,
SETTERS: _core().types.arrayExpression(setters),
SOURCES: _core().types.arrayExpression(sources),
BODY: path.node.body,
EXPORT_IDENTIFIER: _core().types.identifier(exportIdent),
CONTEXT_IDENTIFIER: _core().types.identifier(contextIdent)
})];
}
}
}
};
});
exports.default = _default;
|