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