normalize-options.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = normalizeOptions;
  6. exports.validateUseBuiltInsOption = exports.objectToBrowserslist = exports.validateModulesOption = exports.validateIgnoreBrowserslistConfig = exports.validateBoolOption = exports.validateConfigPathOption = exports.checkDuplicateIncludeExcludes = exports.normalizePluginName = void 0;
  7. function _invariant() {
  8. const data = _interopRequireDefault(require("invariant"));
  9. _invariant = function () {
  10. return data;
  11. };
  12. return data;
  13. }
  14. function _browserslist() {
  15. const data = _interopRequireDefault(require("browserslist"));
  16. _browserslist = function () {
  17. return data;
  18. };
  19. return data;
  20. }
  21. var _builtIns = _interopRequireDefault(require("../data/built-ins.json"));
  22. var _defaultIncludes = require("./default-includes");
  23. var _moduleTransformations = _interopRequireDefault(require("./module-transformations"));
  24. var _targetsParser = require("./targets-parser");
  25. var _utils = require("./utils");
  26. var _plugins = _interopRequireDefault(require("../data/plugins.json"));
  27. var _options = require("./options");
  28. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  29. const validateTopLevelOptions = options => {
  30. for (const option in options) {
  31. if (!_options.TopLevelOptions[option]) {
  32. const validOptions = (0, _utils.getValues)(_options.TopLevelOptions);
  33. throw new Error(`Invalid Option: ${option} is not a valid top-level option.
  34. Maybe you meant to use '${(0, _utils.findSuggestion)(validOptions, option)}'?`);
  35. }
  36. }
  37. };
  38. const validIncludesAndExcludes = new Set([...Object.keys(_plugins.default), ...Object.keys(_moduleTransformations.default).map(m => _moduleTransformations.default[m]), ...Object.keys(_builtIns.default), ..._defaultIncludes.defaultWebIncludes]);
  39. const pluginToRegExp = plugin => {
  40. if (plugin instanceof RegExp) return plugin;
  41. try {
  42. return new RegExp(`^${normalizePluginName(plugin)}$`);
  43. } catch (e) {
  44. return null;
  45. }
  46. };
  47. const selectPlugins = regexp => Array.from(validIncludesAndExcludes).filter(item => regexp instanceof RegExp && regexp.test(item));
  48. const flatten = array => [].concat(...array);
  49. const expandIncludesAndExcludes = (plugins = [], type) => {
  50. if (plugins.length === 0) return [];
  51. const selectedPlugins = plugins.map(plugin => selectPlugins(pluginToRegExp(plugin)));
  52. const invalidRegExpList = plugins.filter((p, i) => selectedPlugins[i].length === 0);
  53. (0, _invariant().default)(invalidRegExpList.length === 0, `Invalid Option: The plugins/built-ins '${invalidRegExpList.join(", ")}' passed to the '${type}' option are not
  54. valid. Please check data/[plugin-features|built-in-features].js in babel-preset-env`);
  55. return flatten(selectedPlugins);
  56. };
  57. const validBrowserslistTargets = [...Object.keys(_browserslist().default.data), ...Object.keys(_browserslist().default.aliases)];
  58. const normalizePluginName = plugin => plugin.replace(/^(@babel\/|babel-)(plugin-)?/, "");
  59. exports.normalizePluginName = normalizePluginName;
  60. const checkDuplicateIncludeExcludes = (include = [], exclude = []) => {
  61. const duplicates = include.filter(opt => exclude.indexOf(opt) >= 0);
  62. (0, _invariant().default)(duplicates.length === 0, `Invalid Option: The plugins/built-ins '${duplicates.join(", ")}' were found in both the "include" and
  63. "exclude" options.`);
  64. };
  65. exports.checkDuplicateIncludeExcludes = checkDuplicateIncludeExcludes;
  66. const normalizeTargets = targets => {
  67. if ((0, _targetsParser.isBrowsersQueryValid)(targets)) {
  68. return {
  69. browsers: targets
  70. };
  71. }
  72. return Object.assign({}, targets);
  73. };
  74. const validateConfigPathOption = (configPath = process.cwd()) => {
  75. (0, _invariant().default)(typeof configPath === "string", `Invalid Option: The configPath option '${configPath}' is invalid, only strings are allowed.`);
  76. return configPath;
  77. };
  78. exports.validateConfigPathOption = validateConfigPathOption;
  79. const validateBoolOption = (name, value, defaultValue) => {
  80. if (typeof value === "undefined") {
  81. value = defaultValue;
  82. }
  83. if (typeof value !== "boolean") {
  84. throw new Error(`Preset env: '${name}' option must be a boolean.`);
  85. }
  86. return value;
  87. };
  88. exports.validateBoolOption = validateBoolOption;
  89. const validateIgnoreBrowserslistConfig = ignoreBrowserslistConfig => validateBoolOption(_options.TopLevelOptions.ignoreBrowserslistConfig, ignoreBrowserslistConfig, false);
  90. exports.validateIgnoreBrowserslistConfig = validateIgnoreBrowserslistConfig;
  91. const validateModulesOption = (modulesOpt = _options.ModulesOption.auto) => {
  92. (0, _invariant().default)(_options.ModulesOption[modulesOpt] || _options.ModulesOption[modulesOpt] === _options.ModulesOption.false, `Invalid Option: 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`);
  93. return modulesOpt;
  94. };
  95. exports.validateModulesOption = validateModulesOption;
  96. const objectToBrowserslist = object => {
  97. return Object.keys(object).reduce((list, targetName) => {
  98. if (validBrowserslistTargets.indexOf(targetName) >= 0) {
  99. const targetVersion = object[targetName];
  100. return list.concat(`${targetName} ${targetVersion}`);
  101. }
  102. return list;
  103. }, []);
  104. };
  105. exports.objectToBrowserslist = objectToBrowserslist;
  106. const validateUseBuiltInsOption = (builtInsOpt = false) => {
  107. (0, _invariant().default)(_options.UseBuiltInsOption[builtInsOpt] || _options.UseBuiltInsOption[builtInsOpt] === _options.UseBuiltInsOption.false, `Invalid Option: The 'useBuiltIns' option must be either
  108. 'false' (default) to indicate no polyfill,
  109. '"entry"' to indicate replacing the entry polyfill, or
  110. '"usage"' to import only used polyfills per file`);
  111. return builtInsOpt;
  112. };
  113. exports.validateUseBuiltInsOption = validateUseBuiltInsOption;
  114. function normalizeOptions(opts) {
  115. validateTopLevelOptions(opts);
  116. const include = expandIncludesAndExcludes(opts.include, _options.TopLevelOptions.include);
  117. const exclude = expandIncludesAndExcludes(opts.exclude, _options.TopLevelOptions.exclude);
  118. checkDuplicateIncludeExcludes(include, exclude);
  119. return {
  120. configPath: validateConfigPathOption(opts.configPath),
  121. debug: validateBoolOption(_options.TopLevelOptions.debug, opts.debug, false),
  122. include,
  123. exclude,
  124. forceAllTransforms: validateBoolOption(_options.TopLevelOptions.forceAllTransforms, opts.forceAllTransforms, false),
  125. ignoreBrowserslistConfig: validateIgnoreBrowserslistConfig(opts.ignoreBrowserslistConfig),
  126. loose: validateBoolOption(_options.TopLevelOptions.loose, opts.loose, false),
  127. modules: validateModulesOption(opts.modules),
  128. shippedProposals: validateBoolOption(_options.TopLevelOptions.shippedProposals, opts.shippedProposals, false),
  129. spec: validateBoolOption(_options.TopLevelOptions.spec, opts.spec, false),
  130. targets: normalizeTargets(opts.targets),
  131. useBuiltIns: validateUseBuiltInsOption(opts.useBuiltIns)
  132. };
  133. }