index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 _helperModuleTransforms() {
  14. const data = require("@babel/helper-module-transforms");
  15. _helperModuleTransforms = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _helperSimpleAccess() {
  21. const data = _interopRequireDefault(require("@babel/helper-simple-access"));
  22. _helperSimpleAccess = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _core() {
  28. const data = require("@babel/core");
  29. _core = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _utils() {
  35. const data = require("babel-plugin-dynamic-import-node/utils");
  36. _utils = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  42. var _default = (0, _helperPluginUtils().declare)((api, options) => {
  43. api.assertVersion(7);
  44. const transformImportCall = (0, _utils().createDynamicImportTransform)(api);
  45. const {
  46. loose,
  47. strictNamespace = false,
  48. mjsStrictNamespace = true,
  49. allowTopLevelThis,
  50. strict,
  51. strictMode,
  52. noInterop,
  53. lazy = false,
  54. allowCommonJSExports = true
  55. } = options;
  56. if (typeof lazy !== "boolean" && typeof lazy !== "function" && (!Array.isArray(lazy) || !lazy.every(item => typeof item === "string"))) {
  57. throw new Error(`.lazy must be a boolean, array of strings, or a function`);
  58. }
  59. if (typeof strictNamespace !== "boolean") {
  60. throw new Error(`.strictNamespace must be a boolean, or undefined`);
  61. }
  62. if (typeof mjsStrictNamespace !== "boolean") {
  63. throw new Error(`.mjsStrictNamespace must be a boolean, or undefined`);
  64. }
  65. const getAssertion = localName => _core().template.expression.ast`
  66. (function(){
  67. throw new Error(
  68. "The CommonJS '" + "${localName}" + "' variable is not available in ES6 modules." +
  69. "Consider setting setting sourceType:script or sourceType:unambiguous in your " +
  70. "Babel config for this file.");
  71. })()
  72. `;
  73. const moduleExportsVisitor = {
  74. ReferencedIdentifier(path) {
  75. const localName = path.node.name;
  76. if (localName !== "module" && localName !== "exports") return;
  77. const localBinding = path.scope.getBinding(localName);
  78. const rootBinding = this.scope.getBinding(localName);
  79. if (rootBinding !== localBinding || path.parentPath.isObjectProperty({
  80. value: path.node
  81. }) && path.parentPath.parentPath.isObjectPattern() || path.parentPath.isAssignmentExpression({
  82. left: path.node
  83. }) || path.isAssignmentExpression({
  84. left: path.node
  85. })) {
  86. return;
  87. }
  88. path.replaceWith(getAssertion(localName));
  89. },
  90. AssignmentExpression(path) {
  91. const left = path.get("left");
  92. if (left.isIdentifier()) {
  93. const localName = path.node.name;
  94. if (localName !== "module" && localName !== "exports") return;
  95. const localBinding = path.scope.getBinding(localName);
  96. const rootBinding = this.scope.getBinding(localName);
  97. if (rootBinding !== localBinding) return;
  98. const right = path.get("right");
  99. right.replaceWith(_core().types.sequenceExpression([right.node, getAssertion(localName)]));
  100. } else if (left.isPattern()) {
  101. const ids = left.getOuterBindingIdentifiers();
  102. const localName = Object.keys(ids).filter(localName => {
  103. if (localName !== "module" && localName !== "exports") return false;
  104. return this.scope.getBinding(localName) === path.scope.getBinding(localName);
  105. })[0];
  106. if (localName) {
  107. const right = path.get("right");
  108. right.replaceWith(_core().types.sequenceExpression([right.node, getAssertion(localName)]));
  109. }
  110. }
  111. }
  112. };
  113. return {
  114. name: "transform-modules-commonjs",
  115. pre() {
  116. this.file.set("@babel/plugin-transform-modules-*", "commonjs");
  117. },
  118. visitor: {
  119. CallExpression(path) {
  120. if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return;
  121. if (!path.get("callee").isImport()) return;
  122. let {
  123. scope
  124. } = path;
  125. do {
  126. scope.rename("require");
  127. } while (scope = scope.parent);
  128. transformImportCall(this, path.get("callee"));
  129. },
  130. Program: {
  131. exit(path, state) {
  132. if (!(0, _helperModuleTransforms().isModule)(path)) return;
  133. path.scope.rename("exports");
  134. path.scope.rename("module");
  135. path.scope.rename("require");
  136. path.scope.rename("__filename");
  137. path.scope.rename("__dirname");
  138. if (!allowCommonJSExports) {
  139. (0, _helperSimpleAccess().default)(path, new Set(["module", "exports"]));
  140. path.traverse(moduleExportsVisitor, {
  141. scope: path.scope
  142. });
  143. }
  144. let moduleName = this.getModuleName();
  145. if (moduleName) moduleName = _core().types.stringLiteral(moduleName);
  146. const {
  147. meta,
  148. headers
  149. } = (0, _helperModuleTransforms().rewriteModuleStatementsAndPrepareHeader)(path, {
  150. exportName: "exports",
  151. loose,
  152. strict,
  153. strictMode,
  154. allowTopLevelThis,
  155. noInterop,
  156. lazy,
  157. esNamespaceOnly: typeof state.filename === "string" && /\.mjs$/.test(state.filename) ? mjsStrictNamespace : strictNamespace
  158. });
  159. for (const [source, metadata] of meta.source) {
  160. const loadExpr = _core().types.callExpression(_core().types.identifier("require"), [_core().types.stringLiteral(source)]);
  161. let header;
  162. if ((0, _helperModuleTransforms().isSideEffectImport)(metadata)) {
  163. if (metadata.lazy) throw new Error("Assertion failure");
  164. header = _core().types.expressionStatement(loadExpr);
  165. } else {
  166. const init = (0, _helperModuleTransforms().wrapInterop)(path, loadExpr, metadata.interop) || loadExpr;
  167. if (metadata.lazy) {
  168. header = _core().template.ast`
  169. function ${metadata.name}() {
  170. const data = ${init};
  171. ${metadata.name} = function(){ return data; };
  172. return data;
  173. }
  174. `;
  175. } else {
  176. header = _core().template.ast`
  177. var ${metadata.name} = ${init};
  178. `;
  179. }
  180. }
  181. header.loc = metadata.loc;
  182. headers.push(header);
  183. headers.push(...(0, _helperModuleTransforms().buildNamespaceInitStatements)(meta, metadata, loose));
  184. }
  185. (0, _helperModuleTransforms().ensureStatementsHoisted)(headers);
  186. path.unshiftContainer("body", headers);
  187. }
  188. }
  189. }
  190. };
  191. });
  192. exports.default = _default;