index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 _helperModuleTransforms = require("@babel/helper-module-transforms");
  8. var _core = require("@babel/core");
  9. var _utils = require("babel-plugin-dynamic-import-node/utils");
  10. const buildWrapper = (0, _core.template)(`
  11. define(MODULE_NAME, AMD_ARGUMENTS, function(IMPORT_NAMES) {
  12. })
  13. `);
  14. const buildAnonymousWrapper = (0, _core.template)(`
  15. define(["require"], function(REQUIRE) {
  16. })
  17. `);
  18. function injectWrapper(path, wrapper) {
  19. const {
  20. body,
  21. directives
  22. } = path.node;
  23. path.node.directives = [];
  24. path.node.body = [];
  25. const amdWrapper = path.pushContainer("body", wrapper)[0];
  26. const amdFactory = amdWrapper.get("expression.arguments").filter(arg => arg.isFunctionExpression())[0].get("body");
  27. amdFactory.pushContainer("directives", directives);
  28. amdFactory.pushContainer("body", body);
  29. }
  30. var _default = (0, _helperPluginUtils.declare)((api, options) => {
  31. var _api$assumption, _api$assumption2;
  32. api.assertVersion(7);
  33. const {
  34. allowTopLevelThis,
  35. strict,
  36. strictMode,
  37. importInterop,
  38. noInterop
  39. } = options;
  40. const constantReexports = (_api$assumption = api.assumption("constantReexports")) != null ? _api$assumption : options.loose;
  41. const enumerableModuleMeta = (_api$assumption2 = api.assumption("enumerableModuleMeta")) != null ? _api$assumption2 : options.loose;
  42. return {
  43. name: "transform-modules-amd",
  44. pre() {
  45. this.file.set("@babel/plugin-transform-modules-*", "amd");
  46. },
  47. visitor: {
  48. CallExpression(path, state) {
  49. if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return;
  50. if (!path.get("callee").isImport()) return;
  51. let {
  52. requireId,
  53. resolveId,
  54. rejectId
  55. } = state;
  56. if (!requireId) {
  57. requireId = path.scope.generateUidIdentifier("require");
  58. state.requireId = requireId;
  59. }
  60. if (!resolveId || !rejectId) {
  61. resolveId = path.scope.generateUidIdentifier("resolve");
  62. rejectId = path.scope.generateUidIdentifier("reject");
  63. state.resolveId = resolveId;
  64. state.rejectId = rejectId;
  65. }
  66. let result = _core.types.identifier("imported");
  67. if (!noInterop) result = (0, _helperModuleTransforms.wrapInterop)(path, result, "namespace");
  68. path.replaceWith(_core.template.expression.ast`
  69. new Promise((${resolveId}, ${rejectId}) =>
  70. ${requireId}(
  71. [${(0, _utils.getImportSource)(_core.types, path.node)}],
  72. imported => ${_core.types.cloneNode(resolveId)}(${result}),
  73. ${_core.types.cloneNode(rejectId)}
  74. )
  75. )`);
  76. },
  77. Program: {
  78. exit(path, {
  79. requireId
  80. }) {
  81. if (!(0, _helperModuleTransforms.isModule)(path)) {
  82. if (requireId) {
  83. injectWrapper(path, buildAnonymousWrapper({
  84. REQUIRE: _core.types.cloneNode(requireId)
  85. }));
  86. }
  87. return;
  88. }
  89. const amdArgs = [];
  90. const importNames = [];
  91. if (requireId) {
  92. amdArgs.push(_core.types.stringLiteral("require"));
  93. importNames.push(_core.types.cloneNode(requireId));
  94. }
  95. let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
  96. if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
  97. const {
  98. meta,
  99. headers
  100. } = (0, _helperModuleTransforms.rewriteModuleStatementsAndPrepareHeader)(path, {
  101. enumerableModuleMeta,
  102. constantReexports,
  103. strict,
  104. strictMode,
  105. allowTopLevelThis,
  106. importInterop,
  107. noInterop
  108. });
  109. if ((0, _helperModuleTransforms.hasExports)(meta)) {
  110. amdArgs.push(_core.types.stringLiteral("exports"));
  111. importNames.push(_core.types.identifier(meta.exportName));
  112. }
  113. for (const [source, metadata] of meta.source) {
  114. amdArgs.push(_core.types.stringLiteral(source));
  115. importNames.push(_core.types.identifier(metadata.name));
  116. if (!(0, _helperModuleTransforms.isSideEffectImport)(metadata)) {
  117. const interop = (0, _helperModuleTransforms.wrapInterop)(path, _core.types.identifier(metadata.name), metadata.interop);
  118. if (interop) {
  119. const header = _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(metadata.name), interop));
  120. header.loc = metadata.loc;
  121. headers.push(header);
  122. }
  123. }
  124. headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, constantReexports));
  125. }
  126. (0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
  127. path.unshiftContainer("body", headers);
  128. injectWrapper(path, buildWrapper({
  129. MODULE_NAME: moduleName,
  130. AMD_ARGUMENTS: _core.types.arrayExpression(amdArgs),
  131. IMPORT_NAMES: importNames
  132. }));
  133. }
  134. }
  135. }
  136. };
  137. });
  138. exports.default = _default;