index.js 18 KB

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