UNPKG

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