index.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. Object.defineProperty(exports, "defineCommonJSHook", {
  7. enumerable: true,
  8. get: function () {
  9. return _hooks.defineCommonJSHook;
  10. }
  11. });
  12. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  13. var _helperModuleTransforms = require("@babel/helper-module-transforms");
  14. var _core = require("@babel/core");
  15. var _dynamicImport = require("./dynamic-import.js");
  16. var _lazy = require("./lazy.js");
  17. var _hooks = require("./hooks.js");
  18. var _default = exports.default = (0, _helperPluginUtils.declare)((api, options) => {
  19. var _api$assumption, _api$assumption2, _api$assumption3;
  20. api.assertVersion(7);
  21. const {
  22. strictNamespace = false,
  23. mjsStrictNamespace = strictNamespace,
  24. allowTopLevelThis,
  25. strict,
  26. strictMode,
  27. noInterop,
  28. importInterop,
  29. lazy = false,
  30. allowCommonJSExports = true,
  31. loose = false
  32. } = options;
  33. const constantReexports = (_api$assumption = api.assumption("constantReexports")) != null ? _api$assumption : loose;
  34. const enumerableModuleMeta = (_api$assumption2 = api.assumption("enumerableModuleMeta")) != null ? _api$assumption2 : loose;
  35. const noIncompleteNsImportDetection = (_api$assumption3 = api.assumption("noIncompleteNsImportDetection")) != null ? _api$assumption3 : false;
  36. if (typeof lazy !== "boolean" && typeof lazy !== "function" && (!Array.isArray(lazy) || !lazy.every(item => typeof item === "string"))) {
  37. throw new Error(`.lazy must be a boolean, array of strings, or a function`);
  38. }
  39. if (typeof strictNamespace !== "boolean") {
  40. throw new Error(`.strictNamespace must be a boolean, or undefined`);
  41. }
  42. if (typeof mjsStrictNamespace !== "boolean") {
  43. throw new Error(`.mjsStrictNamespace must be a boolean, or undefined`);
  44. }
  45. const getAssertion = localName => _core.template.expression.ast`
  46. (function(){
  47. throw new Error(
  48. "The CommonJS '" + "${localName}" + "' variable is not available in ES6 modules." +
  49. "Consider setting setting sourceType:script or sourceType:unambiguous in your " +
  50. "Babel config for this file.");
  51. })()
  52. `;
  53. const moduleExportsVisitor = {
  54. ReferencedIdentifier(path) {
  55. const localName = path.node.name;
  56. if (localName !== "module" && localName !== "exports") return;
  57. const localBinding = path.scope.getBinding(localName);
  58. const rootBinding = this.scope.getBinding(localName);
  59. if (rootBinding !== localBinding || path.parentPath.isObjectProperty({
  60. value