index.js 18 KB

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