index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. {
  62. const DUAL_MODE_RUNTIME = "7.13.0";
  63. var supportsCJSDefault = (0, _helpers.hasMinVersion)(DUAL_MODE_RUNTIME, runtimeVersion);
  64. }
  65. function has(obj, key) {
  66. return Object.prototype.hasOwnProperty.call(obj, key);
  67. }
  68. if (has(options, "useBuiltIns")) {
  69. if (options.useBuiltIns) {
  70. throw new Error("The 'useBuiltIns' option has been removed. The @babel/runtime " + "module now uses builtins by default.");
  71. } else {
  72. throw new Error("The 'useBuiltIns' option has been removed. Use the 'corejs'" + "option to polyfill with `core-js` via @babel/runtime.");
  73. }
  74. }
  75. if (has(options, "polyfill")) {
  76. if (options.polyfill === false) {
  77. throw new Error("The 'polyfill' option has been removed. The @babel/runtime " + "module now skips polyfilling by default.");
  78. } else {
  79. throw new Error("The 'polyfill' option has been removed. Use the 'corejs'" + "option to polyfill with `core-js` via @babel/runtime.");
  80. }
  81. }
  82. if (has(options, "moduleName")) {
  83. 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.");
  84. }
  85. const esModules = useESModules === "auto" ? api.caller(supportsStaticESM) : useESModules;
  86. const injectCoreJS2 = corejsVersion === 2;
  87. const injectCoreJS3 = corejsVersion === 3;
  88. const moduleName = injectCoreJS3 ? "@babel/runtime-corejs3" : injectCoreJS2 ? "@babel/runtime-corejs2" : "@babel/runtime";
  89. const HEADER_HELPERS = ["interopRequireWildcard", "interopRequireDefault"];
  90. const modulePath = (0, _getRuntimePath.default)(moduleName, dirname, absoluteRuntime);
  91. function createCorejsPlgin(plugin, options, regeneratorPlugin) {
  92. return (api, _, filename) => {
  93. return Object.assign({}, plugin(api, options, filename), {
  94. inherits: regeneratorPlugin
  95. });
  96. };
  97. }
  98. function createRegeneratorPlugin(options) {
  99. if (!useRuntimeRegenerator) return undefined;
  100. return (api, _, filename) => {
  101. return pluginRegenerator(api, options, filename);
  102. };
  103. }
  104. return {
  105. name: "transform-runtime",
  106. inherits: injectCoreJS2 ? createCorejsPlgin(pluginCorejs2, {
  107. method: "usage-pure",
  108. absoluteImports: absoluteRuntime ? modulePath : false,
  109. [pluginsCompat]: {
  110. runtimeVersion,
  111. useBabelRuntime: modulePath,
  112. ext: ""
  113. }
  114. }, createRegeneratorPlugin({
  115. method: "usage-pure",
  116. absoluteImports: absoluteRuntime ? modulePath : false,
  117. [pluginsCompat]: {
  118. useBabelRuntime: modulePath
  119. }
  120. })) : injectCoreJS3 ? createCorejsPlgin(pluginCorejs3, {
  121. method: "usage-pure",
  122. version: 3,
  123. proposals,
  124. absoluteImports: absoluteRuntime ? modulePath : false,
  125. [pluginsCompat]: {
  126. useBabelRuntime: modulePath,
  127. ext: ""
  128. }
  129. }, createRegeneratorPlugin({
  130. method: "usage-pure",
  131. absoluteImports: absoluteRuntime ? modulePath : false,
  132. [pluginsCompat]: {
  133. useBabelRuntime: modulePath
  134. }
  135. })) : createRegeneratorPlugin({
  136. method: "usage-pure",
  137. absoluteImports: absoluteRuntime ? modulePath : false,
  138. [pluginsCompat]: {
  139. useBabelRuntime: modulePath
  140. }
  141. }),
  142. pre(file) {
  143. if (!useRuntimeHelpers) return;
  144. file.set("helperGenerator", name => {
  145. if (file.availableHelper && !file.availableHelper(name, runtimeVersion)) {
  146. return;
  147. }
  148. const isInteropHelper = HEADER_HELPERS.indexOf(name) !== -1;
  149. const blockHoist = isInteropHelper && !(0, _helperModuleImports.isModule)(file.path) ? 4 : undefined;
  150. const helpersDir = esModules && file.path.node.sourceType === "module" ? "helpers/esm" : "helpers";
  151. let helperPath = `${modulePath}/${helpersDir}/${name}`;
  152. if (absoluteRuntime) helperPath = (0, _getRuntimePath.resolveFSPath)(helperPath);
  153. return addDefaultImport(helperPath, name, blockHoist, true);
  154. });
  155. const cache = new Map();
  156. function addDefaultImport(source, nameHint, blockHoist, isHelper = false) {
  157. const cacheKey = (0, _helperModuleImports.isModule)(file.path);
  158. const key = `${source}:${nameHint}:${cacheKey || ""}`;
  159. let cached = cache.get(key);
  160. if (cached) {
  161. cached = _core.types.cloneNode(cached);
  162. } else {
  163. cached = (0, _helperModuleImports.addDefault)(file.path, source, {
  164. importedInterop: isHelper && supportsCJSDefault ? "compiled" : "uncompiled",
  165. nameHint,
  166. blockHoist
  167. });
  168. cache.set(key, cached);
  169. }
  170. return cached;
  171. }
  172. }
  173. };
  174. });
  175. exports.default = _default;