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