[79a0317] | 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 | null | undefined} 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 | null | undefined} 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 =
|
---|
| 61 | query ||
|
---|
| 62 | (configPath
|
---|
| 63 | ? browserslist.loadConfig({
|
---|
| 64 | config: configPath,
|
---|
| 65 | env
|
---|
| 66 | })
|
---|
| 67 | : browserslist.loadConfig({ path: context, env }));
|
---|
| 68 |
|
---|
| 69 | if (!config) return;
|
---|
| 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 | 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.includes("-")
|
---|
| 94 | ? parsedVersion.split("-")[0].split(".")
|
---|
| 95 | : parsedVersion.split(".");
|
---|
| 96 | if (typeof requiredVersion === "number") {
|
---|
| 97 | return Number(parsedMajor) >= requiredVersion;
|
---|
| 98 | }
|
---|
| 99 | return requiredVersion[0] === Number(parsedMajor)
|
---|
| 100 | ? Number(parserMinor) >= requiredVersion[1]
|
---|
| 101 | : Number(parsedMajor) > requiredVersion[0];
|
---|
| 102 | });
|
---|
| 103 | const anyNode = browsers.some(b => b.startsWith("node "));
|
---|
| 104 | const anyBrowser = browsers.some(b => /^(?!node)/.test(b));
|
---|
| 105 | const browserProperty = !anyBrowser ? false : anyNode ? null : true;
|
---|
| 106 | const nodeProperty = !anyNode ? false : anyBrowser ? null : true;
|
---|
| 107 | // Internet Explorer Mobile, Blackberry browser and Opera Mini are very old browsers, they do not support new features
|
---|
| 108 | const es6DynamicImport = rawChecker({
|
---|
| 109 | /* eslint-disable camelcase */
|
---|
| 110 | chrome: 63,
|
---|
| 111 | and_chr: 63,
|
---|
| 112 | edge: 79,
|
---|
| 113 | firefox: 67,
|
---|
| 114 | and_ff: 67,
|
---|
| 115 | // ie: Not supported
|
---|
| 116 | opera: 50,
|
---|
| 117 | op_mob: 46,
|
---|
| 118 | safari: [11, 1],
|
---|
| 119 | ios_saf: [11, 3],
|
---|
| 120 | samsung: [8, 2],
|
---|
| 121 | android: 63,
|
---|
| 122 | and_qq: [10, 4],
|
---|
| 123 | baidu: [13, 18],
|
---|
| 124 | and_uc: [15, 5],
|
---|
| 125 | kaios: [3, 0],
|
---|
| 126 | node: [12, 17]
|
---|
| 127 | /* eslint-enable camelcase */
|
---|
| 128 | });
|
---|
| 129 |
|
---|
| 130 | return {
|
---|
| 131 | /* eslint-disable camelcase */
|
---|
| 132 | const: rawChecker({
|
---|
| 133 | chrome: 49,
|
---|
| 134 | and_chr: 49,
|
---|
| 135 | edge: 12,
|
---|
| 136 | // Prior to Firefox 13, <code>const</code> is implemented, but re-assignment is not failing.
|
---|
| 137 | // Prior to Firefox 46, a <code>TypeError</code> was thrown on redeclaration instead of a <code>SyntaxError</code>.
|
---|
| 138 | firefox: 36,
|
---|
| 139 | and_ff: 36,
|
---|
| 140 | // Not supported in for-in and for-of loops
|
---|
| 141 | // ie: Not supported
|
---|
| 142 | opera: 36,
|
---|
| 143 | op_mob: 36,
|
---|
| 144 | safari: [10, 0],
|
---|
| 145 | ios_saf: [10, 0],
|
---|
| 146 | // Before 5.0 supported correctly in strict mode, otherwise supported without block scope
|
---|
| 147 | samsung: [5, 0],
|
---|
| 148 | android: 37,
|
---|
| 149 | and_qq: [10, 4],
|
---|
| 150 | // Supported correctly in strict mode, otherwise supported without block scope
|
---|
| 151 | baidu: [13, 18],
|
---|
| 152 | and_uc: [12, 12],
|
---|
| 153 | kaios: [2, 5],
|
---|
| 154 | node: [6, 0]
|
---|
| 155 | }),
|
---|
| 156 | arrowFunction: rawChecker({
|
---|
| 157 | chrome: 45,
|
---|
| 158 | and_chr: 45,
|
---|
| 159 | edge: 12,
|
---|
| 160 | // 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.
|
---|
| 161 | // 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.
|
---|
| 162 | firefox: 39,
|
---|
| 163 | and_ff: 39,
|
---|
| 164 | // ie: Not supported,
|
---|
| 165 | opera: 32,
|
---|
| 166 | op_mob: 32,
|
---|
| 167 | safari: 10,
|
---|
| 168 | ios_saf: 10,
|
---|
| 169 | samsung: [5, 0],
|
---|
| 170 | android: 45,
|
---|
| 171 | and_qq: [10, 4],
|
---|
| 172 | baidu: [7, 12],
|
---|
| 173 | and_uc: [12, 12],
|
---|
| 174 | kaios: [2, 5],
|
---|
| 175 | node: [6, 0]
|
---|
| 176 | }),
|
---|
| 177 | forOf: rawChecker({
|
---|
| 178 | chrome: 38,
|
---|
| 179 | and_chr: 38,
|
---|
| 180 | edge: 12,
|
---|
| 181 | // Prior to Firefox 51, using the for...of loop construct with the const keyword threw a SyntaxError ("missing = in const declaration").
|
---|
| 182 | firefox: 51,
|
---|
| 183 | and_ff: 51,
|
---|
| 184 | // ie: Not supported,
|
---|
| 185 | opera: 25,
|
---|
| 186 | op_mob: 25,
|
---|
| 187 | safari: 7,
|
---|
| 188 | ios_saf: 7,
|
---|
| 189 | samsung: [3, 0],
|
---|
| 190 | android: 38,
|
---|
| 191 | // and_qq: Unknown support
|
---|
| 192 | // baidu: Unknown support
|
---|
| 193 | // and_uc: Unknown support
|
---|
| 194 | kaios: [3, 0],
|
---|
| 195 | node: [0, 12]
|
---|
| 196 | }),
|
---|
| 197 | destructuring: rawChecker({
|
---|
| 198 | chrome: 49,
|
---|
| 199 | and_chr: 49,
|
---|
| 200 | edge: 14,
|
---|
| 201 | firefox: 41,
|
---|
| 202 | and_ff: 41,
|
---|
| 203 | // ie: Not supported,
|
---|
| 204 | opera: 36,
|
---|
| 205 | op_mob: 36,
|
---|
| 206 | safari: 8,
|
---|
| 207 | ios_saf: 8,
|
---|
| 208 | samsung: [5, 0],
|
---|
| 209 | android: 49,
|
---|
| 210 | // and_qq: Unknown support
|
---|
| 211 | // baidu: Unknown support
|
---|
| 212 | // and_uc: Unknown support
|
---|
| 213 | kaios: [2, 5],
|
---|
| 214 | node: [6, 0]
|
---|
| 215 | }),
|
---|
| 216 | bigIntLiteral: rawChecker({
|
---|
| 217 | chrome: 67,
|
---|
| 218 | and_chr: 67,
|
---|
| 219 | edge: 79,
|
---|
| 220 | firefox: 68,
|
---|
| 221 | and_ff: 68,
|
---|
| 222 | // ie: Not supported,
|
---|
| 223 | opera: 54,
|
---|
| 224 | op_mob: 48,
|
---|
| 225 | safari: 14,
|
---|
| 226 | ios_saf: 14,
|
---|
| 227 | samsung: [9, 2],
|
---|
| 228 | android: 67,
|
---|
| 229 | and_qq: [13, 1],
|
---|
| 230 | baidu: [13, 18],
|
---|
| 231 | and_uc: [15, 5],
|
---|
| 232 | kaios: [3, 0],
|
---|
| 233 | node: [10, 4]
|
---|
| 234 | }),
|
---|
| 235 | // Support syntax `import` and `export` and no limitations and bugs on Node.js
|
---|
| 236 | // Not include `export * as namespace`
|
---|
| 237 | module: rawChecker({
|
---|
| 238 | chrome: 61,
|
---|
| 239 | and_chr: 61,
|
---|
| 240 | edge: 16,
|
---|
| 241 | firefox: 60,
|
---|
| 242 | and_ff: 60,
|
---|
| 243 | // ie: Not supported,
|
---|
| 244 | opera: 48,
|
---|
| 245 | op_mob: 45,
|
---|
| 246 | safari: [10, 1],
|
---|
| 247 | ios_saf: [10, 3],
|
---|
| 248 | samsung: [8, 0],
|
---|
| 249 | android: 61,
|
---|
| 250 | and_qq: [10, 4],
|
---|
| 251 | baidu: [13, 18],
|
---|
| 252 | and_uc: [15, 5],
|
---|
| 253 | kaios: [3, 0],
|
---|
| 254 | node: [12, 17]
|
---|
| 255 | }),
|
---|
| 256 | dynamicImport: es6DynamicImport,
|
---|
| 257 | dynamicImportInWorker: es6DynamicImport && !anyNode,
|
---|
| 258 | // browserslist does not have info about globalThis
|
---|
| 259 | // so this is based on mdn-browser-compat-data
|
---|
| 260 | globalThis: rawChecker({
|
---|
| 261 | chrome: 71,
|
---|
| 262 | and_chr: 71,
|
---|
| 263 | edge: 79,
|
---|
| 264 | firefox: 65,
|
---|
| 265 | and_ff: 65,
|
---|
| 266 | // ie: Not supported,
|
---|
| 267 | opera: 58,
|
---|
| 268 | op_mob: 50,
|
---|
| 269 | safari: [12, 1],
|
---|
| 270 | ios_saf: [12, 2],
|
---|
| 271 | samsung: [10, 1],
|
---|
| 272 | android: 71,
|
---|
| 273 | // and_qq: Unknown support
|
---|
| 274 | // baidu: Unknown support
|
---|
| 275 | // and_uc: Unknown support
|
---|
| 276 | kaios: [3, 0],
|
---|
| 277 | node: 12
|
---|
| 278 | }),
|
---|
| 279 | optionalChaining: rawChecker({
|
---|
| 280 | chrome: 80,
|
---|
| 281 | and_chr: 80,
|
---|
| 282 | edge: 80,
|
---|
| 283 | firefox: 74,
|
---|
| 284 | and_ff: 79,
|
---|
| 285 | // ie: Not supported,
|
---|
| 286 | opera: 67,
|
---|
| 287 | op_mob: 64,
|
---|
| 288 | safari: [13, 1],
|
---|
| 289 | ios_saf: [13, 4],
|
---|
| 290 | samsung: 13,
|
---|
| 291 | android: 80,
|
---|
| 292 | // and_qq: Not supported
|
---|
| 293 | // baidu: Not supported
|
---|
| 294 | // and_uc: Not supported
|
---|
| 295 | kaios: [3, 0],
|
---|
| 296 | node: 14
|
---|
| 297 | }),
|
---|
| 298 | templateLiteral: rawChecker({
|
---|
| 299 | chrome: 41,
|
---|
| 300 | and_chr: 41,
|
---|
| 301 | edge: 13,
|
---|
| 302 | firefox: 34,
|
---|
| 303 | and_ff: 34,
|
---|
| 304 | // ie: Not supported,
|
---|
| 305 | opera: 29,
|
---|
| 306 | op_mob: 64,
|
---|
| 307 | safari: [9, 1],
|
---|
| 308 | ios_saf: 9,
|
---|
| 309 | samsung: 4,
|
---|
| 310 | android: 41,
|
---|
| 311 | and_qq: [10, 4],
|
---|
| 312 | baidu: [7, 12],
|
---|
| 313 | and_uc: [12, 12],
|
---|
| 314 | kaios: [2, 5],
|
---|
| 315 | node: 4
|
---|
| 316 | }),
|
---|
| 317 | asyncFunction: rawChecker({
|
---|
| 318 | chrome: 55,
|
---|
| 319 | and_chr: 55,
|
---|
| 320 | edge: 15,
|
---|
| 321 | firefox: 52,
|
---|
| 322 | and_ff: 52,
|
---|
| 323 | // ie: Not supported,
|
---|
| 324 | opera: 42,
|
---|
| 325 | op_mob: 42,
|
---|
| 326 | safari: 11,
|
---|
| 327 | ios_saf: 11,
|
---|
| 328 | samsung: [6, 2],
|
---|
| 329 | android: 55,
|
---|
| 330 | and_qq: [13, 1],
|
---|
| 331 | baidu: [13, 18],
|
---|
| 332 | and_uc: [15, 5],
|
---|
| 333 | kaios: 3,
|
---|
| 334 | node: [7, 6]
|
---|
| 335 | }),
|
---|
| 336 | /* eslint-enable camelcase */
|
---|
| 337 | browser: browserProperty,
|
---|
| 338 | electron: false,
|
---|
| 339 | node: nodeProperty,
|
---|
| 340 | nwjs: false,
|
---|
| 341 | web: browserProperty,
|
---|
| 342 | webworker: false,
|
---|
| 343 |
|
---|
| 344 | document: browserProperty,
|
---|
| 345 | fetchWasm: browserProperty,
|
---|
| 346 | global: nodeProperty,
|
---|
| 347 | importScripts: false,
|
---|
| 348 | importScriptsInWorker: true,
|
---|
| 349 | nodeBuiltins: nodeProperty,
|
---|
| 350 | nodePrefixForCoreModules:
|
---|
| 351 | nodeProperty &&
|
---|
| 352 | !browsers.some(b => b.startsWith("node 15")) &&
|
---|
| 353 | rawChecker({
|
---|
| 354 | node: [14, 18]
|
---|
| 355 | }),
|
---|
| 356 | require: nodeProperty
|
---|
| 357 | };
|
---|
| 358 | };
|
---|
| 359 |
|
---|
| 360 | module.exports = {
|
---|
| 361 | resolve,
|
---|
| 362 | load
|
---|
| 363 | };
|
---|