index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 _helperModuleImports = require("@babel/helper-module-imports");
  8. var _core = require("@babel/core");
  9. var _helpers = require("./helpers");
  10. var _getRuntimePath = require("./get-runtime-path");
  11. var _polyfills = require("./polyfills");
  12. function supportsStaticESM(caller) {
  13. return !!(caller != null && caller.supportsStaticESM);
  14. }
  15. var _default = (0, _helperPluginUtils.declare)((api, options, dirname) => {
  16. api.assertVersion(7);
  17. const {
  18. helpers: useRuntimeHelpers = true,
  19. useESModules = false,
  20. version: runtimeVersion = "7.0.0-beta.0",
  21. absoluteRuntime = false
  22. } = options;
  23. if (typeof useRuntimeHelpers !== "boolean") {
  24. throw new Error("The 'helpers' option must be undefined, or a boolean.");
  25. }
  26. if (typeof useESModules !== "boolean" && useESModules !== "auto") {
  27. throw new Error("The 'useESModules' option must be undefined, or a boolean, or 'auto'.");
  28. }
  29. if (typeof absoluteRuntime !== "boolean" && typeof absoluteRuntime !== "string") {
  30. throw new Error("The 'absoluteRuntime' option must be undefined, a boolean, or a string.");
  31. }
  32. if (typeof runtimeVersion !== "string") {
  33. throw new Error(`The 'version' option must be a version string.`);
  34. }
  35. {
  36. const DUAL_MODE_RUNTIME = "7.13.0";
  37. var supportsCJSDefault = (0, _helpers.hasMinVersion)(DUAL_MODE_RUNTIME, runtimeVersion);
  38. }
  39. function has(obj, key) {
  40. return Object.prototype.hasOwnProperty.call(obj, key);
  41. }
  42. if (has(options, "useBuiltIns")) {
  43. if (options["useBuiltIns"]) {
  44. throw new Error("The 'useBuiltIns' option has been removed. The @babel/runtime " + "module now uses builtins by default.");
  45. } else {
  46. throw new Error("The 'useBuiltIns' option has been removed. Use the 'corejs'" + "option to polyfill with `core-js` via @babel/runtime.");
  47. }
  48. }
  49. if (has(options, "polyfill")) {
  50. if (options["polyfill"] === false) {
  51. throw new Error("The 'polyfill' option has been removed. The @babel/runtime " + "module now skips polyfilling by default.");
  52. } else {
  53. throw new Error("The 'polyfill' option has been removed. Use the 'corejs'" + "option to polyfill with `core-js` via @babel/runtime.");
  54. }
  55. }
  56. if (has(options, "moduleName")) {
  57. throw new Error("The 'moduleName' option has been removed. @babel/transform-runtime " + "no longer supports arbitrary runtimes. If you were using this to " + "set an absolute path for Babel's standard runtimes, please use the " + "'absoluteRuntime' option.");
  58. }
  59. const esModules = useESModules === "auto" ? api.caller(supportsStaticESM) : useESModules;
  60. const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];
  61. return {
  62. name: "transform-runtime",
  63. inherits: (0, _polyfills.createBasePolyfillsPlugin)(options, runtimeVersion, absoluteRuntime),
  64. pre(file) {
  65. if (!useRuntimeHelpers) return;
  66. let modulePath;
  67. file.set("helperGenerator", name => {
  68. var _modulePath, _file$get;
  69. (_modulePath = modulePath) != null ? _modulePath : modulePath = (0, _getRuntimePath.default)((_file$get = file.get("runtimeHelpersModuleName")) != null ? _file$get : "@babel/runtime", dirname, absoluteRuntime);
  70. {
  71. if (!(file.availableHelper != null && file.availableHelper(name, runtimeVersion))) {
  72. if (name === "regeneratorRuntime") {
  73. return _core.types.arrowFunctionExpression([], _core.types.identifier("regeneratorRuntime"));
  74. }
  75. return;
  76. }
  77. }
  78. const isInteropHelper = HEADER_HELPERS.indexOf(name) !== -1;
  79. const blockHoist = isInteropHelper && !(0, _helperModuleImports.isModule)(file.path) ? 4 : undefined;
  80. const helpersDir = esModules && file.path.node.sourceType === "module" ? "helpers/esm" : "helpers";
  81. let helperPath = `${modulePath}/${helpersDir}/${name}`;
  82. if (absoluteRuntime) helperPath = (0, _getRuntimePath.resolveFSPath)(helperPath);
  83. return addDefaultImport(helperPath, name, blockHoist, true);
  84. });
  85. const cache = new Map();
  86. function addDefaultImport(source, nameHint, blockHoist, isHelper = false) {
  87. const cacheKey = (0, _helperModuleImports.isModule)(file.path);
  88. const key = `${source}:${nameHint}:${cacheKey || ""}`;
  89. let cached = cache.get(key);
  90. if (cached) {
  91. cached = _core.types.cloneNode(cached);
  92. } else {
  93. cached = (0, _helperModuleImports.addDefault)(file.path, source, {
  94. importedInterop: isHelper && supportsCJSDefault ? "compiled" : "uncompiled",
  95. nameHint,
  96. blockHoist
  97. });
  98. cache.set(key, cached);
  99. }
  100. return cached;
  101. }
  102. }
  103. };
  104. });
  105. exports.default = _default;
  106. //# sourceMappingURL=index.js.map