1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Sergey Melyukov @smelukov
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const browserslist = require("browserslist");
|
---|
9 | const path = require("path");
|
---|
10 |
|
---|
11 | /** @typedef {import("./target").ApiTargetProperties} ApiTargetProperties */
|
---|
12 | /** @typedef {import("./target").EcmaTargetProperties} EcmaTargetProperties */
|
---|
13 | /** @typedef {import("./target").PlatformTargetProperties} PlatformTargetProperties */
|
---|
14 |
|
---|
15 | // [[C:]/path/to/config][:env]
|
---|
16 | const inputRx = /^(?:((?:[A-Z]:)?[/\\].*?))?(?::(.+?))?$/i;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * @typedef {Object} BrowserslistHandlerConfig
|
---|
20 | * @property {string=} configPath
|
---|
21 | * @property {string=} env
|
---|
22 | * @property {string=} query
|
---|
23 | */
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @param {string} input input string
|
---|
27 | * @param {string} context the context directory
|
---|
28 | * @returns {BrowserslistHandlerConfig} config
|
---|
29 | */
|
---|
30 | const parse = (input, context) => {
|
---|
31 | if (!input) {
|
---|
32 | return {};
|
---|
33 | }
|
---|
34 |
|
---|
35 | if (path.isAbsolute(input)) {
|
---|
36 | const [, configPath, env] = inputRx.exec(input) || [];
|
---|
37 | return { configPath, env };
|
---|
38 | }
|
---|
39 |
|
---|
40 | const config = browserslist.findConfig(context);
|
---|
41 |
|
---|
42 | if (config && Object.keys(config).includes(input)) {
|
---|
43 | return { env: input };
|
---|
44 | }
|
---|
45 |
|
---|
46 | return { query: input };
|
---|
47 | };
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * @param {string} input input string
|
---|
51 | * @param {string} context the context directory
|
---|
52 | * @returns {string[] | undefined} selected browsers
|
---|
53 | */
|
---|
54 | const load = (input, context) => {
|
---|
55 | const { configPath, env, query } = parse(input, context);
|
---|
56 |
|
---|
57 | // if a query is specified, then use it, else
|
---|
58 | // if a path to a config is specified then load it, else
|
---|
59 | // find a nearest config
|
---|
60 | const config = query
|
---|
61 | ? query
|
---|
62 | : configPath
|
---|
63 | ? browserslist.loadConfig({
|
---|
64 | config: configPath,
|
---|
65 | env
|
---|
66 | })
|
---|
67 | : browserslist.loadConfig({ path: context, env });
|
---|
68 |
|
---|
69 | if (!config) return null;
|
---|
70 | return browserslist(config);
|
---|
71 | };
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * @param {string[]} browsers supported browsers list
|
---|
75 | * @returns {EcmaTargetProperties & PlatformTargetProperties & ApiTargetProperties} target properties
|
---|
76 | */
|
---|
77 | const resolve = browsers => {
|
---|
78 | /**
|
---|
79 | * Checks all against a version number
|
---|
80 | * @param {Record<string, number | [number, number]>} versions first supported version
|
---|
81 | * @returns {boolean} true if supports
|
---|
82 | */
|
---|
83 | const rawChecker = versions => {
|
---|
84 | return browsers.every(v => {
|
---|
85 | const [name, parsedVersion] = v.split(" ");
|
---|
86 | if (!name) return false;
|
---|
87 | const requiredVersion = versions[name];
|
---|
88 | if (!requiredVersion) return false;
|
---|
89 | const [parsedMajor, parserMinor] =
|
---|
90 | // safari TP supports all features for normal safari
|
---|
91 | parsedVersion === "TP"
|
---|
92 | ? [Infinity, Infinity]
|
---|
93 | : parsedVersion.split(".");
|
---|
94 | if (typeof requiredVersion === "number") {
|
---|
95 | return +parsedMajor >= requiredVersion;
|
---|
96 | }
|
---|
97 | return requiredVersion[0] === +parsedMajor
|
---|
98 | ? +parserMinor >= requiredVersion[1]
|
---|
99 | : +parsedMajor > requiredVersion[0];
|
---|
100 | });
|
---|
101 | };
|
---|
102 | const anyNode = browsers.some(b => /^node /.test(b));
|
---|
103 | const anyBrowser = browsers.some(b => /^(?!node)/.test(b));
|
---|
104 | const browserProperty = !anyBrowser ? false : anyNode ? null : true;
|
---|
105 | const nodeProperty = !anyNode ? false : anyBrowser ? null : true;
|
---|
106 | // Internet Explorer Mobile, Blackberry browser and Opera Mini are very old browsers, they do not support new features
|
---|
107 | const es6DynamicImport = rawChecker({
|
---|
108 | chrome: 63,
|
---|
109 | and_chr: 63,
|
---|
110 | edge: 79,
|
---|
111 | firefox: 67,
|
---|
112 | and_ff: 67,
|
---|
113 | // ie: Not supported
|
---|
114 | opera: 50,
|
---|
115 | op_mob: 46,
|
---|
116 | safari: [11, 1],
|
---|
117 | ios_saf: [11, 3],
|
---|
118 | samsung: [8, 2],
|
---|
119 | android: 63,
|
---|
120 | and_qq: [10, 4],
|
---|
121 | // baidu: Not supported
|
---|
122 | // and_uc: Not supported
|
---|
123 | // kaios: Not supported
|
---|
124 | // Since Node.js 13.14.0 no warning about usage, but it was added 8.5.0 with some limitations and it was improved in 12.0.0 and 13.2.0
|
---|
125 | node: [13, 14]
|
---|
126 | });
|
---|
127 |
|
---|
128 | return {
|
---|
129 | const: rawChecker({
|
---|
130 | chrome: 49,
|
---|
131 | and_chr: 49,
|
---|
132 | edge: 12,
|
---|
133 | // Prior to Firefox 13, <code>const</code> is implemented, but re-assignment is not failing.
|
---|
134 | // Prior to Firefox 46, a <code>TypeError</code> was thrown on redeclaration instead of a <code>SyntaxError</code>.
|
---|
135 | firefox: 36,
|
---|
136 | and_ff: 36,
|
---|
137 | // Not supported in for-in and for-of loops
|
---|
138 | // ie: Not supported
|
---|
139 | opera: 36,
|
---|
140 | op_mob: 36,
|
---|
141 | safari: [10, 0],
|
---|
142 | ios_saf: [10, 0],
|
---|
143 | // Before 5.0 supported correctly in strict mode, otherwise supported without block scope
|
---|
144 | samsung: [5, 0],
|
---|
145 | android: 37,
|
---|
146 | and_qq: [10, 4],
|
---|
147 | // Supported correctly in strict mode, otherwise supported without block scope
|
---|
148 | // baidu: Not supported
|
---|
149 | and_uc: [12, 12],
|
---|
150 | kaios: [2, 5],
|
---|
151 | node: [6, 0]
|
---|
152 | }),
|
---|
153 | arrowFunction: rawChecker({
|
---|
154 | chrome: 45,
|
---|
155 | and_chr: 45,
|
---|
156 | edge: 12,
|
---|
157 | // The initial implementation of arrow functions in Firefox made them automatically strict. This has been changed as of Firefox 24. The use of <code>'use strict';</code> is now required.
|
---|
158 | // Prior to Firefox 39, a line terminator (<code>\\n</code>) was incorrectly allowed after arrow function arguments. This has been fixed to conform to the ES2015 specification and code like <code>() \\n => {}</code> will now throw a <code>SyntaxError</code> in this and later versions.
|
---|
159 | firefox: 39,
|
---|
160 | and_ff: 39,
|
---|
161 | // ie: Not supported,
|
---|
162 | opera: 32,
|
---|
163 | op_mob: 32,
|
---|
164 | safari: 10,
|
---|
165 | ios_saf: 10,
|
---|
166 | samsung: [5, 0],
|
---|
167 | android: 45,
|
---|
168 | and_qq: [10, 4],
|
---|
169 | baidu: [7, 12],
|
---|
170 | and_uc: [12, 12],
|
---|
171 | kaios: [2, 5],
|
---|
172 | node: [6, 0]
|
---|
173 | }),
|
---|
174 | forOf: rawChecker({
|
---|
175 | chrome: 38,
|
---|
176 | and_chr: 38,
|
---|
177 | edge: 12,
|
---|
178 | // Prior to Firefox 51, using the for...of loop construct with the const keyword threw a SyntaxError ("missing = in const declaration").
|
---|
179 | firefox: 51,
|
---|
180 | and_ff: 51,
|
---|
181 | // ie: Not supported,
|
---|
182 | opera: 25,
|
---|
183 | op_mob: 25,
|
---|
184 | safari: 7,
|
---|
185 | ios_saf: 7,
|
---|
186 | samsung: [3, 0],
|
---|
187 | android: 38,
|
---|
188 | // and_qq: Unknown support
|
---|
189 | // baidu: Unknown support
|
---|
190 | // and_uc: Unknown support
|
---|
191 | // kaios: Unknown support
|
---|
192 | node: [0, 12]
|
---|
193 | }),
|
---|
194 | destructuring: rawChecker({
|
---|
195 | chrome: 49,
|
---|
196 | and_chr: 49,
|
---|
197 | edge: 14,
|
---|
198 | firefox: 41,
|
---|
199 | and_ff: 41,
|
---|
200 | // ie: Not supported,
|
---|
201 | opera: 36,
|
---|
202 | op_mob: 36,
|
---|
203 | safari: 8,
|
---|
204 | ios_saf: 8,
|
---|
205 | samsung: [5, 0],
|
---|
206 | android: 49,
|
---|
207 | // and_qq: Unknown support
|
---|
208 | // baidu: Unknown support
|
---|
209 | // and_uc: Unknown support
|
---|
210 | // kaios: Unknown support
|
---|
211 | node: [6, 0]
|
---|
212 | }),
|
---|
213 | bigIntLiteral: rawChecker({
|
---|
214 | chrome: 67,
|
---|
215 | and_chr: 67,
|
---|
216 | edge: 79,
|
---|
217 | firefox: 68,
|
---|
218 | and_ff: 68,
|
---|
219 | // ie: Not supported,
|
---|
220 | opera: 54,
|
---|
221 | op_mob: 48,
|
---|
222 | safari: 14,
|
---|
223 | ios_saf: 14,
|
---|
224 | samsung: [9, 2],
|
---|
225 | android: 67,
|
---|
226 | // and_qq: Not supported
|
---|
227 | // baidu: Not supported
|
---|
228 | // and_uc: Not supported
|
---|
229 | // kaios: Not supported
|
---|
230 | node: [10, 4]
|
---|
231 | }),
|
---|
232 | // Support syntax `import` and `export` and no limitations and bugs on Node.js
|
---|
233 | // Not include `export * as namespace`
|
---|
234 | module: rawChecker({
|
---|
235 | chrome: 61,
|
---|
236 | and_chr: 61,
|
---|
237 | edge: 16,
|
---|
238 | firefox: 60,
|
---|
239 | and_ff: 60,
|
---|
240 | // ie: Not supported,
|
---|
241 | opera: 48,
|
---|
242 | op_mob: 45,
|
---|
243 | safari: [10, 1],
|
---|
244 | ios_saf: [10, 3],
|
---|
245 | samsung: [8, 0],
|
---|
246 | android: 61,
|
---|
247 | and_qq: [10, 4],
|
---|
248 | // baidu: Not supported
|
---|
249 | // and_uc: Not supported
|
---|
250 | // kaios: Not supported
|
---|
251 | // Since Node.js 13.14.0 no warning about usage, but it was added 8.5.0 with some limitations and it was improved in 12.0.0 and 13.2.0
|
---|
252 | node: [13, 14]
|
---|
253 | }),
|
---|
254 | dynamicImport: es6DynamicImport,
|
---|
255 | dynamicImportInWorker: es6DynamicImport && !anyNode,
|
---|
256 | // browserslist does not have info about globalThis
|
---|
257 | // so this is based on mdn-browser-compat-data
|
---|
258 | globalThis: rawChecker({
|
---|
259 | chrome: 71,
|
---|
260 | and_chr: 71,
|
---|
261 | edge: 79,
|
---|
262 | firefox: 65,
|
---|
263 | and_ff: 65,
|
---|
264 | // ie: Not supported,
|
---|
265 | opera: 58,
|
---|
266 | op_mob: 50,
|
---|
267 | safari: [12, 1],
|
---|
268 | ios_saf: [12, 2],
|
---|
269 | samsung: [10, 1],
|
---|
270 | android: 71,
|
---|
271 | // and_qq: Unknown support
|
---|
272 | // baidu: Unknown support
|
---|
273 | // and_uc: Unknown support
|
---|
274 | // kaios: Unknown support
|
---|
275 | node: [12, 0]
|
---|
276 | }),
|
---|
277 |
|
---|
278 | browser: browserProperty,
|
---|
279 | electron: false,
|
---|
280 | node: nodeProperty,
|
---|
281 | nwjs: false,
|
---|
282 | web: browserProperty,
|
---|
283 | webworker: false,
|
---|
284 |
|
---|
285 | document: browserProperty,
|
---|
286 | fetchWasm: browserProperty,
|
---|
287 | global: nodeProperty,
|
---|
288 | importScripts: false,
|
---|
289 | importScriptsInWorker: true,
|
---|
290 | nodeBuiltins: nodeProperty,
|
---|
291 | require: nodeProperty
|
---|
292 | };
|
---|
293 | };
|
---|
294 |
|
---|
295 | module.exports = {
|
---|
296 | resolve,
|
---|
297 | load
|
---|
298 | };
|
---|