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.

160 lines
4.7 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 _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. api.assertVersion(7);
  32. const {
  33. loose,
  34. allowTopLevelThis,
  35. strict,
  36. strictMode,
  37. noInterop
  38. } = options;
  39. return {
  40. name: "transform-modules-amd",
  41. pre() {
  42. this.file.set("@babel/plugin-transform-modules-*", "amd");
  43. },
  44. visitor: {
  45. CallExpression(path, state) {
  46. if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return;
  47. if (!path.get("callee").isImport()) return;
  48. let {
  49. requireId,
  50. resolveId,
  51. rejectId
  52. } = state;
  53. if (!requireId) {
  54. requireId = path.scope.generateUidIdentifier("require");
  55. state.requireId = requireId;
  56. }
  57. if (!resolveId || !rejectId) {
  58. resolveId = path.scope.generateUidIdentifier("resolve");
  59. rejectId = path.scope.generateUidIdentifier("reject");
  60. state.resolveId = resolveId;
  61. state.rejectId = rejectId;
  62. }
  63. let result = _core.types.identifier("imported");
  64. if (!noInterop) result = (0, _helperModuleTransforms.wrapInterop)(path, result, "namespace");
  65. path.replaceWith(_core.template.expression.ast`
  66. new Promise((${resolveId}, ${rejectId}) =>
  67. ${requireId}(
  68. [${(0, _utils.getImportSource)(_core.types, path.node)}],
  69. imported => ${resolveId}(${result}),
  70. ${rejectId}
  71. )
  72. )`);
  73. },
  74. Program: {
  75. exit(path, {
  76. requireId
  77. }) {
  78. if (!(0, _helperModuleTransforms.isModule)(path)) {
  79. if (requireId) {
  80. injectWrapper(path, buildAnonymousWrapper({
  81. REQUIRE: requireId
  82. }));
  83. }
  84. return;
  85. }
  86. const amdArgs = [];
  87. const importNames = [];
  88. if (requireId) {
  89. amdArgs.push(_core.types.stringLiteral("require"));
  90. importNames.push(requireId);
  91. }
  92. let moduleName = this.getModuleName();
  93. if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
  94. const {
  95. meta,
  96. headers
  97. } = (0, _helperModuleTransforms.rewriteModuleStatementsAndPrepareHeader)(path, {
  98. loose,
  99. strict,
  100. strictMode,
  101. allowTopLevelThis,
  102. noInterop
  103. });
  104. if ((0, _helperModuleTransforms.hasExports)(meta)) {
  105. amdArgs.push(_core.types.stringLiteral("exports"));
  106. importNames.push(_core.types.identifier(meta.exportName));
  107. }
  108. for (const [source, metadata] of meta.source) {
  109. amdArgs.push(_core.types.stringLiteral(source));
  110. importNames.push(_core.types.identifier(metadata.name));
  111. if (!(0, _helperModuleTransforms.isSideEffectImport)(metadata)) {
  112. const interop = (0, _helperModuleTransforms.wrapInterop)(path, _core.types.identifier(metadata.name), metadata.interop);
  113. if (interop) {
  114. const header = _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(metadata.name), interop));
  115. header.loc = metadata.loc;
  116. headers.push(header);
  117. }
  118. }
  119. headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, loose));
  120. }
  121. (0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
  122. path.unshiftContainer("body", headers);
  123. injectWrapper(path, buildWrapper({
  124. MODULE_NAME: moduleName,
  125. AMD_ARGUMENTS: _core.types.arrayExpression(amdArgs),
  126. IMPORT_NAMES: importNames
  127. }));
  128. }
  129. }
  130. }
  131. };
  132. });
  133. exports.default = _default;