index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. const buildWrapper = _core.template.statement(`
  10. define(MODULE_NAME, AMD_ARGUMENTS, function(IMPORT_NAMES) {
  11. })
  12. `);
  13. const buildAnonymousWrapper = _core.template.statement(`
  14. define(["require"], function(REQUIRE) {
  15. })
  16. `);
  17. function injectWrapper(path, wrapper) {
  18. const {
  19. body,
  20. directives
  21. } = path.node;
  22. path.node.directives = [];
  23. path.node.body = [];
  24. const amdFactoryCall = path.pushContainer("body", wrapper)[0].get("expression");
  25. const amdFactoryCallArgs = amdFactoryCall.get("arguments");
  26. const amdFactory = amdFactoryCallArgs[amdFactoryCallArgs.length - 1].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, _helperModuleTransforms.getDynamicImportSource)(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. filename: this.file.opts.filename
  109. });
  110. if ((0, _helperModuleTransforms.hasExports)(meta)) {
  111. amdArgs.push(_core.types.stringLiteral("exports"));
  112. importNames.push(_core.types.identifier(meta.exportName));
  113. }
  114. for (const [source, metadata] of meta.source) {
  115. amdArgs.push(_core.types.stringLiteral(source));
  116. importNames.push(_core.types.identifier(metadata.name));
  117. if (!(0, _helperModuleTransforms.isSideEffectImport)(metadata)) {
  118. const interop = (0, _helperModuleTransforms.wrapInterop)(path, _core.types.identifier(metadata.name), metadata.interop);
  119. if (interop) {
  120. const header = _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(metadata.name), interop));
  121. header.loc = metadata.loc;
  122. headers.push(header);
  123. }
  124. }
  125. headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, constantReexports));
  126. }
  127. (0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
  128. path.unshiftContainer("body", headers);
  129. injectWrapper(path, buildWrapper({
  130. MODULE_NAME: moduleName,
  131. AMD_ARGUMENTS: _core.types.arrayExpression(amdArgs),
  132. IMPORT_NAMES: importNames
  133. }));
  134. }
  135. }
  136. }
  137. };
  138. });
  139. exports.default = _default;
  140. //# sourceMappingURL=index.js.map