UNPKG

24.3 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4
5var _keys = require("babel-runtime/core-js/object/keys");
6
7var _keys2 = _interopRequireDefault(_keys);
8
9var _create = require("babel-runtime/core-js/object/create");
10
11var _create2 = _interopRequireDefault(_create);
12
13var _getIterator2 = require("babel-runtime/core-js/get-iterator");
14
15var _getIterator3 = _interopRequireDefault(_getIterator2);
16
17var _symbol = require("babel-runtime/core-js/symbol");
18
19var _symbol2 = _interopRequireDefault(_symbol);
20
21exports.default = function () {
22 var REASSIGN_REMAP_SKIP = (0, _symbol2.default)();
23
24 var reassignmentVisitor = {
25 ReferencedIdentifier: function ReferencedIdentifier(path) {
26 var name = path.node.name;
27 var remap = this.remaps[name];
28 if (!remap) return;
29
30 if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
31
32 var replacement = t.cloneDeep(remap);
33
34 replacement.loc = path.node.loc;
35
36 if (path.parentPath.isCallExpression({ callee: path.node })) {
37 path.replaceWith(t.sequenceExpression([t.numericLiteral(0), replacement]));
38 } else if (path.isJSXIdentifier() && t.isMemberExpression(replacement)) {
39 var object = replacement.object,
40 property = replacement.property;
41
42 path.replaceWith(t.JSXMemberExpression(t.JSXIdentifier(object.name), t.JSXIdentifier(property.name)));
43 } else {
44 path.replaceWith(replacement);
45 }
46 this.requeueInParent(path);
47 },
48 AssignmentExpression: function AssignmentExpression(path) {
49 var node = path.node;
50 if (node[REASSIGN_REMAP_SKIP]) return;
51
52 var left = path.get("left");
53 if (left.isIdentifier()) {
54 var name = left.node.name;
55 var exports = this.exports[name];
56 if (!exports) return;
57
58 if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
59
60 node[REASSIGN_REMAP_SKIP] = true;
61
62 for (var _iterator = exports, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
63 var _ref;
64
65 if (_isArray) {
66 if (_i >= _iterator.length) break;
67 _ref = _iterator[_i++];
68 } else {
69 _i = _iterator.next();
70 if (_i.done) break;
71 _ref = _i.value;
72 }
73
74 var reid = _ref;
75
76 node = buildExportsAssignment(reid, node).expression;
77 }
78
79 path.replaceWith(node);
80 this.requeueInParent(path);
81 } else if (left.isObjectPattern()) {
82 for (var _iterator2 = left.node.properties, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
83 var _ref2;
84
85 if (_isArray2) {
86 if (_i2 >= _iterator2.length) break;
87 _ref2 = _iterator2[_i2++];
88 } else {
89 _i2 = _iterator2.next();
90 if (_i2.done) break;
91 _ref2 = _i2.value;
92 }
93
94 var property = _ref2;
95
96 var _name = property.value.name;
97
98 var _exports = this.exports[_name];
99 if (!_exports) continue;
100
101 if (this.scope.getBinding(_name) !== path.scope.getBinding(_name)) return;
102
103 node[REASSIGN_REMAP_SKIP] = true;
104
105 path.insertAfter(buildExportsAssignment(t.identifier(_name), t.identifier(_name)));
106 }
107 } else if (left.isArrayPattern()) {
108 for (var _iterator3 = left.node.elements, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
109 var _ref3;
110
111 if (_isArray3) {
112 if (_i3 >= _iterator3.length) break;
113 _ref3 = _iterator3[_i3++];
114 } else {
115 _i3 = _iterator3.next();
116 if (_i3.done) break;
117 _ref3 = _i3.value;
118 }
119
120 var element = _ref3;
121
122 if (!element) continue;
123 var _name2 = element.name;
124
125 var _exports2 = this.exports[_name2];
126 if (!_exports2) continue;
127
128 if (this.scope.getBinding(_name2) !== path.scope.getBinding(_name2)) return;
129
130 node[REASSIGN_REMAP_SKIP] = true;
131
132 path.insertAfter(buildExportsAssignment(t.identifier(_name2), t.identifier(_name2)));
133 }
134 }
135 },
136 UpdateExpression: function UpdateExpression(path) {
137 var arg = path.get("argument");
138 if (!arg.isIdentifier()) return;
139
140 var name = arg.node.name;
141 var exports = this.exports[name];
142 if (!exports) return;
143
144 if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
145
146 var node = t.assignmentExpression(path.node.operator[0] + "=", arg.node, t.numericLiteral(1));
147
148 if (path.parentPath.isExpressionStatement() && !path.isCompletionRecord() || path.node.prefix) {
149 path.replaceWith(node);
150 this.requeueInParent(path);
151 return;
152 }
153
154 var nodes = [];
155 nodes.push(node);
156
157 var operator = void 0;
158 if (path.node.operator === "--") {
159 operator = "+";
160 } else {
161 operator = "-";
162 }
163 nodes.push(t.binaryExpression(operator, arg.node, t.numericLiteral(1)));
164
165 path.replaceWithMultiple(t.sequenceExpression(nodes));
166 }
167 };
168
169 return {
170 inherits: _babelPluginTransformStrictMode2.default,
171
172 visitor: {
173 ThisExpression: function ThisExpression(path, state) {
174 if (this.ranCommonJS) return;
175
176 if (state.opts.allowTopLevelThis !== true && !path.findParent(function (path) {
177 return !path.is("shadow") && THIS_BREAK_KEYS.indexOf(path.type) >= 0;
178 })) {
179 path.replaceWith(t.identifier("undefined"));
180 }
181 },
182
183
184 Program: {
185 exit: function exit(path) {
186 this.ranCommonJS = true;
187
188 var strict = !!this.opts.strict;
189 var noInterop = !!this.opts.noInterop;
190
191 var scope = path.scope;
192
193 scope.rename("module");
194 scope.rename("exports");
195 scope.rename("require");
196
197 var hasExports = false;
198 var hasImports = false;
199
200 var body = path.get("body");
201 var imports = (0, _create2.default)(null);
202 var exports = (0, _create2.default)(null);
203
204 var nonHoistedExportNames = (0, _create2.default)(null);
205
206 var topNodes = [];
207 var remaps = (0, _create2.default)(null);
208
209 var requires = (0, _create2.default)(null);
210
211 function addRequire(source, blockHoist) {
212 var cached = requires[source];
213 if (cached) return cached;
214
215 var ref = path.scope.generateUidIdentifier((0, _path2.basename)(source, (0, _path2.extname)(source)));
216
217 var varDecl = t.variableDeclaration("var", [t.variableDeclarator(ref, buildRequire(t.stringLiteral(source)).expression)]);
218
219 if (imports[source]) {
220 varDecl.loc = imports[source].loc;
221 }
222
223 if (typeof blockHoist === "number" && blockHoist > 0) {
224 varDecl._blockHoist = blockHoist;
225 }
226
227 topNodes.push(varDecl);
228
229 return requires[source] = ref;
230 }
231
232 function addTo(obj, key, arr) {
233 var existing = obj[key] || [];
234 obj[key] = existing.concat(arr);
235 }
236
237 for (var _iterator4 = body, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : (0, _getIterator3.default)(_iterator4);;) {
238 var _ref4;
239
240 if (_isArray4) {
241 if (_i4 >= _iterator4.length) break;
242 _ref4 = _iterator4[_i4++];
243 } else {
244 _i4 = _iterator4.next();
245 if (_i4.done) break;
246 _ref4 = _i4.value;
247 }
248
249 var _path = _ref4;
250
251 if (_path.isExportDeclaration()) {
252 hasExports = true;
253
254 var specifiers = [].concat(_path.get("declaration"), _path.get("specifiers"));
255 for (var _iterator6 = specifiers, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : (0, _getIterator3.default)(_iterator6);;) {
256 var _ref6;
257
258 if (_isArray6) {
259 if (_i6 >= _iterator6.length) break;
260 _ref6 = _iterator6[_i6++];
261 } else {
262 _i6 = _iterator6.next();
263 if (_i6.done) break;
264 _ref6 = _i6.value;
265 }
266
267 var _specifier2 = _ref6;
268
269 var ids = _specifier2.getBindingIdentifiers();
270 if (ids.__esModule) {
271 throw _specifier2.buildCodeFrameError("Illegal export \"__esModule\"");
272 }
273 }
274 }
275
276 if (_path.isImportDeclaration()) {
277 var _importsEntry$specifi;
278
279 hasImports = true;
280
281 var key = _path.node.source.value;
282 var importsEntry = imports[key] || {
283 specifiers: [],
284 maxBlockHoist: 0,
285 loc: _path.node.loc
286 };
287
288 (_importsEntry$specifi = importsEntry.specifiers).push.apply(_importsEntry$specifi, _path.node.specifiers);
289
290 if (typeof _path.node._blockHoist === "number") {
291 importsEntry.maxBlockHoist = Math.max(_path.node._blockHoist, importsEntry.maxBlockHoist);
292 }
293
294 imports[key] = importsEntry;
295
296 _path.remove();
297 } else if (_path.isExportDefaultDeclaration()) {
298 var declaration = _path.get("declaration");
299 if (declaration.isFunctionDeclaration()) {
300 var id = declaration.node.id;
301 var defNode = t.identifier("default");
302 if (id) {
303 addTo(exports, id.name, defNode);
304 topNodes.push(buildExportsAssignment(defNode, id));
305 _path.replaceWith(declaration.node);
306 } else {
307 topNodes.push(buildExportsAssignment(defNode, t.toExpression(declaration.node)));
308 _path.remove();
309 }
310 } else if (declaration.isClassDeclaration()) {
311 var _id = declaration.node.id;
312 var _defNode = t.identifier("default");
313 if (_id) {
314 addTo(exports, _id.name, _defNode);
315 _path.replaceWithMultiple([declaration.node, buildExportsAssignment(_defNode, _id)]);
316 } else {
317 _path.replaceWith(buildExportsAssignment(_defNode, t.toExpression(declaration.node)));
318
319 _path.parentPath.requeue(_path.get("expression.left"));
320 }
321 } else {
322 _path.replaceWith(buildExportsAssignment(t.identifier("default"), declaration.node));
323
324 _path.parentPath.requeue(_path.get("expression.left"));
325 }
326 } else if (_path.isExportNamedDeclaration()) {
327 var _declaration = _path.get("declaration");
328 if (_declaration.node) {
329 if (_declaration.isFunctionDeclaration()) {
330 var _id2 = _declaration.node.id;
331 addTo(exports, _id2.name, _id2);
332 topNodes.push(buildExportsAssignment(_id2, _id2));
333 _path.replaceWith(_declaration.node);
334 } else if (_declaration.isClassDeclaration()) {
335 var _id3 = _declaration.node.id;
336 addTo(exports, _id3.name, _id3);
337 _path.replaceWithMultiple([_declaration.node, buildExportsAssignment(_id3, _id3)]);
338 nonHoistedExportNames[_id3.name] = true;
339 } else if (_declaration.isVariableDeclaration()) {
340 var declarators = _declaration.get("declarations");
341 for (var _iterator7 = declarators, _isArray7 = Array.isArray(_iterator7), _i7 = 0, _iterator7 = _isArray7 ? _iterator7 : (0, _getIterator3.default)(_iterator7);;) {
342 var _ref7;
343
344 if (_isArray7) {
345 if (_i7 >= _iterator7.length) break;
346 _ref7 = _iterator7[_i7++];
347 } else {
348 _i7 = _iterator7.next();
349 if (_i7.done) break;
350 _ref7 = _i7.value;
351 }
352
353 var decl = _ref7;
354
355 var _id4 = decl.get("id");
356
357 var init = decl.get("init");
358 var exportsToInsert = [];
359 if (!init.node) init.replaceWith(t.identifier("undefined"));
360
361 if (_id4.isIdentifier()) {
362 addTo(exports, _id4.node.name, _id4.node);
363 init.replaceWith(buildExportsAssignment(_id4.node, init.node).expression);
364 nonHoistedExportNames[_id4.node.name] = true;
365 } else if (_id4.isObjectPattern()) {
366 for (var _i8 = 0; _i8 < _id4.node.properties.length; _i8++) {
367 var prop = _id4.node.properties[_i8];
368 var propValue = prop.value;
369 if (t.isAssignmentPattern(propValue)) {
370 propValue = propValue.left;
371 } else if (t.isRestProperty(prop)) {
372 propValue = prop.argument;
373 }
374 addTo(exports, propValue.name, propValue);
375 exportsToInsert.push(buildExportsAssignment(propValue, propValue));
376 nonHoistedExportNames[propValue.name] = true;
377 }
378 } else if (_id4.isArrayPattern() && _id4.node.elements) {
379 for (var _i9 = 0; _i9 < _id4.node.elements.length; _i9++) {
380 var elem = _id4.node.elements[_i9];
381 if (!elem) continue;
382 if (t.isAssignmentPattern(elem)) {
383 elem = elem.left;
384 } else if (t.isRestElement(elem)) {
385 elem = elem.argument;
386 }
387 var name = elem.name;
388 addTo(exports, name, elem);
389 exportsToInsert.push(buildExportsAssignment(elem, elem));
390 nonHoistedExportNames[name] = true;
391 }
392 }
393 _path.insertAfter(exportsToInsert);
394 }
395 _path.replaceWith(_declaration.node);
396 }
397 continue;
398 }
399
400 var _specifiers = _path.get("specifiers");
401 var nodes = [];
402 var _source = _path.node.source;
403 if (_source) {
404 var ref = addRequire(_source.value, _path.node._blockHoist);
405
406 for (var _iterator8 = _specifiers, _isArray8 = Array.isArray(_iterator8), _i10 = 0, _iterator8 = _isArray8 ? _iterator8 : (0, _getIterator3.default)(_iterator8);;) {
407 var _ref8;
408
409 if (_isArray8) {
410 if (_i10 >= _iterator8.length) break;
411 _ref8 = _iterator8[_i10++];
412 } else {
413 _i10 = _iterator8.next();
414 if (_i10.done) break;
415 _ref8 = _i10.value;
416 }
417
418 var _specifier3 = _ref8;
419
420 if (_specifier3.isExportNamespaceSpecifier()) {} else if (_specifier3.isExportDefaultSpecifier()) {} else if (_specifier3.isExportSpecifier()) {
421 if (!noInterop && _specifier3.node.local.name === "default") {
422 topNodes.push(buildExportsFrom(t.stringLiteral(_specifier3.node.exported.name), t.memberExpression(t.callExpression(this.addHelper("interopRequireDefault"), [ref]), _specifier3.node.local)));
423 } else {
424 topNodes.push(buildExportsFrom(t.stringLiteral(_specifier3.node.exported.name), t.memberExpression(ref, _specifier3.node.local)));
425 }
426 nonHoistedExportNames[_specifier3.node.exported.name] = true;
427 }
428 }
429 } else {
430 for (var _iterator9 = _specifiers, _isArray9 = Array.isArray(_iterator9), _i11 = 0, _iterator9 = _isArray9 ? _iterator9 : (0, _getIterator3.default)(_iterator9);;) {
431 var _ref9;
432
433 if (_isArray9) {
434 if (_i11 >= _iterator9.length) break;
435 _ref9 = _iterator9[_i11++];
436 } else {
437 _i11 = _iterator9.next();
438 if (_i11.done) break;
439 _ref9 = _i11.value;
440 }
441
442 var _specifier4 = _ref9;
443
444 if (_specifier4.isExportSpecifier()) {
445 addTo(exports, _specifier4.node.local.name, _specifier4.node.exported);
446 nonHoistedExportNames[_specifier4.node.exported.name] = true;
447 nodes.push(buildExportsAssignment(_specifier4.node.exported, _specifier4.node.local));
448 }
449 }
450 }
451 _path.replaceWithMultiple(nodes);
452 } else if (_path.isExportAllDeclaration()) {
453 var exportNode = buildExportAll({
454 OBJECT: addRequire(_path.node.source.value, _path.node._blockHoist)
455 });
456 exportNode.loc = _path.node.loc;
457 topNodes.push(exportNode);
458 _path.remove();
459 }
460 }
461
462 for (var source in imports) {
463 var _imports$source = imports[source],
464 specifiers = _imports$source.specifiers,
465 maxBlockHoist = _imports$source.maxBlockHoist;
466
467 if (specifiers.length) {
468 var uid = addRequire(source, maxBlockHoist);
469
470 var wildcard = void 0;
471
472 for (var i = 0; i < specifiers.length; i++) {
473 var specifier = specifiers[i];
474 if (t.isImportNamespaceSpecifier(specifier)) {
475 if (strict || noInterop) {
476 remaps[specifier.local.name] = uid;
477 } else {
478 var varDecl = t.variableDeclaration("var", [t.variableDeclarator(specifier.local, t.callExpression(this.addHelper("interopRequireWildcard"), [uid]))]);
479
480 if (maxBlockHoist > 0) {
481 varDecl._blockHoist = maxBlockHoist;
482 }
483
484 topNodes.push(varDecl);
485 }
486 wildcard = specifier.local;
487 } else if (t.isImportDefaultSpecifier(specifier)) {
488 specifiers[i] = t.importSpecifier(specifier.local, t.identifier("default"));
489 }
490 }
491
492 for (var _iterator5 = specifiers, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : (0, _getIterator3.default)(_iterator5);;) {
493 var _ref5;
494
495 if (_isArray5) {
496 if (_i5 >= _iterator5.length) break;
497 _ref5 = _iterator5[_i5++];
498 } else {
499 _i5 = _iterator5.next();
500 if (_i5.done) break;
501 _ref5 = _i5.value;
502 }
503
504 var _specifier = _ref5;
505
506 if (t.isImportSpecifier(_specifier)) {
507 var target = uid;
508 if (_specifier.imported.name === "default") {
509 if (wildcard) {
510 target = wildcard;
511 } else if (!noInterop) {
512 target = wildcard = path.scope.generateUidIdentifier(uid.name);
513 var _varDecl = t.variableDeclaration("var", [t.variableDeclarator(target, t.callExpression(this.addHelper("interopRequireDefault"), [uid]))]);
514
515 if (maxBlockHoist > 0) {
516 _varDecl._blockHoist = maxBlockHoist;
517 }
518
519 topNodes.push(_varDecl);
520 }
521 }
522 remaps[_specifier.local.name] = t.memberExpression(t.cloneWithoutLoc(target), t.cloneWithoutLoc(_specifier.imported));
523 }
524 }
525 } else {
526 var requireNode = buildRequire(t.stringLiteral(source));
527 requireNode.loc = imports[source].loc;
528 topNodes.push(requireNode);
529 }
530 }
531
532 if (hasImports && (0, _keys2.default)(nonHoistedExportNames).length) {
533 var maxHoistedExportsNodeAssignmentLength = 100;
534 var nonHoistedExportNamesArr = (0, _keys2.default)(nonHoistedExportNames);
535
536 var _loop = function _loop(currentExportsNodeAssignmentLength) {
537 var nonHoistedExportNamesChunk = nonHoistedExportNamesArr.slice(currentExportsNodeAssignmentLength, currentExportsNodeAssignmentLength + maxHoistedExportsNodeAssignmentLength);
538
539 var hoistedExportsNode = t.identifier("undefined");
540
541 nonHoistedExportNamesChunk.forEach(function (name) {
542 hoistedExportsNode = buildExportsAssignment(t.identifier(name), hoistedExportsNode).expression;
543 });
544
545 var node = t.expressionStatement(hoistedExportsNode);
546 node._blockHoist = 3;
547
548 topNodes.unshift(node);
549 };
550
551 for (var currentExportsNodeAssignmentLength = 0; currentExportsNodeAssignmentLength < nonHoistedExportNamesArr.length; currentExportsNodeAssignmentLength += maxHoistedExportsNodeAssignmentLength) {
552 _loop(currentExportsNodeAssignmentLength);
553 }
554 }
555
556 if (hasExports && !strict) {
557 var buildTemplate = buildExportsModuleDeclaration;
558 if (this.opts.loose) buildTemplate = buildLooseExportsModuleDeclaration;
559
560 var declar = buildTemplate();
561 declar._blockHoist = 3;
562
563 topNodes.unshift(declar);
564 }
565
566 path.unshiftContainer("body", topNodes);
567 path.traverse(reassignmentVisitor, {
568 remaps: remaps,
569 scope: scope,
570 exports: exports,
571 requeueInParent: function requeueInParent(newPath) {
572 return path.requeue(newPath);
573 }
574 });
575 }
576 }
577 }
578 };
579};
580
581var _path2 = require("path");
582
583var _babelTemplate = require("babel-template");
584
585var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
586
587var _babelPluginTransformStrictMode = require("babel-plugin-transform-strict-mode");
588
589var _babelPluginTransformStrictMode2 = _interopRequireDefault(_babelPluginTransformStrictMode);
590
591var _babelTypes = require("babel-types");
592
593var t = _interopRequireWildcard(_babelTypes);
594
595function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
596
597function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
598
599var buildRequire = (0, _babelTemplate2.default)("\n require($0);\n");
600
601var buildExportsModuleDeclaration = (0, _babelTemplate2.default)("\n Object.defineProperty(exports, \"__esModule\", {\n value: true\n });\n");
602
603var buildExportsFrom = (0, _babelTemplate2.default)("\n Object.defineProperty(exports, $0, {\n enumerable: true,\n get: function () {\n return $1;\n }\n });\n");
604
605var buildLooseExportsModuleDeclaration = (0, _babelTemplate2.default)("\n exports.__esModule = true;\n");
606
607var buildExportsAssignment = (0, _babelTemplate2.default)("\n exports.$0 = $1;\n");
608
609var buildExportAll = (0, _babelTemplate2.default)("\n Object.keys(OBJECT).forEach(function (key) {\n if (key === \"default\" || key === \"__esModule\") return;\n Object.defineProperty(exports, key, {\n enumerable: true,\n get: function () {\n return OBJECT[key];\n }\n });\n });\n");
610
611var THIS_BREAK_KEYS = ["FunctionExpression", "FunctionDeclaration", "ClassProperty", "ClassMethod", "ObjectMethod"];
612
613module.exports = exports["default"];
\No newline at end of file