index.js 18 KB

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