index.js
6.86 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "TargetNames", {
enumerable: true,
get: function () {
return _options.TargetNames;
}
});
exports.default = getTargets;
Object.defineProperty(exports, "filterItems", {
enumerable: true,
get: function () {
return _filterItems.default;
}
});
Object.defineProperty(exports, "getInclusionReasons", {
enumerable: true,
get: function () {
return _debug.getInclusionReasons;
}
});
exports.isBrowsersQueryValid = isBrowsersQueryValid;
Object.defineProperty(exports, "isRequired", {
enumerable: true,
get: function () {
return _filterItems.isRequired;
}
});
Object.defineProperty(exports, "prettifyTargets", {
enumerable: true,
get: function () {
return _pretty.prettifyTargets;
}
});
Object.defineProperty(exports, "unreleasedLabels", {
enumerable: true,
get: function () {
return _targets.unreleasedLabels;
}
});
var _browserslist = require("browserslist");
var _helperValidatorOption = require("@babel/helper-validator-option");
var _nativeModules = require("@babel/compat-data/native-modules");
var _utils = require("./utils");
var _targets = require("./targets");
var _options = require("./options");
var _pretty = require("./pretty");
var _debug = require("./debug");
var _filterItems = require("./filter-items");
const ESM_SUPPORT = _nativeModules["es6.module"];
const v = new _helperValidatorOption.OptionValidator("@babel/helper-compilation-targets");
function validateTargetNames(targets) {
const validTargets = Object.keys(_options.TargetNames);
for (const target of Object.keys(targets)) {
if (!(target in _options.TargetNames)) {
throw new Error(v.formatMessage(`'${target}' is not a valid target
- Did you mean '${(0, _helperValidatorOption.findSuggestion)(target, validTargets)}'?`));
}
}
return targets;
}
function isBrowsersQueryValid(browsers) {
return typeof browsers === "string" || Array.isArray(browsers) && browsers.every(b => typeof b === "string");
}
function validateBrowsers(browsers) {
v.invariant(browsers === undefined || isBrowsersQueryValid(browsers), `'${String(browsers)}' is not a valid browserslist query`);
return browsers;
}
function getLowestVersions(browsers) {
return browsers.reduce((all, browser) => {
const [browserName, browserVersion] = browser.split(" ");
const target = _targets.browserNameMap[browserName];
if (!target) {
return all;
}
try {
const splitVersion = browserVersion.split("-")[0].toLowerCase();
const isSplitUnreleased = (0, _utils.isUnreleasedVersion)(splitVersion, target);
if (!all[target]) {
all[target] = isSplitUnreleased ? splitVersion : (0, _utils.semverify)(splitVersion);
return all;
}
const version = all[target];
const isUnreleased = (0, _utils.isUnreleasedVersion)(version, target);
if (isUnreleased && isSplitUnreleased) {
all[target] = (0, _utils.getLowestUnreleased)(version, splitVersion, target);
} else if (isUnreleased) {
all[target] = (0, _utils.semverify)(splitVersion);
} else if (!isUnreleased && !isSplitUnreleased) {
const parsedBrowserVersion = (0, _utils.semverify)(splitVersion);
all[target] = (0, _utils.semverMin)(version, parsedBrowserVersion);
}
} catch (e) {}
return all;
}, {});
}
function outputDecimalWarning(decimalTargets) {
if (!decimalTargets.length) {
return;
}
console.warn("Warning, the following targets are using a decimal version:\n");
decimalTargets.forEach(({
target,
value
}) => console.warn(` ${target}: ${value}`));
console.warn(`
We recommend using a string for minor/patch versions to avoid numbers like 6.10
getting parsed as 6.1, which can lead to unexpected behavior.
`);
}
function semverifyTarget(target, value) {
try {
return (0, _utils.semverify)(value);
} catch (error) {
throw new Error(v.formatMessage(`'${value}' is not a valid value for 'targets.${target}'.`));
}
}
function nodeTargetParser(value) {
const parsed = value === true || value === "current" ? process.versions.node : semverifyTarget("node", value);
return ["node", parsed];
}
function defaultTargetParser(target, value) {
const version = (0, _utils.isUnreleasedVersion)(value, target) ? value.toLowerCase() : semverifyTarget(target, value);
return [target, version];
}
function generateTargets(inputTargets) {
const input = Object.assign({}, inputTargets);
delete input.esmodules;
delete input.browsers;
return input;
}
function resolveTargets(queries, env) {
const resolved = _browserslist(queries, {
mobileToDesktop: true,
env
});
return getLowestVersions(resolved);
}
function getTargets(inputTargets = {}, options = {}) {
var _browsers, _browsers2;
let {
browsers,
esmodules
} = inputTargets;
const {
configPath = "."
} = options;
validateBrowsers(browsers);
const input = generateTargets(inputTargets);
let targets = validateTargetNames(input);
const shouldParseBrowsers = !!browsers;
const hasTargets = shouldParseBrowsers || Object.keys(targets).length > 0;
const shouldSearchForConfig = !options.ignoreBrowserslistConfig && !hasTargets;
if (!browsers && shouldSearchForConfig) {
browsers = _browserslist.loadConfig({
config: options.configFile,
path: configPath,
env: options.browserslistEnv
});
if (browsers == null) {
{
browsers = [];
}
}
}
if (esmodules && (esmodules !== "intersect" || !((_browsers = browsers) != null && _browsers.length))) {
browsers = Object.keys(ESM_SUPPORT).map(browser => `${browser} >= ${ESM_SUPPORT[browser]}`).join(", ");
esmodules = false;
}
if ((_browsers2 = browsers) != null && _browsers2.length) {
const queryBrowsers = resolveTargets(browsers, options.browserslistEnv);
if (esmodules === "intersect") {
for (const browser of Object.keys(queryBrowsers)) {
const version = queryBrowsers[browser];
const esmSupportVersion = ESM_SUPPORT[browser];
if (esmSupportVersion) {
queryBrowsers[browser] = (0, _utils.getHighestUnreleased)(version, (0, _utils.semverify)(esmSupportVersion), browser);
} else {
delete queryBrowsers[browser];
}
}
}
targets = Object.assign(queryBrowsers, targets);
}
const result = {};
const decimalWarnings = [];
for (const target of Object.keys(targets).sort()) {
const value = targets[target];
if (typeof value === "number" && value % 1 !== 0) {
decimalWarnings.push({
target,
value
});
}
const [parsedTarget, parsedValue] = target === "node" ? nodeTargetParser(value) : defaultTargetParser(target, value);
if (parsedValue) {
result[parsedTarget] = parsedValue;
}
}
outputDecimalWarning(decimalWarnings);
return result;
}
//# sourceMappingURL=index.js.map