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