1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.isBrowsersQueryValid = isBrowsersQueryValid;
|
---|
7 | exports.default = getTargets;
|
---|
8 | Object.defineProperty(exports, "unreleasedLabels", {
|
---|
9 | enumerable: true,
|
---|
10 | get: function () {
|
---|
11 | return _targets.unreleasedLabels;
|
---|
12 | }
|
---|
13 | });
|
---|
14 | Object.defineProperty(exports, "TargetNames", {
|
---|
15 | enumerable: true,
|
---|
16 | get: function () {
|
---|
17 | return _options.TargetNames;
|
---|
18 | }
|
---|
19 | });
|
---|
20 | Object.defineProperty(exports, "prettifyTargets", {
|
---|
21 | enumerable: true,
|
---|
22 | get: function () {
|
---|
23 | return _pretty.prettifyTargets;
|
---|
24 | }
|
---|
25 | });
|
---|
26 | Object.defineProperty(exports, "getInclusionReasons", {
|
---|
27 | enumerable: true,
|
---|
28 | get: function () {
|
---|
29 | return _debug.getInclusionReasons;
|
---|
30 | }
|
---|
31 | });
|
---|
32 | Object.defineProperty(exports, "filterItems", {
|
---|
33 | enumerable: true,
|
---|
34 | get: function () {
|
---|
35 | return _filterItems.default;
|
---|
36 | }
|
---|
37 | });
|
---|
38 | Object.defineProperty(exports, "isRequired", {
|
---|
39 | enumerable: true,
|
---|
40 | get: function () {
|
---|
41 | return _filterItems.isRequired;
|
---|
42 | }
|
---|
43 | });
|
---|
44 |
|
---|
45 | var _browserslist = require("browserslist");
|
---|
46 |
|
---|
47 | var _helperValidatorOption = require("@babel/helper-validator-option");
|
---|
48 |
|
---|
49 | var _nativeModules = require("@babel/compat-data/native-modules");
|
---|
50 |
|
---|
51 | var _utils = require("./utils");
|
---|
52 |
|
---|
53 | var _targets = require("./targets");
|
---|
54 |
|
---|
55 | var _options = require("./options");
|
---|
56 |
|
---|
57 | var _pretty = require("./pretty");
|
---|
58 |
|
---|
59 | var _debug = require("./debug");
|
---|
60 |
|
---|
61 | var _filterItems = require("./filter-items");
|
---|
62 |
|
---|
63 | const ESM_SUPPORT = _nativeModules["es6.module"];
|
---|
64 | const v = new _helperValidatorOption.OptionValidator("@babel/helper-compilation-targets");
|
---|
65 |
|
---|
66 | function validateTargetNames(targets) {
|
---|
67 | const validTargets = Object.keys(_options.TargetNames);
|
---|
68 |
|
---|
69 | for (const target of Object.keys(targets)) {
|
---|
70 | if (!(target in _options.TargetNames)) {
|
---|
71 | throw new Error(v.formatMessage(`'${target}' is not a valid target
|
---|
72 | - Did you mean '${(0, _helperValidatorOption.findSuggestion)(target, validTargets)}'?`));
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | return targets;
|
---|
77 | }
|
---|
78 |
|
---|
79 | function isBrowsersQueryValid(browsers) {
|
---|
80 | return typeof browsers === "string" || Array.isArray(browsers) && browsers.every(b => typeof b === "string");
|
---|
81 | }
|
---|
82 |
|
---|
83 | function validateBrowsers(browsers) {
|
---|
84 | v.invariant(browsers === undefined || isBrowsersQueryValid(browsers), `'${String(browsers)}' is not a valid browserslist query`);
|
---|
85 | return browsers;
|
---|
86 | }
|
---|
87 |
|
---|
88 | function getLowestVersions(browsers) {
|
---|
89 | return browsers.reduce((all, browser) => {
|
---|
90 | const [browserName, browserVersion] = browser.split(" ");
|
---|
91 | const normalizedBrowserName = _targets.browserNameMap[browserName];
|
---|
92 |
|
---|
93 | if (!normalizedBrowserName) {
|
---|
94 | return all;
|
---|
95 | }
|
---|
96 |
|
---|
97 | try {
|
---|
98 | const splitVersion = browserVersion.split("-")[0].toLowerCase();
|
---|
99 | const isSplitUnreleased = (0, _utils.isUnreleasedVersion)(splitVersion, browserName);
|
---|
100 |
|
---|
101 | if (!all[normalizedBrowserName]) {
|
---|
102 | all[normalizedBrowserName] = isSplitUnreleased ? splitVersion : (0, _utils.semverify)(splitVersion);
|
---|
103 | return all;
|
---|
104 | }
|
---|
105 |
|
---|
106 | const version = all[normalizedBrowserName];
|
---|
107 | const isUnreleased = (0, _utils.isUnreleasedVersion)(version, browserName);
|
---|
108 |
|
---|
109 | if (isUnreleased && isSplitUnreleased) {
|
---|
110 | all[normalizedBrowserName] = (0, _utils.getLowestUnreleased)(version, splitVersion, browserName);
|
---|
111 | } else if (isUnreleased) {
|
---|
112 | all[normalizedBrowserName] = (0, _utils.semverify)(splitVersion);
|
---|
113 | } else if (!isUnreleased && !isSplitUnreleased) {
|
---|
114 | const parsedBrowserVersion = (0, _utils.semverify)(splitVersion);
|
---|
115 | all[normalizedBrowserName] = (0, _utils.semverMin)(version, parsedBrowserVersion);
|
---|
116 | }
|
---|
117 | } catch (e) {}
|
---|
118 |
|
---|
119 | return all;
|
---|
120 | }, {});
|
---|
121 | }
|
---|
122 |
|
---|
123 | function outputDecimalWarning(decimalTargets) {
|
---|
124 | if (!decimalTargets.length) {
|
---|
125 | return;
|
---|
126 | }
|
---|
127 |
|
---|
128 | console.warn("Warning, the following targets are using a decimal version:\n");
|
---|
129 | decimalTargets.forEach(({
|
---|
130 | target,
|
---|
131 | value
|
---|
132 | }) => console.warn(` ${target}: ${value}`));
|
---|
133 | console.warn(`
|
---|
134 | We recommend using a string for minor/patch versions to avoid numbers like 6.10
|
---|
135 | getting parsed as 6.1, which can lead to unexpected behavior.
|
---|
136 | `);
|
---|
137 | }
|
---|
138 |
|
---|
139 | function semverifyTarget(target, value) {
|
---|
140 | try {
|
---|
141 | return (0, _utils.semverify)(value);
|
---|
142 | } catch (error) {
|
---|
143 | throw new Error(v.formatMessage(`'${value}' is not a valid value for 'targets.${target}'.`));
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | const targetParserMap = {
|
---|
148 | __default(target, value) {
|
---|
149 | const version = (0, _utils.isUnreleasedVersion)(value, target) ? value.toLowerCase() : semverifyTarget(target, value);
|
---|
150 | return [target, version];
|
---|
151 | },
|
---|
152 |
|
---|
153 | node(target, value) {
|
---|
154 | const parsed = value === true || value === "current" ? process.versions.node : semverifyTarget(target, value);
|
---|
155 | return [target, parsed];
|
---|
156 | }
|
---|
157 |
|
---|
158 | };
|
---|
159 |
|
---|
160 | function generateTargets(inputTargets) {
|
---|
161 | const input = Object.assign({}, inputTargets);
|
---|
162 | delete input.esmodules;
|
---|
163 | delete input.browsers;
|
---|
164 | return input;
|
---|
165 | }
|
---|
166 |
|
---|
167 | function resolveTargets(queries, env) {
|
---|
168 | const resolved = _browserslist(queries, {
|
---|
169 | mobileToDesktop: true,
|
---|
170 | env
|
---|
171 | });
|
---|
172 |
|
---|
173 | return getLowestVersions(resolved);
|
---|
174 | }
|
---|
175 |
|
---|
176 | function getTargets(inputTargets = {}, options = {}) {
|
---|
177 | var _browsers;
|
---|
178 |
|
---|
179 | let {
|
---|
180 | browsers,
|
---|
181 | esmodules
|
---|
182 | } = inputTargets;
|
---|
183 | const {
|
---|
184 | configPath = "."
|
---|
185 | } = options;
|
---|
186 | validateBrowsers(browsers);
|
---|
187 | const input = generateTargets(inputTargets);
|
---|
188 | let targets = validateTargetNames(input);
|
---|
189 | const shouldParseBrowsers = !!browsers;
|
---|
190 | const hasTargets = shouldParseBrowsers || Object.keys(targets).length > 0;
|
---|
191 | const shouldSearchForConfig = !options.ignoreBrowserslistConfig && !hasTargets;
|
---|
192 |
|
---|
193 | if (!browsers && shouldSearchForConfig) {
|
---|
194 | browsers = _browserslist.loadConfig({
|
---|
195 | config: options.configFile,
|
---|
196 | path: configPath,
|
---|
197 | env: options.browserslistEnv
|
---|
198 | });
|
---|
199 |
|
---|
200 | if (browsers == null) {
|
---|
201 | {
|
---|
202 | browsers = [];
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (esmodules && (esmodules !== "intersect" || !((_browsers = browsers) != null && _browsers.length))) {
|
---|
208 | browsers = Object.keys(ESM_SUPPORT).map(browser => `${browser} >= ${ESM_SUPPORT[browser]}`).join(", ");
|
---|
209 | esmodules = false;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (browsers) {
|
---|
213 | const queryBrowsers = resolveTargets(browsers, options.browserslistEnv);
|
---|
214 |
|
---|
215 | if (esmodules === "intersect") {
|
---|
216 | for (const browser of Object.keys(queryBrowsers)) {
|
---|
217 | const version = queryBrowsers[browser];
|
---|
218 |
|
---|
219 | if (ESM_SUPPORT[browser]) {
|
---|
220 | queryBrowsers[browser] = (0, _utils.getHighestUnreleased)(version, (0, _utils.semverify)(ESM_SUPPORT[browser]), browser);
|
---|
221 | } else {
|
---|
222 | delete queryBrowsers[browser];
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | targets = Object.assign(queryBrowsers, targets);
|
---|
228 | }
|
---|
229 |
|
---|
230 | const result = {};
|
---|
231 | const decimalWarnings = [];
|
---|
232 |
|
---|
233 | for (const target of Object.keys(targets).sort()) {
|
---|
234 | var _targetParserMap$targ;
|
---|
235 |
|
---|
236 | const value = targets[target];
|
---|
237 |
|
---|
238 | if (typeof value === "number" && value % 1 !== 0) {
|
---|
239 | decimalWarnings.push({
|
---|
240 | target,
|
---|
241 | value
|
---|
242 | });
|
---|
243 | }
|
---|
244 |
|
---|
245 | const parser = (_targetParserMap$targ = targetParserMap[target]) != null ? _targetParserMap$targ : targetParserMap.__default;
|
---|
246 | const [parsedTarget, parsedValue] = parser(target, value);
|
---|
247 |
|
---|
248 | if (parsedValue) {
|
---|
249 | result[parsedTarget] = parsedValue;
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | outputDecimalWarning(decimalWarnings);
|
---|
254 | return result;
|
---|
255 | } |
---|