index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 = exports.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" + (api.types.importExpression ? "|ImportExpression" : "")](path, state) {
  49. if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return;
  50. if (path.isCallExpression() && !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) {
  68. result = (0, _helperModuleTransforms.wrapInterop)(this.file.path, result, "namespace");
  69. }
  70. path.replaceWith((0, _helperModuleTransforms.buildDynamicImport)(path.node, false, false, specifier => _core.template.expression.ast`
  71. new Promise((${resolveId}, ${rejectId}) =>
  72. ${requireId}(
  73. [${specifier}],
  74. imported => ${_core.types.cloneNode(resolveId)}(${result}),
  75. ${_core.types.cloneNode(rejectId)}
  76. )
  77. )
  78. `));
  79. },
  80. Program: {
  81. exit(path, {
  82. requireId
  83. }) {
  84. if (!(0, _helperModuleTransforms.isModule)(path)) {
  85. if (requireId) {
  86. injectWrapper(path, buildAnonymousWrapper({
  87. REQUIRE: _core.types.cloneNode(requireId)
  88. }));
  89. }
  90. return;
  91. }
  92. const amdArgs = [];
  93. const importNames = [];
  94. if (requireId) {
  95. amdArgs.push(_core.types.stringLiteral("require"));
  96. importNames.push(_core.types.cloneNode(requireId));
  97. }
  98. let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
  99. if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
  100. const {
  101. meta,
  102. headers
  103. } = (0, _helperModuleTransforms.rewriteModuleStatementsAndPrepareHeader)(path, {
  104. enumerableModuleMeta,
  105. constantReexports,
  106. strict,
  107. strictMode,
  108. allowTopLevelThis,
  109. importInterop,
  110. noInterop,
  111. filename: this.file.opts.filename
  112. });
  113. if ((0, _helperModuleTransforms.hasExports)(meta)) {
  114. amdArgs.push(_core.types.stringLiteral("exports"));
  115. importNames.push(_core.types.identifier(meta.exportName));
  116. }
  117. for (const [source, metadata] of meta.source) {
  118. amdArgs.push(_core.types.stringLiteral(source));
  119. importNames.push(_core.types.identifier(metadata.name));
  120. if (!(0, _helperModuleTransforms.isSideEffectImport)(metadata)) {
  121. const interop = (0, _helperModuleTransforms.wrapInterop)(path, _core.types.identifier(metadata.name), metadata.interop);
  122. if (interop) {
  123. const header = _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(metadata.name), interop));
  124. header.loc = metadata.loc;
  125. headers.push(header);
  126. }
  127. }
  128. headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, constantReexports));
  129. }
  130. (0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
  131. path.unshiftContainer("body", headers);
  132. injectWrapper(path, buildWrapper({
  133. MODULE_NAME: moduleName,
  134. AMD_ARGUMENTS: _core.types.arrayExpression(amdArgs),
  135. IMPORT_NAMES: importNames
  136. }));
  137. }
  138. }
  139. }
  140. };
  141. });
  142. //# sourceMappingURL=index.js.map