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 _helperHoistVariables = require("@babel/helper-hoist-variables");
  9. var _core = require("@babel/core");
  10. var _helperModuleTransforms = require("@babel/helper-module-transforms");
  11. var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
  12. const buildTemplate = _core.template.statement(`
  13. SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
  14. "use strict";
  15. BEFORE_BODY;
  16. return {
  17. setters: SETTERS,
  18. execute: EXECUTE,
  19. };
  20. });
  21. `);
  22. const buildExportAll = _core.template.statement(`
  23. for (var KEY in TARGET) {
  24. if (KEY !== "default" && KEY !== "__esModule") EXPORT_OBJ[KEY] = TARGET[KEY];
  25. }
  26. `);
  27. const MISSING_PLUGIN_WARNING = `\
  28. WARNING: Dynamic import() transformation must be enabled using the
  29. @babel/plugin-proposal-dynamic-import plugin. Babel 8 will
  30. no longer transform import() without using that plugin.
  31. `;
  32. const MISSING_PLUGIN_ERROR = `\
  33. ERROR: Dynamic import() transformation must be enabled using the
  34. @babel/plugin-proposal-dynamic-import plugin. Babel 8
  35. no longer transforms import() without using that plugin.
  36. `;
  37. function getExportSpecifierName(node, stringSpecifiers) {
  38. if (node.type === "Identifier") {
  39. return node.name;
  40. } else if (node.type === "StringLiteral") {
  41. const stringValue = node.value;
  42. if (!(0, _helperValidatorIdentifier.isIdentifierName)(stringValue)) {
  43. stringSpecifiers.add(stringValue);
  44. }
  45. return stringValue;
  46. } else {
  47. throw new Error(`Expected export specifier to be either Identifier or StringLiteral, got ${node.type}`);
  48. }
  49. }
  50. function constructExportCall(path, exportIdent, exportNames, exportValues, exportStarTarget, stringSpecifiers) {
  51. const statements = [];
  52. if (!exportStarTarget) {
  53. if (exportNames.length === 1) {
  54. statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.stringLiteral(exportNames[0]), exportValues[0]])));
  55. } else {
  56. const objectProperties = [];
  57. for (let i = 0; i < exportNames.length; i++) {
  58. const exportName = exportNames[i];
  59. const exportValue = exportValues[i];
  60. objectProperties.push(_core.types.objectProperty(stringSpecifiers.has(exportName) ? _core.types.stringLiteral(exportName) : _core.types.identifier(exportName), 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 exportValue = exportValues[i];
  75. statements.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.identifier(exportObj), _core.types.identifier(exportName)), exportValue)));
  76. }
  77. statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.identifier(exportObj)])));
  78. }
  79. return statements;
  80. }
  81. var _default = (0, _helperPluginUtils.declare)((api, options) => {
  82. api.assertVersion(7);
  83. const {
  84. systemGlobal = "System",
  85. allowTopLevelThis = false
  86. } = options;
  87. const reassignmentVisited = new WeakSet();
  88. const reassignmentVisitor = {
  89. "AssignmentExpression|UpdateExpression"(path) {
  90. if (reassignmentVisited.has(path.node)) return;
  91. reassignmentVisited.add(path.node);
  92. const arg = path.isAssignmentExpression() ? path.get("left") : path.get("argument");
  93. if (arg.isObjectPattern() || arg.isArrayPattern()) {
  94. const exprs = [path.node];
  95. for (const name of Object.keys(arg.getBindingIdentifiers())) {
  96. if (this.scope.getBinding(name) !== path.scope.getBinding(name)) {
  97. return;
  98. }
  99. const exportedNames = this.exports[name];
  100. if (!exportedNames) continue;
  101. for (const exportedName of exportedNames) {
  102. exprs.push(this.buildCall(exportedName, _core.types.identifier(name)).expression);
  103. }
  104. }
  105. path.replaceWith(_core.types.sequenceExpression(exprs));
  106. return;
  107. }
  108. if (!arg.isIdentifier()) return;
  109. const name = arg.node.name;
  110. if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
  111. const exportedNames = this.exports[name];
  112. if (!exportedNames) return;
  113. let node = path.node;
  114. const isPostUpdateExpression = _core.types.isUpdateExpression(node, {
  115. prefix: false
  116. });
  117. if (isPostUpdateExpression) {
  118. node = _core.types.binaryExpression(
  119. node.operator[0], _core.types.unaryExpression("+", _core.types.cloneNode(
  120. 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(path, state) {
  138. if (_core.types.isImport(path.node.callee)) {
  139. if (!this.file.has("@babel/plugin-proposal-dynamic-import")) {
  140. {
  141. console.warn(MISSING_PLUGIN_WARNING);
  142. }
  143. }
  144. 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])));
  145. }
  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. const scope = path.scope;
  167. const exportIdent = scope.generateUid("export");
  168. const {
  169. contextIdent,
  170. stringSpecifiers
  171. } = state;
  172. const exportMap = Object.create(null);
  173. const modules = [];
  174. const beforeBody = [];
  175. const setters = [];
  176. const sources = [];
  177. const variableIds = [];
  178. const removedPaths = [];
  179. function addExportName(key, val) {
  180. exportMap[key] = exportMap[key] || [];
  181. exportMap[key].push(val);
  182. }
  183. function pushModule(source, key, specifiers) {
  184. let module;
  185. modules.forEach(function (m) {
  186. if (m.key === source) {
  187. module = m;
  188. }
  189. });
  190. if (!module) {
  191. modules.push(module = {
  192. key: source,
  193. imports: [],
  194. exports: []
  195. });
  196. }
  197. module[key] = module[key].concat(specifiers);
  198. }
  199. function buildExportCall(name, val) {
  200. return _core.types.expressionStatement(_core.types.callExpression(_core.types.identifier(exportIdent), [_core.types.stringLiteral(name), val]));
  201. }
  202. const exportNames = [];
  203. const exportValues = [];
  204. const body = path.get("body");
  205. for (const path of body) {
  206. if (path.isFunctionDeclaration()) {
  207. beforeBody.push(path.node);
  208. removedPaths.push(path);
  209. } else if (path.isClassDeclaration()) {
  210. variableIds.push(_core.types.cloneNode(path.node.id));
  211. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(path.node.id), _core.types.toExpression(path.node))));
  212. } else if (path.isVariableDeclaration()) {
  213. path.node.kind = "var";
  214. } else if (path.isImportDeclaration()) {
  215. const source = path.node.source.value;
  216. pushModule(source, "imports", path.node.specifiers);
  217. for (const name of Object.keys(path.getBindingIdentifiers())) {
  218. scope.removeBinding(name);
  219. variableIds.push(_core.types.identifier(name));
  220. }
  221. path.remove();
  222. } else if (path.isExportAllDeclaration()) {
  223. pushModule(path.node.source.value, "exports", path.node);
  224. path.remove();
  225. } else if (path.isExportDefaultDeclaration()) {
  226. const declar = path.node.declaration;
  227. if (_core.types.isClassDeclaration(declar)) {
  228. const id = declar.id;
  229. if (id) {
  230. exportNames.push("default");
  231. exportValues.push(scope.buildUndefinedNode());
  232. variableIds.push(_core.types.cloneNode(id));
  233. addExportName(id.name, "default");
  234. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(id), _core.types.toExpression(declar))));
  235. } else {
  236. exportNames.push("default");
  237. exportValues.push(_core.types.toExpression(declar));
  238. removedPaths.push(path);
  239. }
  240. } else if (_core.types.isFunctionDeclaration(declar)) {
  241. const id = declar.id;
  242. if (id) {
  243. beforeBody.push(declar);
  244. exportNames.push("default");
  245. exportValues.push(_core.types.cloneNode(id));
  246. addExportName(id.name, "default");
  247. } else {
  248. exportNames.push("default");
  249. exportValues.push(_core.types.toExpression(declar));
  250. }
  251. removedPaths.push(path);
  252. } else {
  253. path.replaceWith(buildExportCall("default", declar));
  254. }
  255. } else if (path.isExportNamedDeclaration()) {
  256. const declar = path.node.declaration;
  257. if (declar) {
  258. path.replaceWith(declar);
  259. if (_core.types.isFunction(declar)) {
  260. const name = declar.id.name;
  261. addExportName(name, name);
  262. beforeBody.push(declar);
  263. exportNames.push(name);
  264. exportValues.push(_core.types.cloneNode(declar.id));
  265. removedPaths.push(path);
  266. } else if (_core.types.isClass(declar)) {
  267. const name = declar.id.name;
  268. exportNames.push(name);
  269. exportValues.push(scope.buildUndefinedNode());
  270. variableIds.push(_core.types.cloneNode(declar.id));
  271. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(declar.id), _core.types.toExpression(declar))));
  272. addExportName(name, name);
  273. } else {
  274. if (_core.types.isVariableDeclaration(declar)) {
  275. declar.kind = "var";
  276. }
  277. for (const name of Object.keys(_core.types.getBindingIdentifiers(declar))) {
  278. addExportName(name, name);
  279. }
  280. }
  281. } else {
  282. const specifiers = path.node.specifiers;
  283. if (specifiers != null && specifiers.length) {
  284. if (path.node.source) {
  285. pushModule(path.node.source.value, "exports", specifiers);
  286. path.remove();
  287. } else {
  288. const nodes = [];
  289. for (const specifier of specifiers) {
  290. const {
  291. local,
  292. exported
  293. } = specifier;
  294. const binding = scope.getBinding(local.name);
  295. const exportedName = getExportSpecifierName(exported, stringSpecifiers);
  296. if (binding && _core.types.isFunctionDeclaration(binding.path.node)) {
  297. exportNames.push(exportedName);
  298. exportValues.push(_core.types.cloneNode(local));
  299. }
  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. }
  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. (0, _helperHoistVariables.default)(path, (id, name, hasInit) => {
  351. variableIds.push(id);
  352. if (!hasInit && name in exportMap) {
  353. for (const exported of exportMap[name]) {
  354. exportNames.push(exported);
  355. exportValues.push(scope.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. }
  395. }
  396. }
  397. };
  398. });
  399. exports.default = _default;
  400. //# sourceMappingURL=index.js.map