You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

404 lines
16 KiB

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