index.js 18 KB

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