index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 _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 = _core.template.statement(`
  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 = _core.template.statement(`
  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 reassignmentVisited = new WeakSet();
  89. const reassignmentVisitor = {
  90. "AssignmentExpression|UpdateExpression"(path) {
  91. if (reassignmentVisited.has(path.node)) return;
  92. reassignmentVisited.add(path.node);
  93. const arg = path.isAssignmentExpression() ? path.get("left") : path.get("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) continue;
  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 = _core.types.isUpdateExpression(node, {
  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. const 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.isVariableDeclaration()) {
  212. path.node.kind = "var";
  213. } else if (path.isImportDeclaration()) {
  214. const source = path.node.source.value;
  215. pushModule(source, "imports", path.node.specifiers);
  216. for (const name of Object.keys(path.getBindingIdentifiers())) {
  217. scope.removeBinding(name);
  218. variableIds.push(_core.types.identifier(name));
  219. }
  220. path.remove();
  221. } else if (path.isExportAllDeclaration()) {
  222. pushModule(path.node.source.value, "exports", path.node);
  223. path.remove();
  224. } else if (path.isExportDefaultDeclaration()) {
  225. const declar = path.get("declaration");
  226. if (declar.isClassDeclaration()) {
  227. const id = declar.node.id;
  228. if (id) {
  229. exportNames.push("default");
  230. exportValues.push(scope.buildUndefinedNode());
  231. variableIds.push(_core.types.cloneNode(id));
  232. addExportName(id.name, "default");
  233. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(id), _core.types.toExpression(declar.node))));
  234. } else {
  235. exportNames.push("default");
  236. exportValues.push(_core.types.toExpression(declar.node));
  237. removedPaths.push(path);
  238. }
  239. } else if (declar.isFunctionDeclaration()) {
  240. const id = declar.node.id;
  241. if (id) {
  242. beforeBody.push(declar.node);
  243. exportNames.push("default");
  244. exportValues.push(_core.types.cloneNode(id));
  245. addExportName(id.name, "default");
  246. } else {
  247. exportNames.push("default");
  248. exportValues.push(_core.types.toExpression(declar.node));
  249. }
  250. removedPaths.push(path);
  251. } else {
  252. path.replaceWith(buildExportCall("default", declar.node));
  253. }
  254. } else if (path.isExportNamedDeclaration()) {
  255. const declar = path.get("declaration");
  256. if (declar.node) {
  257. path.replaceWith(declar);
  258. if (declar.isFunction()) {
  259. const node = declar.node;
  260. const name = node.id.name;
  261. addExportName(name, name);
  262. beforeBody.push(node);
  263. exportNames.push(name);
  264. exportValues.push(_core.types.cloneNode(node.id));
  265. removedPaths.push(path);
  266. } else if (declar.isClass()) {
  267. const name = declar.node.id.name;
  268. exportNames.push(name);
  269. exportValues.push(scope.buildUndefinedNode());
  270. variableIds.push(_core.types.cloneNode(declar.node.id));
  271. path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(declar.node.id), _core.types.toExpression(declar.node))));
  272. addExportName(name, name);
  273. } else {
  274. if (declar.isVariableDeclaration()) {
  275. declar.node.kind = "var";
  276. }
  277. for (const name of Object.keys(declar.getBindingIdentifiers())) {
  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. } else if (!binding) {
  300. nodes.push(buildExportCall(exportedName, local));
  301. }
  302. addExportName(local.name, exportedName);
  303. }
  304. path.replaceWithMultiple(nodes);
  305. }
  306. } else {
  307. path.remove();
  308. }
  309. }
  310. }
  311. }
  312. modules.forEach(function (specifiers) {
  313. const setterBody = [];
  314. const target = scope.generateUid(specifiers.key);
  315. for (let specifier of specifiers.imports) {
  316. if (_core.types.isImportNamespaceSpecifier(specifier)) {
  317. setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.identifier(target))));
  318. } else if (_core.types.isImportDefaultSpecifier(specifier)) {
  319. specifier = _core.types.importSpecifier(specifier.local, _core.types.identifier("default"));
  320. }
  321. if (_core.types.isImportSpecifier(specifier)) {
  322. const {
  323. imported
  324. } = specifier;
  325. setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.memberExpression(_core.types.identifier(target), specifier.imported, imported.type === "StringLiteral"))));
  326. }
  327. }
  328. if (specifiers.exports.length) {
  329. const exportNames = [];
  330. const exportValues = [];
  331. let hasExportStar = false;
  332. for (const node of specifiers.exports) {
  333. if (_core.types.isExportAllDeclaration(node)) {
  334. hasExportStar = true;
  335. } else if (_core.types.isExportSpecifier(node)) {
  336. const exportedName = getExportSpecifierName(node.exported, stringSpecifiers);
  337. exportNames.push(exportedName);
  338. exportValues.push(_core.types.memberExpression(_core.types.identifier(target), node.local, _core.types.isStringLiteral(node.local)));
  339. } else {}
  340. }
  341. setterBody.push(...constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, hasExportStar ? _core.types.identifier(target) : null, stringSpecifiers));
  342. }
  343. sources.push(_core.types.stringLiteral(specifiers.key));
  344. setters.push(_core.types.functionExpression(null, [_core.types.identifier(target)], _core.types.blockStatement(setterBody)));
  345. });
  346. let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
  347. if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
  348. (0, _helperHoistVariables.default)(path, (id, name, hasInit) => {
  349. variableIds.push(id);
  350. if (!hasInit && name in exportMap) {
  351. for (const exported of exportMap[name]) {
  352. exportNames.push(exported);
  353. exportValues.push(scope.buildUndefinedNode());
  354. }
  355. }
  356. });
  357. if (variableIds.length) {
  358. beforeBody.unshift(_core.types.variableDeclaration("var", variableIds.map(id => _core.types.variableDeclarator(id))));
  359. }
  360. if (exportNames.length) {
  361. beforeBody.push(...constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, null, stringSpecifiers));
  362. }
  363. path.traverse(reassignmentVisitor, {
  364. exports: exportMap,
  365. buildCall: buildExportCall,
  366. scope
  367. });
  368. for (const path of removedPaths) {
  369. path.remove();
  370. }
  371. let hasTLA = false;
  372. path.traverse({
  373. AwaitExpression(path) {
  374. hasTLA = true;
  375. path.stop();
  376. },
  377. Function(path) {
  378. path.skip();
  379. },
  380. noScope: true
  381. });
  382. path.node.body = [buildTemplate({
  383. SYSTEM_REGISTER: _core.types.memberExpression(_core.types.identifier(systemGlobal), _core.types.identifier("register")),
  384. BEFORE_BODY: beforeBody,
  385. MODULE_NAME: moduleName,
  386. SETTERS: _core.types.arrayExpression(setters),
  387. EXECUTE: _core.types.functionExpression(null, [], _core.types.blockStatement(path.node.body), false, hasTLA),
  388. SOURCES: _core.types.arrayExpression(sources),
  389. EXPORT_IDENTIFIER: _core.types.identifier(exportIdent),
  390. CONTEXT_IDENTIFIER: _core.types.identifier(contextIdent)
  391. })];
  392. }
  393. }
  394. }
  395. };
  396. });
  397. exports.default = _default;