index.js 4.5 KB

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