index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. {
  139. console.warn(MISSING_PLUGIN_WARNING);
  140. }
  141. }
  142. } else {
  143. if (!this.file.has("@babel/plugin-proposal-dynamic-import")) {
  144. throw new Error(MISSING_PLUGIN_ERROR);
  145. }
  146. }
  147. 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])));
  148. },
  149. MetaProperty(path, state) {
  150. if (path.node.meta.name === "import" && path.node.property.name === "meta") {
  151. path.replaceWith(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("meta")));
  152. }
  153. },
  154. ReferencedIdentifier(path, state) {
  155. if (path.node.name === "__moduleName" && !path.scope.hasBinding("__moduleName")) {
  156. path.replaceWith(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("id")));
  157. }
  158. },
  159. Program: {
  160. enter(path, state) {
  161. state.contextIdent = path.scope.generateUid("context");
  162. state.stringSpecifiers = new Set();
  163. if (!allowTopLevelThis) {
  164. (0, _helperModuleTransforms.rewriteThis)(path);
  165. }
  166. },
  167. exit(path, state) {
  168. const scope = path.scope;
  169. const exportIdent = scope.generateUid("export");
  170. const {
  171. contextIdent,
  172. stringSpecifiers
  173. } = state;
  174. const exportMap = Object.create(null);
  175. const modules = [];
  176. const beforeBody = [];
  177. const setters = [];
  178. const sources = [];
  179. const variableIds = [];
  180. const removedPaths = [];
  181. function addExportName(key, val) {
  182. exportMap[key] = exportMap[key] || [];
  183. exportMap[key].push(val);
  184. }
  185. function pushModule(source, key, specifiers) {
  186. let module;
  187. modules.forEach(function (m) {
  188. if (m.key === source) {
  189. module = m;
  190. }
  191. });
  192. if (!module) {
  193. modules.push(module = {
  194. key: source,
  195. imports: [],
  196. exports: []
  197. });
  198. }
  199. module[key] = module[key].concat(specifiers);
  200. }
  201. function buildExportCall(name, val) {
  202. return _core.types.expressionStatement(_core.types.callExpression(_core.types.identifier(exportIdent), [_core.types.stringLiteral(name), val]));
  203. }
  204. const exportNames = [];
  205. const exportValues = [];
  206. const body = path.get("body");
  207. for (const path of body) {
  208. if (path.isFunctionDeclaration()) {
  209. beforeBody.push(path.node);
  210. removedPaths.push(path);
  211. } else if (path.isClassDeclaration()) {
  212. variableIds.push(_core.types.cloneNode(path.node.id));
  213. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(path.node.id), _core.types.toExpression(path.node))));
  214. } else if (path.isVariableDeclaration()) {
  215. path.node.kind = "var";
  216. } else if (path.isImportDeclaration()) {
  217. const source = path.node.source.value;
  218. pushModule(source, "imports", path.node.specifiers);
  219. for (const name of Object.keys(path.getBindingIdentifiers())) {
  220. scope.removeBinding(name);
  221. variableIds.push(_core.types.identifier(name));
  222. }
  223. path.remove();
  224. } else if (path.isExportAllDeclaration()) {
  225. pushModule(path.node.source.value, "exports", path.node);
  226. path.remove();
  227. } else if (path.isExportDefaultDeclaration()) {
  228. const declar = path.node.declaration;
  229. if (_core.types.isClassDeclaration(declar)) {
  230. const id = declar.id;
  231. if (id) {
  232. exportNames.push("default");
  233. exportValues.push(scope.buildUndefinedNode());
  234. variableIds.push(_core.types.cloneNode(id));
  235. addExportName(id.name, "default");
  236. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(id), _core.types.toExpression(declar))));
  237. } else {
  238. exportNames.push("default");
  239. exportValues.push(_core.types.toExpression(declar));
  240. removedPaths.push(path);
  241. }
  242. } else if (_core.types.isFunctionDeclaration(declar)) {
  243. const id = declar.id;
  244. if (id) {
  245. beforeBody.push(declar);
  246. exportNames.push("default");
  247. exportValues.push(_core.types.cloneNode(id));
  248. addExportName(id.name, "default");
  249. } else {
  250. exportNames.push("default");
  251. exportValues.push(_core.types.toExpression(declar));
  252. }
  253. removedPaths.push(path);
  254. } else {
  255. path.replaceWith(buildExportCall("default", declar));
  256. }
  257. } else if (path.isExportNamedDeclaration()) {
  258. const declar = path.node.declaration;
  259. if (declar) {
  260. path.replaceWith(declar);
  261. if (_core.types.isFunction(declar)) {
  262. const name = declar.id.name;
  263. addExportName(name, name);
  264. beforeBody.push(declar);
  265. exportNames.push(name);
  266. exportValues.push(_core.types.cloneNode(declar.id));
  267. removedPaths.push(path);
  268. } else if (_core.types.isClass(declar)) {
  269. const name = declar.id.name;
  270. exportNames.push(name);
  271. exportValues.push(scope.buildUndefinedNode());
  272. variableIds.push(_core.types.cloneNode(declar.id));
  273. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(declar.id), _core.types.toExpression(declar))));
  274. addExportName(name, name);
  275. } else {
  276. if (_core.types.isVariableDeclaration(declar)) {
  277. declar.kind = "var";
  278. }
  279. for (const name of Object.keys(_core.types.getBindingIdentifiers(declar))) {
  280. addExportName(name, name);
  281. }
  282. }
  283. } else {
  284. const specifiers = path.node.specifiers;
  285. if (specifiers != null && specifiers.length) {
  286. if (path.node.source) {
  287. pushModule(path.node.source.value, "exports", specifiers);
  288. path.remove();
  289. } else {
  290. const nodes = [];
  291. for (const specifier of specifiers) {
  292. const {
  293. local,
  294. exported
  295. } = specifier;
  296. const binding = scope.getBinding(local.name);
  297. const exportedName = getExportSpecifierName(exported, stringSpecifiers);
  298. if (binding && _core.types.isFunctionDeclaration(binding.path.node)) {
  299. exportNames.push(exportedName);
  300. exportValues.push(_core.types.cloneNode(local));
  301. } else if (!binding) {
  302. nodes.push(buildExportCall(exportedName, local));
  303. }
  304. addExportName(local.name, exportedName);
  305. }
  306. path.replaceWithMultiple(nodes);
  307. }
  308. } else {
  309. path.remove();
  310. }
  311. }
  312. }
  313. }
  314. modules.forEach(function (specifiers) {
  315. const setterBody = [];
  316. const target = scope.generateUid(specifiers.key);
  317. for (let specifier of specifiers.imports) {
  318. if (_core.types.isImportNamespaceSpecifier(specifier)) {
  319. setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.identifier(target))));
  320. } else if (_core.types.isImportDefaultSpecifier(specifier)) {
  321. specifier = _core.types.importSpecifier(specifier.local, _core.types.identifier("default"));
  322. }
  323. if (_core.types.isImportSpecifier(specifier)) {
  324. const {
  325. imported
  326. } = specifier;
  327. setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.memberExpression(_core.types.identifier(target), specifier.imported, imported.type === "StringLiteral"))));
  328. }
  329. }
  330. if (specifiers.exports.length) {
  331. const exportNames = [];
  332. const exportValues = [];
  333. let hasExportStar = false;
  334. for (const node of specifiers.exports) {
  335. if (_core.types.isExportAllDeclaration(node)) {
  336. hasExportStar = true;
  337. } else if (_core.types.isExportSpecifier(node)) {
  338. const exportedName = getExportSpecifierName(node.exported, stringSpecifiers);
  339. exportNames.push(exportedName);
  340. exportValues.push(_core.types.memberExpression(_core.types.identifier(target), node.local, _core.types.isStringLiteral(node.local)));
  341. } else {}
  342. }
  343. setterBody.push(...constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, hasExportStar ? _core.types.identifier(target) : null, stringSpecifiers));
  344. }
  345. sources.push(_core.types.stringLiteral(specifiers.key));
  346. setters.push(_core.types.functionExpression(null, [_core.types.identifier(target)], _core.types.blockStatement(setterBody)));
  347. });
  348. let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
  349. if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
  350. {
  351. var _path$scope, _path$scope$hoistVari;
  352. (_path$scope$hoistVari = (_path$scope = path.scope).hoistVariables) != null ? _path$scope$hoistVari : _path$scope.hoistVariables = require("@babel/traverse").Scope.prototype.hoistVariables;
  353. }
  354. path.scope.hoistVariables((id, hasInit) => {
  355. variableIds.push(id);
  356. if (!hasInit && id.name in exportMap) {
  357. for (const exported of exportMap[id.name]) {
  358. exportNames.push(exported);
  359. exportValues.push(_core.types.buildUndefinedNode());
  360. }
  361. }
  362. });
  363. if (variableIds.length) {
  364. beforeBody.unshift(_core.types.variableDeclaration("var", variableIds.map(id => _core.types.variableDeclarator(id))));
  365. }
  366. if (exportNames.length) {
  367. beforeBody.push(...constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, null, stringSpecifiers));
  368. }
  369. path.traverse(reassignmentVisitor, {
  370. exports: exportMap,
  371. buildCall: buildExportCall,
  372. scope
  373. });
  374. for (const path of removedPaths) {
  375. path.remove();
  376. }
  377. let hasTLA = false;
  378. path.traverse({
  379. AwaitExpression(path) {
  380. hasTLA = true;
  381. path.stop();
  382. },
  383. Function(path) {
  384. path.skip();
  385. },
  386. noScope: true
  387. });
  388. path.node.body = [buildTemplate({
  389. SYSTEM_REGISTER: _core.types.memberExpression(_core.types.identifier(systemGlobal), _core.types.identifier("register")),
  390. BEFORE_BODY: beforeBody,
  391. MODULE_NAME: moduleName,
  392. SETTERS: _core.types.arrayExpression(setters),
  393. EXECUTE: _core.types.functionExpression(null, [], _core.types.blockStatement(path.node.body), false, hasTLA),
  394. SOURCES: _core.types.arrayExpression(sources),
  395. EXPORT_IDENTIFIER: _core.types.identifier(exportIdent),
  396. CONTEXT_IDENTIFIER: _core.types.identifier(contextIdent)
  397. })];
  398. path.requeue(path.get("body.0"));
  399. }
  400. }
  401. }
  402. };
  403. });
  404. //# sourceMappingURL=index.js.map