normalize-options.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.normalizeCoreJSOption = normalizeCoreJSOption;
  6. exports.default = normalizeOptions;
  7. exports.validateUseBuiltInsOption = exports.validateModulesOption = exports.checkDuplicateIncludeExcludes = exports.normalizePluginName = void 0;
  8. var _data = require("core-js-compat/data.json");
  9. var _semver = require("semver");
  10. var _corejs2BuiltIns = require("@babel/compat-data/corejs2-built-ins");
  11. var _pluginsCompatData = require("./plugins-compat-data");
  12. var _moduleTransformations = require("./module-transformations");
  13. var _options = require("./options");
  14. var _helperValidatorOption = require("@babel/helper-validator-option");
  15. const corejs2DefaultWebIncludes = ["web.timers", "web.immediate", "web.dom.iterable"];
  16. const v = new _helperValidatorOption.OptionValidator("@babel/preset-env");
  17. const allPluginsList = Object.keys(_pluginsCompatData.plugins);
  18. const modulePlugins = ["proposal-dynamic-import", ...Object.keys(_moduleTransformations.default).map(m => _moduleTransformations.default[m])];
  19. const getValidIncludesAndExcludes = (type, corejs) => new Set([...allPluginsList, ...(type === "exclude" ? modulePlugins : []), ...(corejs ? corejs == 2 ? [...Object.keys(_corejs2BuiltIns), ...corejs2DefaultWebIncludes] : Object.keys(_data) : [])]);
  20. const pluginToRegExp = plugin => {
  21. if (plugin instanceof RegExp) return plugin;
  22. try {
  23. return new RegExp(`^${normalizePluginName(plugin)}$`);
  24. } catch (e) {
  25. return null;
  26. }
  27. };
  28. const selectPlugins = (regexp, type, corejs) => Array.from(getValidIncludesAndExcludes(type, corejs)).filter(item => regexp instanceof RegExp && regexp.test(item));
  29. const flatten = array => [].concat(...array);
  30. const expandIncludesAndExcludes = (plugins = [], type, corejs) => {
  31. if (plugins.length === 0) return [];
  32. const selectedPlugins = plugins.map(plugin => selectPlugins(pluginToRegExp(plugin), type, corejs));
  33. const invalidRegExpList = plugins.filter((p, i) => selectedPlugins[i].length === 0);
  34. v.invariant(invalidRegExpList.length === 0, `The plugins/built-ins '${invalidRegExpList.join(", ")}' passed to the '${type}' option are not
  35. valid. Please check data/[plugin-features|built-in-features].js in babel-preset-env`);
  36. return flatten(selectedPlugins);
  37. };
  38. const normalizePluginName = plugin => plugin.replace(/^(@babel\/|babel-)(plugin-)?/, "");
  39. exports.normalizePluginName = normalizePluginName;
  40. const checkDuplicateIncludeExcludes = (include = [], exclude = []) => {
  41. const duplicates = include.filter(opt => exclude.indexOf(opt) >= 0);
  42. v.invariant(duplicates.length === 0, `The plugins/built-ins '${duplicates.join(", ")}' were found in both the "include" and
  43. "exclude" options.`);
  44. };
  45. exports.checkDuplicateIncludeExcludes = checkDuplicateIncludeExcludes;
  46. const normalizeTargets = targets => {
  47. if (typeof targets === "string" || Array.isArray(targets)) {
  48. return {
  49. browsers: targets
  50. };
  51. }
  52. return Object.assign({}, targets);
  53. };
  54. const validateModulesOption = (modulesOpt = _options.ModulesOption.auto) => {
  55. v.invariant(_options.ModulesOption[modulesOpt.toString()] || modulesOpt === _options.ModulesOption.false, `The 'modules' option must be one of \n` + ` - 'false' to indicate no module processing\n` + ` - a specific module type: 'commonjs', 'amd', 'umd', 'systemjs'` + ` - 'auto' (default) which will automatically select 'false' if the current\n` + ` process is known to support ES module syntax, or "commonjs" otherwise\n`);
  56. return modulesOpt;
  57. };
  58. exports.validateModulesOption = validateModulesOption;
  59. const validateUseBuiltInsOption = (builtInsOpt = false) => {
  60. v.invariant(_options.UseBuiltInsOption[builtInsOpt.toString()] || builtInsOpt === _options.UseBuiltInsOption.false, `The 'useBuiltIns' option must be either
  61. 'false' (default) to indicate no polyfill,
  62. '"entry"' to indicate replacing the entry polyfill, or
  63. '"usage"' to import only used polyfills per file`);
  64. return builtInsOpt;
  65. };
  66. exports.validateUseBuiltInsOption = validateUseBuiltInsOption;
  67. function normalizeCoreJSOption(corejs, useBuiltIns) {
  68. let proposals = false;
  69. let rawVersion;
  70. if (useBuiltIns && corejs === undefined) {
  71. rawVersion = 2;
  72. console.warn("\nWARNING (@babel/preset-env): We noticed you're using the `useBuiltIns` option without declaring a " + "core-js version. Currently, we assume version 2.x when no version " + "is passed. Since this default version will likely change in future " + "versions of Babel, we recommend explicitly setting the core-js version " + "you are using via the `corejs` option.\n" + "\nYou should also be sure that the version you pass to the `corejs` " + "option matches the version specified in your `package.json`'s " + "`dependencies` section. If it doesn't, you need to run one of the " + "following commands:\n\n" + " npm install --save core-js@2 npm install --save core-js@3\n" + " yarn add core-js@2 yarn add core-js@3\n\n" + "More info about useBuiltIns: https://babeljs.io/docs/en/babel-preset-env#usebuiltins\n" + "More info about core-js: https://babeljs.io/docs/en/babel-preset-env#corejs");
  73. } else if (typeof corejs === "object" && corejs !== null) {
  74. rawVersion = corejs.version;
  75. proposals = Boolean(corejs.proposals);
  76. } else {
  77. rawVersion = corejs;
  78. }
  79. const version = rawVersion ? (0, _semver.coerce)(String(rawVersion)) : false;
  80. if (!useBuiltIns && version) {
  81. console.warn("\nWARNING (@babel/preset-env): The `corejs` option only has an effect when the `useBuiltIns` option is not `false`\n");
  82. }
  83. if (useBuiltIns && (!version || version.major < 2 || version.major > 3)) {
  84. throw new RangeError("Invalid Option: The version passed to `corejs` is invalid. Currently, " + "only core-js@2 and core-js@3 are supported.");
  85. }
  86. return {
  87. version,
  88. proposals
  89. };
  90. }
  91. function normalizeOptions(opts) {
  92. v.validateTopLevelOptions(opts, _options.TopLevelOptions);
  93. const useBuiltIns = validateUseBuiltInsOption(opts.useBuiltIns);
  94. const corejs = normalizeCoreJSOption(opts.corejs, useBuiltIns);
  95. const include = expandIncludesAndExcludes(opts.include, _options.TopLevelOptions.include, !!corejs.version && corejs.version.major);
  96. const exclude = expandIncludesAndExcludes(opts.exclude, _options.TopLevelOptions.exclude, !!corejs.version && corejs.version.major);
  97. checkDuplicateIncludeExcludes(include, exclude);
  98. return {
  99. bugfixes: v.validateBooleanOption(_options.TopLevelOptions.bugfixes, opts.bugfixes, false),
  100. configPath: v.validateStringOption(_options.TopLevelOptions.configPath, opts.configPath, process.cwd()),
  101. corejs,
  102. debug: v.validateBooleanOption(_options.TopLevelOptions.debug, opts.debug, false),
  103. include,
  104. exclude,
  105. forceAllTransforms: v.validateBooleanOption(_options.TopLevelOptions.forceAllTransforms, opts.forceAllTransforms, false),
  106. ignoreBrowserslistConfig: v.validateBooleanOption(_options.TopLevelOptions.ignoreBrowserslistConfig, opts.ignoreBrowserslistConfig, false),
  107. loose: v.validateBooleanOption(_options.TopLevelOptions.loose, opts.loose),
  108. modules: validateModulesOption(opts.modules),
  109. shippedProposals: v.validateBooleanOption(_options.TopLevelOptions.shippedProposals, opts.shippedProposals, false),
  110. spec: v.validateBooleanOption(_options.TopLevelOptions.spec, opts.spec, false),
  111. targets: normalizeTargets(opts.targets),
  112. useBuiltIns: useBuiltIns,
  113. browserslistEnv: v.validateStringOption(_options.TopLevelOptions.browserslistEnv, opts.browserslistEnv)
  114. };
  115. }