index.js 6.3 KB

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