index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 _babelPluginPolyfillCorejs = require("babel-plugin-polyfill-corejs2");
  12. var _babelPluginPolyfillCorejs2 = require("babel-plugin-polyfill-corejs3");
  13. var _babelPluginPolyfillRegenerator = require("babel-plugin-polyfill-regenerator");
  14. const pluginCorejs2 = _babelPluginPolyfillCorejs.default || _babelPluginPolyfillCorejs;
  15. const pluginCorejs3 = _babelPluginPolyfillCorejs2.default || _babelPluginPolyfillCorejs2;
  16. const pluginRegenerator = _babelPluginPolyfillRegenerator.default || _babelPluginPolyfillRegenerator;
  17. const pluginsCompat = "#__secret_key__@babel/runtime__compatibility";
  18. function supportsStaticESM(caller) {
  19. return !!(caller != null && caller.supportsStaticESM);
  20. }
  21. var _default = (0, _helperPluginUtils.declare)((api, options, dirname) => {
  22. api.assertVersion(7);
  23. const {
  24. corejs,
  25. helpers: useRuntimeHelpers = true,
  26. regenerator: useRuntimeRegenerator = true,
  27. useESModules = false,
  28. version: runtimeVersion = "7.0.0-beta.0",
  29. absoluteRuntime = false
  30. } = options;
  31. let proposals = false;
  32. let rawVersion;
  33. if (typeof corejs === "object" && corejs !== null) {
  34. rawVersion = corejs.version;
  35. proposals = Boolean(corejs.proposals);
  36. } else {
  37. rawVersion = corejs;
  38. }
  39. const corejsVersion = rawVersion ? Number(rawVersion) : false;
  40. if (![false, 2, 3].includes(corejsVersion)) {
  41. throw new Error(`The \`core-js\` version must be false, 2 or 3, but got ${JSON.stringify(rawVersion)}.`);
  42. }
  43. if (proposals && (!corejsVersion || corejsVersion < 3)) {
  44. throw new Error("The 'proposals' option is only supported when using 'corejs: 3'");
  45. }
  46. if (typeof useRuntimeRegenerator !== "boolean") {
  47. throw new Error("The 'regenerator' option must be undefined, or a boolean.");
  48. }
  49. if (typeof useRuntimeHelpers !== "boolean") {
  50. throw new Error("The 'helpers' option must be undefined, or a boolean.");
  51. }
  52. if (typeof useESModules !== "boolean" && useESModules !== "auto") {
  53. throw new Error("The 'useESModules' option must be undefined, or a boolean, or 'auto'.");
  54. }
  55. if (typeof absoluteRuntime !== "boolean" && typeof absoluteRuntime !== "string") {
  56. throw new Error("The 'absoluteRuntime' option must be undefined, a boolean, or a string.");
  57. }
  58. if (typeof runtimeVersion !== "string") {
  59. throw new Error(`The 'version' option must be a version string.`);
  60. }
  61. const DUAL_MODE_RUNTIME = "7.13.0";
  62. const supportsCJSDefault = (0, _helpers.hasMinVersion)(DUAL_MODE_RUNTIME, runtimeVersion);
  63. function has(obj, key) {
  64. return Object.prototype.hasOwnProperty.call(obj, key);
  65. }
  66. if (has(options, "useBuiltIns")) {
  67. if (options.useBuiltIns) {
  68. throw new Error("The 'useBuiltIns' option has been removed. The @babel/runtime " + "module now uses builtins by default.");
  69. } else {
  70. throw new Error("The 'useBuiltIns' option has been removed. Use the 'corejs'" + "option to polyfill with `core-js` via @babel/runtime.");
  71. }
  72. }
  73. if (has(options, "polyfill")) {
  74. if (options.polyfill === false) {
  75. throw new Error("The 'polyfill' option has been removed. The @babel/runtime " + "module now skips polyfilling by default.");
  76. } else {
  77. throw new Error("The 'polyfill' option has been removed. Use the 'corejs'" + "option to polyfill with `core-js` via @babel/runtime.");
  78. }
  79. }
  80. if (has(options, "moduleName")) {
  81. 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.");
  82. }
  83. const esModules = useESModules === "auto" ? api.caller(supportsStaticESM) : useESModules;
  84. const injectCoreJS2 = corejsVersion === 2;
  85. const injectCoreJS3 = corejsVersion === 3;
  86. const moduleName = injectCoreJS3 ? "@babel/runtime-corejs3" : injectCoreJS2 ? "@babel/runtime-corejs2" : "@babel/runtime";
  87. const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];
  88. const modulePath = (0, _getRuntimePath.default)(moduleName, dirname, absoluteRuntime);
  89. function createCorejsPlgin(plugin, options, regeneratorPlugin) {
  90. return (api, _, filename) => {
  91. return Object.assign({}, plugin(api, options, filename), {
  92. inherits: regeneratorPlugin
  93. });
  94. };
  95. }
  96. function createRegeneratorPlugin(options) {
  97. if (!useRuntimeRegenerator) return undefined;
  98. return (api, _, filename) => {
  99. return pluginRegenerator(api, options, filename);
  100. };
  101. }
  102. return {
  103. name: "transform-runtime",
  104. inherits: injectCoreJS2 ? createCorejsPlgin(pluginCorejs2, {
  105. method: "usage-pure",
  106. absoluteImports: absoluteRuntime ? modulePath : false,
  107. [pluginsCompat]: {
  108. runtimeVersion,
  109. useBabelRuntime: modulePath,
  110. ext: ""
  111. }
  112. }, createRegeneratorPlugin({
  113. method: "usage-pure",
  114. absoluteImports: absoluteRuntime ? modulePath : false,
  115. [pluginsCompat]: {
  116. useBabelRuntime: modulePath
  117. }
  118. })) : injectCoreJS3 ? createCorejsPlgin(pluginCorejs3, {
  119. method: "usage-pure",
  120. version: 3,
  121. proposals,
  122. absoluteImports: absoluteRuntime ? modulePath : false,
  123. [pluginsCompat]: {
  124. useBabelRuntime: modulePath,
  125. ext: ""
  126. }
  127. }, createRegeneratorPlugin({
  128. method: "usage-pure",
  129. absoluteImports: absoluteRuntime ? modulePath : false,
  130. [pluginsCompat]: {
  131. useBabelRuntime: modulePath
  132. }
  133. })) : createRegeneratorPlugin({
  134. method: "usage-pure",
  135. absoluteImports: absoluteRuntime ? modulePath : false,
  136. [pluginsCompat]: {
  137. useBabelRuntime: modulePath
  138. }
  139. }),
  140. pre(file) {
  141. if (!useRuntimeHelpers) return;
  142. file.set("helperGenerator", name => {
  143. if (file.availableHelper && !file.availableHelper(name, runtimeVersion)) {
  144. return;
  145. }
  146. const isInteropHelper = HEADER_HELPERS.indexOf(name) !== -1;
  147. const blockHoist = isInteropHelper && !(0, _helperModuleImports.isModule)(file.path) ? 4 : undefined;
  148. const helpersDir = esModules && file.path.node.sourceType === "module" ? "helpers/esm" : "helpers";
  149. let helperPath = `${modulePath}/${helpersDir}/${name}`;
  150. if (absoluteRuntime) helperPath = (0, _getRuntimePath.resolveFSPath)(helperPath);
  151. return addDefaultImport(helperPath, name, blockHoist, true);
  152. });
  153. const cache = new Map();
  154. function addDefaultImport(source, nameHint, blockHoist, isHelper = false) {
  155. const cacheKey = (0, _helperModuleImports.isModule)(file.path);
  156. const key = `${source}:${nameHint}:${cacheKey || ""}`;
  157. let cached = cache.get(key);
  158. if (cached) {
  159. cached = _core.types.cloneNode(cached);
  160. } else {
  161. cached = (0, _helperModuleImports.addDefault)(file.path, source, {
  162. importedInterop: isHelper && supportsCJSDefault ? "compiled" : "uncompiled",
  163. nameHint,
  164. blockHoist
  165. });
  166. cache.set(key, cached);
  167. }
  168. return cached;
  169. }
  170. }
  171. };
  172. });
  173. exports.default = _default;