index.js 18 KB

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