normalize-options.js 7.4 KB

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