index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  7. var _helperHoistVariables = _interopRequireDefault(require("@babel/helper-hoist-variables"));
  8. var _core = require("@babel/core");
  9. var _utils = require("babel-plugin-dynamic-import-node/utils");
  10. var _helperModuleTransforms = require("@babel/helper-module-transforms");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. const buildTemplate = (0, _core.template)(`
  13. SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
  14. "use strict";
  15. BEFORE_BODY;
  16. return {
  17. setters: SETTERS,
  18. execute: function () {
  19. BODY;
  20. }
  21. };
  22. });
  23. `);
  24. const buildExportAll = (0, _core.template)(`
  25. for (var KEY in TARGET) {
  26. if (KEY !== "default" && KEY !== "__esModule") EXPORT_OBJ[KEY] = TARGET[KEY];
  27. }
  28. `);
  29. const MISSING_PLUGIN_WARNING = `\
  30. WARNING: Dynamic import() transformation must be enabled using the
  31. @babel/plugin-proposal-dynamic-import plugin. Babel 8 will
  32. no longer transform import() without using that plugin.
  33. `;
  34. function constructExportCall(path, exportIdent, exportNames, exportValues, exportStarTarget) {
  35. const statements = [];
  36. if (exportNames.length === 1) {
  37. statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.stringLiteral(exportNames[0]), exportValues[0]])));
  38. } else if (!exportStarTarget) {
  39. const objectProperties = [];
  40. for (let i = 0; i < exportNames.length; i++) {
  41. const exportName = exportNames[i];
  42. const exportValue = exportValues[i];
  43. objectProperties.push(_core.types.objectProperty(_core.types.identifier(exportName), exportValue));
  44. }
  45. statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.objectExpression(objectProperties)])));
  46. } else {
  47. const exportObj = path.scope.generateUid("exportObj");
  48. statements.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(exportObj), _core.types.objectExpression([]))]));
  49. statements.push(buildExportAll({
  50. KEY: path.scope.generateUidIdentifier("key"),
  51. EXPORT_OBJ: _core.types.identifier(exportObj),
  52. TARGET: exportStarTarget
  53. }));
  54. for (let i = 0; i < exportNames.length; i++) {
  55. const exportName = exportNames[i];
  56. const exportValue = exportValues[i];
  57. statements.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.identifier(exportObj), _core.types.identifier(exportName)), exportValue)));
  58. }
  59. statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.identifier(exportObj)])));
  60. }
  61. return statements;
  62. }
  63. var _default = (0, _helperPluginUtils.declare)((api, options) => {
  64. api.assertVersion(7);
  65. const {
  66. systemGlobal = "System",
  67. allowTopLevelThis = false
  68. } = options;
  69. const IGNORE_REASSIGNMENT_SYMBOL = Symbol();
  70. const reassignmentVisitor = {
  71. "AssignmentExpression|UpdateExpression"(path) {
  72. if (path.node[IGNORE_REASSIGNMENT_SYMBOL]) return;
  73. path.node[IGNORE_REASSIGNMENT_SYMBOL] = true;
  74. const arg = path.get(path.isAssignmentExpression() ? "left" : "argument");
  75. if (arg.isObjectPattern() || arg.isArrayPattern()) {
  76. const exprs = [path.node];
  77. for (const name of Object.keys(arg.getBindingIdentifiers())) {
  78. if (this.scope.getBinding(name) !== path.scope.getBinding(name)) {
  79. return;
  80. }
  81. const exportedNames = this.exports[name];
  82. if (!exportedNames) return;
  83. for (const exportedName of exportedNames) {
  84. exprs.push(this.buildCall(exportedName, _core.types.identifier(name)).expression);
  85. }
  86. }
  87. path.replaceWith(_core.types.sequenceExpression(exprs));
  88. return;
  89. }
  90. if (!arg.isIdentifier()) return;
  91. const name = arg.node.name;
  92. if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
  93. const exportedNames = this.exports[name];
  94. if (!exportedNames) return;
  95. let node = path.node;
  96. const isPostUpdateExpression = path.isUpdateExpression({
  97. prefix: false
  98. });
  99. if (isPostUpdateExpression) {
  100. node = _core.types.binaryExpression(node.operator[0], _core.types.unaryExpression("+", _core.types.cloneNode(node.argument)), _core.types.numericLiteral(1));
  101. }
  102. for (const exportedName of exportedNames) {
  103. node = this.buildCall(exportedName, node).expression;
  104. }
  105. if (isPostUpdateExpression) {
  106. node = _core.types.sequenceExpression([node, path.node]);
  107. }
  108. path.replaceWith(node);
  109. }
  110. };
  111. return {
  112. name: "transform-modules-systemjs",
  113. pre() {
  114. this.file.set("@babel/plugin-transform-modules-*", "systemjs");
  115. },
  116. visitor: {
  117. CallExpression(path, state) {
  118. if (_core.types.isImport(path.node.callee)) {
  119. if (!this.file.has("@babel/plugin-proposal-dynamic-import")) {
  120. console.warn(MISSING_PLUGIN_WARNING);
  121. }
  122. path.replaceWith(_core.types.callExpression(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("import")), [(0, _utils.getImportSource)(_core.types, path.node)]));
  123. }
  124. },
  125. MetaProperty(path, state) {
  126. if (path.node.meta.name === "import" && path.node.property.name === "meta") {
  127. path.replaceWith(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("meta")));
  128. }
  129. },
  130. ReferencedIdentifier(path, state) {
  131. if (path.node.name === "__moduleName" && !path.scope.hasBinding("__moduleName")) {
  132. path.replaceWith(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("id")));
  133. }
  134. },
  135. Program: {
  136. enter(path, state) {
  137. state.contextIdent = path.scope.generateUid("context");
  138. if (!allowTopLevelThis) {
  139. (0, _helperModuleTransforms.rewriteThis)(path);
  140. }
  141. },
  142. exit(path, state) {
  143. const undefinedIdent = path.scope.buildUndefinedNode();
  144. const exportIdent = path.scope.generateUid("export");
  145. const contextIdent = state.contextIdent;
  146. const exportMap = Object.create(null);
  147. const modules = [];
  148. let beforeBody = [];
  149. const setters = [];
  150. const sources = [];
  151. const variableIds = [];
  152. const removedPaths = [];
  153. function addExportName(key, val) {
  154. exportMap[key] = exportMap[key] || [];
  155. exportMap[key].push(val);
  156. }
  157. function pushModule(source, key, specifiers) {
  158. let module;
  159. modules.forEach(function (m) {
  160. if (m.key === source) {
  161. module = m;
  162. }
  163. });
  164. if (!module) {
  165. modules.push(module = {
  166. key: source,
  167. imports: [],
  168. exports: []
  169. });
  170. }
  171. module[key] = module[key].concat(specifiers);
  172. }
  173. function buildExportCall(name, val) {
  174. return _core.types.expressionStatement(_core.types.callExpression(_core.types.identifier(exportIdent), [_core.types.stringLiteral(name), val]));
  175. }
  176. const exportNames = [];
  177. const exportValues = [];
  178. const body = path.get("body");
  179. for (const path of body) {
  180. if (path.isFunctionDeclaration()) {
  181. beforeBody.push(path.node);
  182. removedPaths.push(path);
  183. } else if (path.isClassDeclaration()) {
  184. variableIds.push(path.node.id);
  185. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(path.node.id), _core.types.toExpression(path.node))));
  186. } else if (path.isImportDeclaration()) {
  187. const source = path.node.source.value;
  188. pushModule(source, "imports", path.node.specifiers);
  189. for (const name of Object.keys(path.getBindingIdentifiers())) {
  190. path.scope.removeBinding(name);
  191. variableIds.push(_core.types.identifier(name));
  192. }
  193. path.remove();
  194. } else if (path.isExportAllDeclaration()) {
  195. pushModule(path.node.source.value, "exports", path.node);
  196. path.remove();
  197. } else if (path.isExportDefaultDeclaration()) {
  198. const declar = path.get("declaration");
  199. const id = declar.node.id;
  200. if (declar.isClassDeclaration()) {
  201. if (id) {
  202. exportNames.push("default");
  203. exportValues.push(undefinedIdent);
  204. variableIds.push(id);
  205. addExportName(id.name, "default");
  206. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(id), _core.types.toExpression(declar.node))));
  207. } else {
  208. exportNames.push("default");
  209. exportValues.push(_core.types.toExpression(declar.node));
  210. removedPaths.push(path);
  211. }
  212. } else if (declar.isFunctionDeclaration()) {
  213. if (id) {
  214. beforeBody.push(declar.node);
  215. exportNames.push("default");
  216. exportValues.push(_core.types.cloneNode(id));
  217. addExportName(id.name, "default");
  218. } else {
  219. exportNames.push("default");
  220. exportValues.push(_core.types.toExpression(declar.node));
  221. }
  222. removedPaths.push(path);
  223. } else {
  224. path.replaceWith(buildExportCall("default", declar.node));
  225. }
  226. } else if (path.isExportNamedDeclaration()) {
  227. const declar = path.get("declaration");
  228. if (declar.node) {
  229. path.replaceWith(declar);
  230. if (path.isFunction()) {
  231. const node = declar.node;
  232. const name = node.id.name;
  233. addExportName(name, name);
  234. beforeBody.push(node);
  235. exportNames.push(name);
  236. exportValues.push(_core.types.cloneNode(node.id));
  237. removedPaths.push(path);
  238. } else if (path.isClass()) {
  239. const name = declar.node.id.name;
  240. exportNames.push(name);
  241. exportValues.push(undefinedIdent);
  242. variableIds.push(declar.node.id);
  243. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(declar.node.id), _core.types.toExpression(declar.node))));
  244. addExportName(name, name);
  245. } else {
  246. for (const name of Object.keys(declar.getBindingIdentifiers())) {
  247. addExportName(name, name);
  248. }
  249. }
  250. } else {
  251. const specifiers = path.node.specifiers;
  252. if (specifiers && specifiers.length) {
  253. if (path.node.source) {
  254. pushModule(path.node.source.value, "exports", specifiers);
  255. path.remove();
  256. } else {
  257. const nodes = [];
  258. for (const specifier of specifiers) {
  259. const binding = path.scope.getBinding(specifier.local.name);
  260. if (binding && _core.types.isFunctionDeclaration(binding.path.node)) {
  261. exportNames.push(specifier.exported.name);
  262. exportValues.push(_core.types.cloneNode(specifier.local));
  263. } else if (!binding) {
  264. nodes.push(buildExportCall(specifier.exported.name, specifier.local));
  265. }
  266. addExportName(specifier.local.name, specifier.exported.name);
  267. }
  268. path.replaceWithMultiple(nodes);
  269. }
  270. } else {
  271. path.remove();
  272. }
  273. }
  274. }
  275. }
  276. modules.forEach(function (specifiers) {
  277. let setterBody = [];
  278. const target = path.scope.generateUid(specifiers.key);
  279. for (let specifier of specifiers.imports) {
  280. if (_core.types.isImportNamespaceSpecifier(specifier)) {
  281. setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.identifier(target))));
  282. } else if (_core.types.isImportDefaultSpecifier(specifier)) {
  283. specifier = _core.types.importSpecifier(specifier.local, _core.types.identifier("default"));
  284. }
  285. if (_core.types.isImportSpecifier(specifier)) {
  286. setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.memberExpression(_core.types.identifier(target), specifier.imported))));
  287. }
  288. }
  289. if (specifiers.exports.length) {
  290. const exportNames = [];
  291. const exportValues = [];
  292. let hasExportStar = false;
  293. for (const node of specifiers.exports) {
  294. if (_core.types.isExportAllDeclaration(node)) {
  295. hasExportStar = true;
  296. } else if (_core.types.isExportSpecifier(node)) {
  297. exportNames.push(node.exported.name);
  298. exportValues.push(_core.types.memberExpression(_core.types.identifier(target), node.local));
  299. } else {}
  300. }
  301. setterBody = setterBody.concat(constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, hasExportStar ? _core.types.identifier(target) : null));
  302. }
  303. sources.push(_core.types.stringLiteral(specifiers.key));
  304. setters.push(_core.types.functionExpression(null, [_core.types.identifier(target)], _core.types.blockStatement(setterBody)));
  305. });
  306. let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
  307. if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
  308. (0, _helperHoistVariables.default)(path, (id, name, hasInit) => {
  309. variableIds.push(id);
  310. if (!hasInit) {
  311. exportNames.push(name);
  312. exportValues.push(undefinedIdent);
  313. }
  314. }, null);
  315. if (variableIds.length) {
  316. beforeBody.unshift(_core.types.variableDeclaration("var", variableIds.map(id => _core.types.variableDeclarator(id))));
  317. }
  318. if (exportNames.length) {
  319. beforeBody = beforeBody.concat(constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, null));
  320. }
  321. path.traverse(reassignmentVisitor, {
  322. exports: exportMap,
  323. buildCall: buildExportCall,
  324. scope: path.scope
  325. });
  326. for (const path of removedPaths) {
  327. path.remove();
  328. }
  329. path.node.body = [buildTemplate({
  330. SYSTEM_REGISTER: _core.types.memberExpression(_core.types.identifier(systemGlobal), _core.types.identifier("register")),
  331. BEFORE_BODY: beforeBody,
  332. MODULE_NAME: moduleName,
  333. SETTERS: _core.types.arrayExpression(setters),
  334. SOURCES: _core.types.arrayExpression(sources),
  335. BODY: path.node.body,
  336. EXPORT_IDENTIFIER: _core.types.identifier(exportIdent),
  337. CONTEXT_IDENTIFIER: _core.types.identifier(contextIdent)
  338. })];
  339. }
  340. }
  341. }
  342. };
  343. });
  344. exports.default = _default;