polyfills.cjs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. ;
  2. const pluginCorejs2 = require("babel-plugin-polyfill-corejs2").default;
  3. const pluginRegenerator = require("babel-plugin-polyfill-regenerator").default;
  4. const pluginsCompat = "#__secret_key__@babel/runtime__compatibility";
  5. function createCorejs2Plugin(options) {
  6. return (api, _, filename) => pluginCorejs2(api, options, filename);
  7. }
  8. function createRegeneratorPlugin(options, useRuntimeRegenerator, corejsPlugin) {
  9. if (!useRuntimeRegenerator) return corejsPlugin != null ? corejsPlugin : undefined;
  10. return (api, _, filename) => {
  11. return Object.assign({}, pluginRegenerator(api, options, filename), {
  12. inherits: corejsPlugin != null ? corejsPlugin : undefined
  13. });
  14. };
  15. }
  16. module.exports = function createBasePolyfillsPlugin({
  17. corejs,
  18. regenerator = true
  19. }, runtimeVersion, absoluteImports, corejs3Plugin) {
  20. let proposals = false;
  21. let rawVersion;
  22. if (typeof corejs === "object" && corejs !== null) {
  23. rawVersion = corejs.version;
  24. proposals = Boolean(corejs.proposals);
  25. } else {
  26. rawVersion = corejs;
  27. }
  28. const corejsVersion = rawVersion ? Number(rawVersion) : false;
  29. if (![false, 2, 3].includes(corejsVersion)) {
  30. throw new Error(`The \`core-js\` version must be false, 2 or 3, but got ${JSON.stringify(rawVersion)}.`);
  31. }
  32. if (proposals && (!corejsVersion || corejsVersion < 3)) {
  33. throw new Error("The 'proposals' option is only supported when using 'corejs: 3'");
  34. }
  35. if (typeof regenerator !== "boolean") {
  36. throw new Error("The 'regenerator' option must be undefined, or a boolean.");
  37. }
  38. const polyfillOpts = {
  39. method: "usage-pure",
  40. absoluteImports,
  41. [pluginsCompat]: {
  42. useBabelRuntime: true,
  43. runtimeVersion,
  44. ext: ""
  45. }
  46. };
  47. return createRegeneratorPlugin(polyfillOpts, regenerator, corejsVersion === 2 ? createCorejs2Plugin(polyfillOpts) : corejsVersion === 3 ? corejs3Plugin : null);
  48. };
  49. //# sourceMappingURL=polyfills.cjs.map