You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
8.6 KiB

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