1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.normalizeCoreJSOption = normalizeCoreJSOption;
|
7 | exports.default = normalizeOptions;
|
8 | exports.validateUseBuiltInsOption = exports.validateModulesOption = exports.validateIgnoreBrowserslistConfig = exports.validateBoolOption = exports.validateConfigPathOption = exports.checkDuplicateIncludeExcludes = exports.normalizePluginName = void 0;
|
9 |
|
10 | function _data() {
|
11 | const data = _interopRequireDefault(require("core-js-compat/data"));
|
12 |
|
13 | _data = function () {
|
14 | return data;
|
15 | };
|
16 |
|
17 | return data;
|
18 | }
|
19 |
|
20 | function _invariant() {
|
21 | const data = _interopRequireDefault(require("invariant"));
|
22 |
|
23 | _invariant = function () {
|
24 | return data;
|
25 | };
|
26 |
|
27 | return data;
|
28 | }
|
29 |
|
30 | function _semver() {
|
31 | const data = require("semver");
|
32 |
|
33 | _semver = function () {
|
34 | return data;
|
35 | };
|
36 |
|
37 | return data;
|
38 | }
|
39 |
|
40 | var _corejs2BuiltIns = _interopRequireDefault(require("../data/corejs2-built-ins.json"));
|
41 |
|
42 | var _plugins = _interopRequireDefault(require("../data/plugins.json"));
|
43 |
|
44 | var _moduleTransformations = _interopRequireDefault(require("./module-transformations"));
|
45 |
|
46 | var _options = require("./options");
|
47 |
|
48 | var _getPlatformSpecificDefault = require("./polyfills/corejs2/get-platform-specific-default");
|
49 |
|
50 | var _targetsParser = require("./targets-parser");
|
51 |
|
52 | var _utils = require("./utils");
|
53 |
|
54 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
55 |
|
56 | const validateTopLevelOptions = options => {
|
57 | const validOptions = Object.keys(_options.TopLevelOptions);
|
58 |
|
59 | for (const option in options) {
|
60 | if (!_options.TopLevelOptions[option]) {
|
61 | throw new Error(`Invalid Option: ${option} is not a valid top-level option.
|
62 | Maybe you meant to use '${(0, _utils.findSuggestion)(validOptions, option)}'?`);
|
63 | }
|
64 | }
|
65 | };
|
66 |
|
67 | const allPluginsList = [...Object.keys(_plugins.default), ...Object.keys(_moduleTransformations.default).map(m => _moduleTransformations.default[m])];
|
68 | const validIncludesAndExcludesWithoutCoreJS = new Set(allPluginsList);
|
69 | const validIncludesAndExcludesWithCoreJS2 = new Set([...allPluginsList, ...Object.keys(_corejs2BuiltIns.default), ..._getPlatformSpecificDefault.defaultWebIncludes]);
|
70 | const validIncludesAndExcludesWithCoreJS3 = new Set([...allPluginsList, ...Object.keys(_data().default)]);
|
71 |
|
72 | const pluginToRegExp = plugin => {
|
73 | if (plugin instanceof RegExp) return plugin;
|
74 |
|
75 | try {
|
76 | return new RegExp(`^${normalizePluginName(plugin)}$`);
|
77 | } catch (e) {
|
78 | return null;
|
79 | }
|
80 | };
|
81 |
|
82 | const selectPlugins = (regexp, corejs) => Array.from(corejs ? corejs == 2 ? validIncludesAndExcludesWithCoreJS2 : validIncludesAndExcludesWithCoreJS3 : validIncludesAndExcludesWithoutCoreJS).filter(item => regexp instanceof RegExp && regexp.test(item));
|
83 |
|
84 | const flatten = array => [].concat(...array);
|
85 |
|
86 | const expandIncludesAndExcludes = (plugins = [], type, corejs) => {
|
87 | if (plugins.length === 0) return [];
|
88 | const selectedPlugins = plugins.map(plugin => selectPlugins(pluginToRegExp(plugin), corejs));
|
89 | const invalidRegExpList = plugins.filter((p, i) => selectedPlugins[i].length === 0);
|
90 | (0, _invariant().default)(invalidRegExpList.length === 0, `Invalid Option: The plugins/built-ins '${invalidRegExpList.join(", ")}' passed to the '${type}' option are not
|
91 | valid. Please check data/[plugin-features|built-in-features].js in babel-preset-env`);
|
92 | return flatten(selectedPlugins);
|
93 | };
|
94 |
|
95 | const normalizePluginName = plugin => plugin.replace(/^(@babel\/|babel-)(plugin-)?/, "");
|
96 |
|
97 | exports.normalizePluginName = normalizePluginName;
|
98 |
|
99 | const checkDuplicateIncludeExcludes = (include = [], exclude = []) => {
|
100 | const duplicates = include.filter(opt => exclude.indexOf(opt) >= 0);
|
101 | (0, _invariant().default)(duplicates.length === 0, `Invalid Option: The plugins/built-ins '${duplicates.join(", ")}' were found in both the "include" and
|
102 | "exclude" options.`);
|
103 | };
|
104 |
|
105 | exports.checkDuplicateIncludeExcludes = checkDuplicateIncludeExcludes;
|
106 |
|
107 | const normalizeTargets = targets => {
|
108 | if ((0, _targetsParser.isBrowsersQueryValid)(targets)) {
|
109 | return {
|
110 | browsers: targets
|
111 | };
|
112 | }
|
113 |
|
114 | return Object.assign({}, targets);
|
115 | };
|
116 |
|
117 | const validateConfigPathOption = (configPath = process.cwd()) => {
|
118 | (0, _invariant().default)(typeof configPath === "string", `Invalid Option: The configPath option '${configPath}' is invalid, only strings are allowed.`);
|
119 | return configPath;
|
120 | };
|
121 |
|
122 | exports.validateConfigPathOption = validateConfigPathOption;
|
123 |
|
124 | const validateBoolOption = (name, value, defaultValue) => {
|
125 | if (typeof value === "undefined") {
|
126 | value = defaultValue;
|
127 | }
|
128 |
|
129 | if (typeof value !== "boolean") {
|
130 | throw new Error(`Preset env: '${name}' option must be a boolean.`);
|
131 | }
|
132 |
|
133 | return value;
|
134 | };
|
135 |
|
136 | exports.validateBoolOption = validateBoolOption;
|
137 |
|
138 | const validateIgnoreBrowserslistConfig = ignoreBrowserslistConfig => validateBoolOption(_options.TopLevelOptions.ignoreBrowserslistConfig, ignoreBrowserslistConfig, false);
|
139 |
|
140 | exports.validateIgnoreBrowserslistConfig = validateIgnoreBrowserslistConfig;
|
141 |
|
142 | const validateModulesOption = (modulesOpt = _options.ModulesOption.auto) => {
|
143 | (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`);
|
144 | return modulesOpt;
|
145 | };
|
146 |
|
147 | exports.validateModulesOption = validateModulesOption;
|
148 |
|
149 | const validateUseBuiltInsOption = (builtInsOpt = false) => {
|
150 | (0, _invariant().default)(_options.UseBuiltInsOption[builtInsOpt.toString()] || _options.UseBuiltInsOption[builtInsOpt.toString()] === _options.UseBuiltInsOption.false, `Invalid Option: The 'useBuiltIns' option must be either
|
151 | 'false' (default) to indicate no polyfill,
|
152 | '"entry"' to indicate replacing the entry polyfill, or
|
153 | '"usage"' to import only used polyfills per file`);
|
154 | return builtInsOpt;
|
155 | };
|
156 |
|
157 | exports.validateUseBuiltInsOption = validateUseBuiltInsOption;
|
158 |
|
159 | function normalizeCoreJSOption(corejs, useBuiltIns) {
|
160 | let proposals = false;
|
161 | let rawVersion;
|
162 |
|
163 | if (useBuiltIns && corejs === undefined) {
|
164 | rawVersion = 2;
|
165 | console.log("\nWith `useBuiltIns` option, required direct setting of `corejs` option\n");
|
166 | } else if (typeof corejs === "object" && corejs !== null) {
|
167 | rawVersion = corejs.version;
|
168 | proposals = Boolean(corejs.proposals);
|
169 | } else {
|
170 | rawVersion = corejs;
|
171 | }
|
172 |
|
173 | const version = rawVersion ? (0, _semver().coerce)(String(rawVersion)) : false;
|
174 |
|
175 | if (!useBuiltIns && version) {
|
176 | console.log("\n`corejs` option required only with `useBuiltIns` option\n");
|
177 | }
|
178 |
|
179 | if (useBuiltIns && (!version || version.major < 2 || version.major > 3)) {
|
180 | throw new RangeError("Supported only core-js@2 and core-js@3.");
|
181 | }
|
182 |
|
183 | return {
|
184 | version,
|
185 | proposals
|
186 | };
|
187 | }
|
188 |
|
189 | function normalizeOptions(opts) {
|
190 | validateTopLevelOptions(opts);
|
191 | const useBuiltIns = validateUseBuiltInsOption(opts.useBuiltIns);
|
192 | const corejs = normalizeCoreJSOption(opts.corejs, useBuiltIns);
|
193 | const include = expandIncludesAndExcludes(opts.include, _options.TopLevelOptions.include, !!corejs.version && corejs.version.major);
|
194 | const exclude = expandIncludesAndExcludes(opts.exclude, _options.TopLevelOptions.exclude, !!corejs.version && corejs.version.major);
|
195 | checkDuplicateIncludeExcludes(include, exclude);
|
196 | const shippedProposals = validateBoolOption(_options.TopLevelOptions.shippedProposals, opts.shippedProposals, false) || corejs.proposals;
|
197 | return {
|
198 | configPath: validateConfigPathOption(opts.configPath),
|
199 | corejs,
|
200 | debug: validateBoolOption(_options.TopLevelOptions.debug, opts.debug, false),
|
201 | include,
|
202 | exclude,
|
203 | forceAllTransforms: validateBoolOption(_options.TopLevelOptions.forceAllTransforms, opts.forceAllTransforms, false),
|
204 | ignoreBrowserslistConfig: validateIgnoreBrowserslistConfig(opts.ignoreBrowserslistConfig),
|
205 | loose: validateBoolOption(_options.TopLevelOptions.loose, opts.loose, false),
|
206 | modules: validateModulesOption(opts.modules),
|
207 | shippedProposals,
|
208 | spec: validateBoolOption(_options.TopLevelOptions.spec, opts.spec, false),
|
209 | targets: normalizeTargets(opts.targets),
|
210 | useBuiltIns: useBuiltIns
|
211 | };
|
212 | } |
\ | No newline at end of file |