index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. "use strict";
  2. exports.__esModule = true;
  3. var _create = require("babel-runtime/core-js/object/create");
  4. var _create2 = _interopRequireDefault(_create);
  5. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  6. var _getIterator3 = _interopRequireDefault(_getIterator2);
  7. var _symbol = require("babel-runtime/core-js/symbol");
  8. var _symbol2 = _interopRequireDefault(_symbol);
  9. exports.default = function (_ref) {
  10. var t = _ref.types;
  11. var IGNORE_REASSIGNMENT_SYMBOL = (0, _symbol2.default)();
  12. var reassignmentVisitor = {
  13. "AssignmentExpression|UpdateExpression": function AssignmentExpressionUpdateExpression(path) {
  14. if (path.node[IGNORE_REASSIGNMENT_SYMBOL]) return;
  15. path.node[IGNORE_REASSIGNMENT_SYMBOL] = true;
  16. var arg = path.get(path.isAssignmentExpression() ? "left" : "argument");
  17. if (!arg.isIdentifier()) return;
  18. var name = arg.node.name;
  19. if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
  20. var exportedNames = this.exports[name];
  21. if (!exportedNames) return;
  22. var node = path.node;
  23. var isPostUpdateExpression = path.isUpdateExpression() && !node.prefix;
  24. if (isPostUpdateExpression) {
  25. 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;
  26. }
  27. for (var _iterator = exportedNames, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  28. var _ref2;
  29. if (_isArray) {
  30. if (_i >= _iterator.length) break;
  31. _ref2 = _iterator[_i++];
  32. } else {
  33. _i = _iterator.next();
  34. if (_i.done) break;
  35. _ref2 = _i.value;
  36. }
  37. var exportedName = _ref2;
  38. node = this.buildCall(exportedName, node).expression;
  39. }
  40. if (isPostUpdateExpression) node = t.sequenceExpression([node, path.node]);
  41. path.replaceWith(node);
  42. }
  43. };
  44. return {
  45. visitor: {
  46. CallExpression: function CallExpression(path, state) {
  47. if (path.node.callee.type === TYPE_IMPORT) {
  48. var contextIdent = state.contextIdent;
  49. path.replaceWith(t.callExpression(t.memberExpression(contextIdent, t.identifier("import")), path.node.arguments));
  50. }
  51. },
  52. ReferencedIdentifier: function ReferencedIdentifier(path, state) {
  53. if (path.node.name == "__moduleName" && !path.scope.hasBinding("__moduleName")) {
  54. path.replaceWith(t.memberExpression(state.contextIdent, t.identifier("id")));
  55. }
  56. },
  57. Program: {
  58. enter: function enter(path, state) {
  59. state.contextIdent = path.scope.generateUidIdentifier("context");
  60. },
  61. exit: function exit(path, state) {
  62. var exportIdent = path.scope.generateUidIdentifier("export");
  63. var contextIdent = state.contextIdent;
  64. var exportNames = (0, _create2.default)(null);
  65. var modules = [];
  66. var beforeBody = [];
  67. var setters = [];
  68. var sources = [];
  69. var variableIds = [];
  70. var removedPaths = [];
  71. function addExportName(key, val) {
  72. exportNames[key] = exportNames[key] || [];
  73. exportNames[key].push(val);
  74. }
  75. function pushModule(source, key, specifiers) {
  76. var module = void 0;
  77. modules.forEach(function (m) {
  78. if (m.key === source) {
  79. module = m;
  80. }
  81. });
  82. if (!module) {
  83. modules.push(module = { key: source, imports: [], exports: [] });
  84. }
  85. module[key] = module[key].concat(specifiers);
  86. }
  87. function buildExportCall(name, val) {
  88. return t.expressionStatement(t.callExpression(exportIdent, [t.stringLiteral(name), val]));
  89. }
  90. var body = path.get("body");
  91. var canHoist = true;
  92. for (var _iterator2 = body, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  93. var _ref3;
  94. if (_isArray2) {
  95. if (_i2 >= _iterator2.length) break;
  96. _ref3 = _iterator2[_i2++];
  97. } else {
  98. _i2 = _iterator2.next();
  99. if (_i2.done) break;
  100. _ref3 = _i2.value;
  101. }
  102. var _path = _ref3;
  103. if (_path.isExportDeclaration()) _path = _path.get("declaration");
  104. if (_path.isVariableDeclaration() && _path.node.kind !== "var") {
  105. canHoist = false;
  106. break;
  107. }
  108. }
  109. for (var _iterator3 = body, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
  110. var _ref4;
  111. if (_isArray3) {
  112. if (_i3 >= _iterator3.length) break;
  113. _ref4 = _iterator3[_i3++];
  114. } else {
  115. _i3 = _iterator3.next();
  116. if (_i3.done) break;
  117. _ref4 = _i3.value;
  118. }
  119. var _path2 = _ref4;
  120. if (canHoist && _path2.isFunctionDeclaration()) {
  121. beforeBody.push(_path2.node);
  122. removedPaths.push(_path2);
  123. } else if (_path2.isImportDeclaration()) {
  124. var source = _path2.node.source.value;
  125. pushModule(source, "imports", _path2.node.specifiers);
  126. for (var name in _path2.getBindingIdentifiers()) {
  127. _path2.scope.removeBinding(name);
  128. variableIds.push(t.identifier(name));
  129. }
  130. _path2.remove();
  131. } else if (_path2.isExportAllDeclaration()) {
  132. pushModule(_path2.node.source.value, "exports", _path2.node);
  133. _path2.remove();
  134. } else if (_path2.isExportDefaultDeclaration()) {
  135. var declar = _path2.get("declaration");
  136. if (declar.isClassDeclaration() || declar.isFunctionDeclaration()) {
  137. var id = declar.node.id;
  138. var nodes = [];
  139. if (id) {
  140. nodes.push(declar.node);
  141. nodes.push(buildExportCall("default", id));
  142. addExportName(id.name, "default");
  143. } else {
  144. nodes.push(buildExportCall("default", t.toExpression(declar.node)));
  145. }
  146. if (!canHoist || declar.isClassDeclaration()) {
  147. _path2.replaceWithMultiple(nodes);
  148. } else {
  149. beforeBody = beforeBody.concat(nodes);
  150. removedPaths.push(_path2);
  151. }
  152. } else {
  153. _path2.replaceWith(buildExportCall("default", declar.node));
  154. }
  155. } else if (_path2.isExportNamedDeclaration()) {
  156. var _declar = _path2.get("declaration");
  157. if (_declar.node) {
  158. _path2.replaceWith(_declar);
  159. var _nodes = [];
  160. var bindingIdentifiers = void 0;
  161. if (_path2.isFunction()) {
  162. var node = _declar.node;
  163. var _name = node.id.name;
  164. if (canHoist) {
  165. addExportName(_name, _name);
  166. beforeBody.push(node);
  167. beforeBody.push(buildExportCall(_name, node.id));
  168. removedPaths.push(_path2);
  169. } else {
  170. var _bindingIdentifiers;
  171. bindingIdentifiers = (_bindingIdentifiers = {}, _bindingIdentifiers[_name] = node.id, _bindingIdentifiers);
  172. }
  173. } else {
  174. bindingIdentifiers = _declar.getBindingIdentifiers();
  175. }
  176. for (var _name2 in bindingIdentifiers) {
  177. addExportName(_name2, _name2);
  178. _nodes.push(buildExportCall(_name2, t.identifier(_name2)));
  179. }
  180. _path2.insertAfter(_nodes);
  181. } else {
  182. var specifiers = _path2.node.specifiers;
  183. if (specifiers && specifiers.length) {
  184. if (_path2.node.source) {
  185. pushModule(_path2.node.source.value, "exports", specifiers);
  186. _path2.remove();
  187. } else {
  188. var _nodes2 = [];
  189. for (var _iterator7 = specifiers, _isArray7 = Array.isArray(_iterator7), _i7 = 0, _iterator7 = _isArray7 ? _iterator7 : (0, _getIterator3.default)(_iterator7);;) {
  190. var _ref8;
  191. if (_isArray7) {
  192. if (_i7 >= _iterator7.length) break;
  193. _ref8 = _iterator7[_i7++];
  194. } else {
  195. _i7 = _iterator7.next();
  196. if (_i7.done) break;
  197. _ref8 = _i7.value;
  198. }
  199. var specifier = _ref8;
  200. _nodes2.push(buildExportCall(specifier.exported.name, specifier.local));
  201. addExportName(specifier.local.name, specifier.exported.name);
  202. }
  203. _path2.replaceWithMultiple(_nodes2);
  204. }
  205. }
  206. }
  207. }
  208. }
  209. modules.forEach(function (specifiers) {
  210. var setterBody = [];
  211. var target = path.scope.generateUidIdentifier(specifiers.key);
  212. for (var _iterator4 = specifiers.imports, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : (0, _getIterator3.default)(_iterator4);;) {
  213. var _ref5;
  214. if (_isArray4) {
  215. if (_i4 >= _iterator4.length) break;
  216. _ref5 = _iterator4[_i4++];
  217. } else {
  218. _i4 = _iterator4.next();
  219. if (_i4.done) break;
  220. _ref5 = _i4.value;
  221. }
  222. var specifier = _ref5;
  223. if (t.isImportNamespaceSpecifier(specifier)) {
  224. setterBody.push(t.expressionStatement(t.assignmentExpression("=", specifier.local, target)));
  225. } else if (t.isImportDefaultSpecifier(specifier)) {
  226. specifier = t.importSpecifier(specifier.local, t.identifier("default"));
  227. }
  228. if (t.isImportSpecifier(specifier)) {
  229. setterBody.push(t.expressionStatement(t.assignmentExpression("=", specifier.local, t.memberExpression(target, specifier.imported))));
  230. }
  231. }
  232. if (specifiers.exports.length) {
  233. var exportObjRef = path.scope.generateUidIdentifier("exportObj");
  234. setterBody.push(t.variableDeclaration("var", [t.variableDeclarator(exportObjRef, t.objectExpression([]))]));
  235. for (var _iterator5 = specifiers.exports, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : (0, _getIterator3.default)(_iterator5);;) {
  236. var _ref6;
  237. if (_isArray5) {
  238. if (_i5 >= _iterator5.length) break;
  239. _ref6 = _iterator5[_i5++];
  240. } else {
  241. _i5 = _iterator5.next();
  242. if (_i5.done) break;
  243. _ref6 = _i5.value;
  244. }
  245. var node = _ref6;
  246. if (t.isExportAllDeclaration(node)) {
  247. setterBody.push(buildExportAll({
  248. KEY: path.scope.generateUidIdentifier("key"),
  249. EXPORT_OBJ: exportObjRef,
  250. TARGET: target
  251. }));
  252. } else if (t.isExportSpecifier(node)) {
  253. setterBody.push(t.expressionStatement(t.assignmentExpression("=", t.memberExpression(exportObjRef, node.exported), t.memberExpression(target, node.local))));
  254. } else {}
  255. }
  256. setterBody.push(t.expressionStatement(t.callExpression(exportIdent, [exportObjRef])));
  257. }
  258. sources.push(t.stringLiteral(specifiers.key));
  259. setters.push(t.functionExpression(null, [target], t.blockStatement(setterBody)));
  260. });
  261. var moduleName = this.getModuleName();
  262. if (moduleName) moduleName = t.stringLiteral(moduleName);
  263. if (canHoist) {
  264. (0, _babelHelperHoistVariables2.default)(path, function (id) {
  265. return variableIds.push(id);
  266. });
  267. }
  268. if (variableIds.length) {
  269. beforeBody.unshift(t.variableDeclaration("var", variableIds.map(function (id) {
  270. return t.variableDeclarator(id);
  271. })));
  272. }
  273. path.traverse(reassignmentVisitor, {
  274. exports: exportNames,
  275. buildCall: buildExportCall,
  276. scope: path.scope
  277. });
  278. for (var _iterator6 = removedPaths, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : (0, _getIterator3.default)(_iterator6);;) {
  279. var _ref7;
  280. if (_isArray6) {
  281. if (_i6 >= _iterator6.length) break;
  282. _ref7 = _iterator6[_i6++];
  283. } else {
  284. _i6 = _iterator6.next();
  285. if (_i6.done) break;
  286. _ref7 = _i6.value;
  287. }
  288. var _path3 = _ref7;
  289. _path3.remove();
  290. }
  291. path.node.body = [buildTemplate({
  292. SYSTEM_REGISTER: t.memberExpression(t.identifier(state.opts.systemGlobal || "System"), t.identifier("register")),
  293. BEFORE_BODY: beforeBody,
  294. MODULE_NAME: moduleName,
  295. SETTERS: setters,
  296. SOURCES: sources,
  297. BODY: path.node.body,
  298. EXPORT_IDENTIFIER: exportIdent,
  299. CONTEXT_IDENTIFIER: contextIdent
  300. })];
  301. }
  302. }
  303. }
  304. };
  305. };
  306. var _babelHelperHoistVariables = require("babel-helper-hoist-variables");
  307. var _babelHelperHoistVariables2 = _interopRequireDefault(_babelHelperHoistVariables);
  308. var _babelTemplate = require("babel-template");
  309. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  310. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  311. 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");
  312. var buildExportAll = (0, _babelTemplate2.default)("\n for (var KEY in TARGET) {\n if (KEY !== \"default\" && KEY !== \"__esModule\") EXPORT_OBJ[KEY] = TARGET[KEY];\n }\n");
  313. var TYPE_IMPORT = "Import";
  314. module.exports = exports["default"];