UNPKG

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