index.js 15 KB

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