webpack/lib/config/browserslistTargetHandler.js

351 lines
8.4 KiB
JavaScript
Raw Normal View History

2020-09-24 19:33:09 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Sergey Melyukov @smelukov
*/
2020-09-24 19:42:27 +08:00
"use strict";
2020-09-24 19:33:09 +08:00
2020-09-24 20:46:28 +08:00
const path = require("path");
2025-07-03 17:06:45 +08:00
const browserslist = require("browserslist");
2020-09-24 19:33:09 +08:00
/** @typedef {import("./target").ApiTargetProperties} ApiTargetProperties */
/** @typedef {import("./target").EcmaTargetProperties} EcmaTargetProperties */
/** @typedef {import("./target").PlatformTargetProperties} PlatformTargetProperties */
2020-09-25 04:10:33 +08:00
// [[C:]/path/to/config][:env]
2020-09-25 04:44:41 +08:00
const inputRx = /^(?:((?:[A-Z]:)?[/\\].*?))?(?::(.+?))?$/i;
2020-09-24 19:33:09 +08:00
/**
2023-05-24 05:25:06 +08:00
* @param {string | null | undefined} input input string
* @param {string} context the context directory
* @returns {string[] | undefined} selected browsers
2020-09-24 19:33:09 +08:00
*/
const load = (input, context) => {
// browserslist:path-to-config
// browserslist:path-to-config:env
if (input && path.isAbsolute(input)) {
2020-09-24 20:41:54 +08:00
const [, configPath, env] = inputRx.exec(input) || [];
const config = browserslist.loadConfig({
config: configPath,
env
});
2020-09-24 20:41:54 +08:00
return browserslist(config, { env });
2020-09-24 20:41:54 +08:00
}
2020-09-24 19:33:09 +08:00
const env = input || undefined;
const config = browserslist.loadConfig({
path: context,
env
});
// browserslist
// browserslist:env
if (config) {
try {
return browserslist(config, { env, throwOnMissing: true });
} catch (_err) {
// Nothing, no `env` was found in browserslist, maybe input is `queries`
}
}
// browserslist:query
if (env) {
return browserslist(env);
}
};
2020-09-24 19:33:09 +08:00
/**
* @param {string[]} browsers supported browsers list
* @returns {EcmaTargetProperties & PlatformTargetProperties & ApiTargetProperties} target properties
2020-09-24 19:33:09 +08:00
*/
const resolve = (browsers) => {
/**
* Checks all against a version number
2020-09-28 17:09:07 +08:00
* @param {Record<string, number | [number, number]>} versions first supported version
* @returns {boolean} true if supports
*/
const rawChecker = (versions) =>
browsers.every((v) => {
2020-10-31 21:54:47 +08:00
const [name, parsedVersion] = v.split(" ");
if (!name) return false;
const requiredVersion = versions[name];
if (!requiredVersion) return false;
2020-10-31 22:03:54 +08:00
const [parsedMajor, parserMinor] =
// safari TP supports all features for normal safari
parsedVersion === "TP"
? [Infinity, Infinity]
: parsedVersion.includes("-")
? parsedVersion.split("-")[0].split(".")
: parsedVersion.split(".");
2020-10-31 21:54:47 +08:00
if (typeof requiredVersion === "number") {
2024-07-31 11:11:11 +08:00
return Number(parsedMajor) >= requiredVersion;
2020-10-31 21:54:47 +08:00
}
2024-07-31 11:11:11 +08:00
return requiredVersion[0] === Number(parsedMajor)
? Number(parserMinor) >= requiredVersion[1]
: Number(parsedMajor) > requiredVersion[0];
});
const anyNode = browsers.some((b) => b.startsWith("node "));
const anyBrowser = browsers.some((b) => /^(?!node)/.test(b));
const browserProperty = !anyBrowser ? false : anyNode ? null : true;
const nodeProperty = !anyNode ? false : anyBrowser ? null : true;
2020-11-03 00:08:06 +08:00
// Internet Explorer Mobile, Blackberry browser and Opera Mini are very old browsers, they do not support new features
2020-10-31 21:54:47 +08:00
const es6DynamicImport = rawChecker({
2024-07-31 09:37:24 +08:00
/* eslint-disable camelcase */
2020-10-31 21:54:47 +08:00
chrome: 63,
2020-10-31 22:17:47 +08:00
and_chr: 63,
2020-10-31 21:54:47 +08:00
edge: 79,
firefox: 67,
2020-10-31 22:17:47 +08:00
and_ff: 67,
2020-11-03 00:08:06 +08:00
// ie: Not supported
2020-10-31 21:54:47 +08:00
opera: 50,
2020-11-02 23:21:37 +08:00
op_mob: 46,
2020-10-31 21:54:47 +08:00
safari: [11, 1],
2020-10-31 22:17:47 +08:00
ios_saf: [11, 3],
2020-11-03 00:08:06 +08:00
samsung: [8, 2],
2020-11-02 23:21:37 +08:00
android: 63,
2020-11-03 00:08:06 +08:00
and_qq: [10, 4],
2024-01-31 21:15:13 +08:00
baidu: [13, 18],
and_uc: [15, 5],
kaios: [3, 0],
2022-02-21 22:22:42 +08:00
node: [12, 17]
2024-07-31 09:37:24 +08:00
/* eslint-enable camelcase */
2020-10-31 21:54:47 +08:00
});
return {
2024-07-31 09:37:24 +08:00
/* eslint-disable camelcase */
2020-10-31 21:54:47 +08:00
const: rawChecker({
2020-11-03 00:08:06 +08:00
chrome: 49,
and_chr: 49,
2020-10-31 21:54:47 +08:00
edge: 12,
// Prior to Firefox 13, <code>const</code> is implemented, but re-assignment is not failing.
// Prior to Firefox 46, a <code>TypeError</code> was thrown on redeclaration instead of a <code>SyntaxError</code>.
firefox: 36,
2020-10-31 22:17:47 +08:00
and_ff: 36,
2020-11-02 23:21:37 +08:00
// Not supported in for-in and for-of loops
// ie: Not supported
2020-11-03 00:08:06 +08:00
opera: 36,
op_mob: 36,
safari: [10, 0],
ios_saf: [10, 0],
2020-11-02 23:21:37 +08:00
// Before 5.0 supported correctly in strict mode, otherwise supported without block scope
samsung: [5, 0],
android: 37,
2020-11-03 00:08:06 +08:00
and_qq: [10, 4],
// Supported correctly in strict mode, otherwise supported without block scope
2024-01-31 21:15:13 +08:00
baidu: [13, 18],
2020-11-03 00:08:06 +08:00
and_uc: [12, 12],
kaios: [2, 5],
2020-11-02 23:21:37 +08:00
node: [6, 0]
2020-10-31 21:54:47 +08:00
}),
arrowFunction: rawChecker({
chrome: 45,
2020-10-31 22:17:47 +08:00
and_chr: 45,
2020-10-31 21:54:47 +08:00
edge: 12,
// 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.
// 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.
firefox: 39,
2020-10-31 22:17:47 +08:00
and_ff: 39,
2020-10-31 21:54:47 +08:00
// ie: Not supported,
opera: 32,
2020-11-02 23:21:37 +08:00
op_mob: 32,
2020-10-31 21:54:47 +08:00
safari: 10,
2020-10-31 22:17:47 +08:00
ios_saf: 10,
samsung: [5, 0],
2020-11-02 23:21:37 +08:00
android: 45,
2020-11-03 00:08:06 +08:00
and_qq: [10, 4],
baidu: [7, 12],
and_uc: [12, 12],
kaios: [2, 5],
2020-11-02 23:21:37 +08:00
node: [6, 0]
2020-10-31 21:54:47 +08:00
}),
2020-10-31 01:24:42 +08:00
forOf: rawChecker({
chrome: 38,
2020-10-31 22:17:47 +08:00
and_chr: 38,
2020-10-31 01:24:42 +08:00
edge: 12,
// Prior to Firefox 51, using the for...of loop construct with the const keyword threw a SyntaxError ("missing = in const declaration").
firefox: 51,
2020-10-31 22:17:47 +08:00
and_ff: 51,
2020-10-31 01:24:42 +08:00
// ie: Not supported,
opera: 25,
2020-11-02 23:21:37 +08:00
op_mob: 25,
2020-10-31 01:24:42 +08:00
safari: 7,
2020-10-31 22:17:47 +08:00
ios_saf: 7,
samsung: [3, 0],
2020-11-02 23:21:37 +08:00
android: 38,
2020-11-03 00:08:06 +08:00
// and_qq: Unknown support
// baidu: Unknown support
// and_uc: Unknown support
kaios: [3, 0],
2020-11-02 23:21:37 +08:00
node: [0, 12]
2020-10-31 01:24:42 +08:00
}),
destructuring: rawChecker({
chrome: 49,
2020-10-31 22:17:47 +08:00
and_chr: 49,
2020-10-31 01:24:42 +08:00
edge: 14,
firefox: 41,
2020-10-31 22:17:47 +08:00
and_ff: 41,
2020-10-31 01:24:42 +08:00
// ie: Not supported,
opera: 36,
2020-11-02 23:21:37 +08:00
op_mob: 36,
2020-10-31 01:24:42 +08:00
safari: 8,
2020-10-31 22:17:47 +08:00
ios_saf: 8,
samsung: [5, 0],
2020-11-02 23:21:37 +08:00
android: 49,
2020-11-03 00:08:06 +08:00
// and_qq: Unknown support
// baidu: Unknown support
// and_uc: Unknown support
kaios: [2, 5],
2020-11-02 23:21:37 +08:00
node: [6, 0]
2020-10-31 01:24:42 +08:00
}),
2020-10-31 21:54:47 +08:00
bigIntLiteral: rawChecker({
chrome: 67,
2020-10-31 22:17:47 +08:00
and_chr: 67,
2020-10-31 21:54:47 +08:00
edge: 79,
firefox: 68,
2020-10-31 22:17:47 +08:00
and_ff: 68,
2020-10-31 21:54:47 +08:00
// ie: Not supported,
opera: 54,
2020-11-02 23:21:37 +08:00
op_mob: 48,
2020-10-31 21:54:47 +08:00
safari: 14,
2020-10-31 22:17:47 +08:00
ios_saf: 14,
2020-11-03 00:08:06 +08:00
samsung: [9, 2],
2020-11-02 23:21:37 +08:00
android: 67,
2024-01-31 21:15:13 +08:00
and_qq: [13, 1],
baidu: [13, 18],
and_uc: [15, 5],
kaios: [3, 0],
2020-11-02 23:21:37 +08:00
node: [10, 4]
2020-10-31 21:54:47 +08:00
}),
// Support syntax `import` and `export` and no limitations and bugs on Node.js
// Not include `export * as namespace`
module: rawChecker({
chrome: 61,
2020-10-31 22:17:47 +08:00
and_chr: 61,
2020-10-31 21:54:47 +08:00
edge: 16,
firefox: 60,
2020-10-31 22:17:47 +08:00
and_ff: 60,
2020-10-31 21:54:47 +08:00
// ie: Not supported,
opera: 48,
2020-11-02 23:21:37 +08:00
op_mob: 45,
2020-10-31 21:54:47 +08:00
safari: [10, 1],
2020-10-31 22:17:47 +08:00
ios_saf: [10, 3],
2020-11-02 23:21:37 +08:00
samsung: [8, 0],
2020-11-03 00:08:06 +08:00
android: 61,
and_qq: [10, 4],
2024-01-31 21:15:13 +08:00
baidu: [13, 18],
and_uc: [15, 5],
kaios: [3, 0],
2022-02-21 22:22:42 +08:00
node: [12, 17]
2020-10-31 21:54:47 +08:00
}),
dynamicImport: es6DynamicImport,
dynamicImportInWorker: es6DynamicImport && !anyNode,
// browserslist does not have info about globalThis
// so this is based on mdn-browser-compat-data
globalThis: rawChecker({
chrome: 71,
2020-10-31 22:17:47 +08:00
and_chr: 71,
edge: 79,
firefox: 65,
2020-10-31 22:17:47 +08:00
and_ff: 65,
// ie: Not supported,
opera: 58,
2020-11-02 23:21:37 +08:00
op_mob: 50,
safari: [12, 1],
2020-10-31 22:17:47 +08:00
ios_saf: [12, 2],
2020-11-03 00:08:06 +08:00
samsung: [10, 1],
2020-11-02 23:21:37 +08:00
android: 71,
2020-11-03 00:08:06 +08:00
// and_qq: Unknown support
// baidu: Unknown support
// and_uc: Unknown support
kaios: [3, 0],
2022-02-21 22:22:42 +08:00
node: 12
}),
optionalChaining: rawChecker({
chrome: 80,
and_chr: 80,
edge: 80,
firefox: 74,
and_ff: 79,
// ie: Not supported,
opera: 67,
op_mob: 64,
safari: [13, 1],
ios_saf: [13, 4],
samsung: 13,
android: 80,
// and_qq: Not supported
// baidu: Not supported
// and_uc: Not supported
kaios: [3, 0],
node: 14
}),
templateLiteral: rawChecker({
chrome: 41,
and_chr: 41,
edge: 13,
firefox: 34,
and_ff: 34,
// ie: Not supported,
opera: 29,
op_mob: 64,
safari: [9, 1],
ios_saf: 9,
samsung: 4,
android: 41,
and_qq: [10, 4],
baidu: [7, 12],
and_uc: [12, 12],
kaios: [2, 5],
node: 4
}),
2024-01-14 18:11:12 +08:00
asyncFunction: rawChecker({
chrome: 55,
and_chr: 55,
edge: 15,
firefox: 52,
and_ff: 52,
// ie: Not supported,
opera: 42,
op_mob: 42,
safari: 11,
ios_saf: 11,
samsung: [6, 2],
2024-01-14 18:11:12 +08:00
android: 55,
and_qq: [13, 1],
baidu: [13, 18],
and_uc: [15, 5],
kaios: 3,
2024-01-14 18:11:12 +08:00
node: [7, 6]
}),
2024-07-31 09:37:24 +08:00
/* eslint-enable camelcase */
browser: browserProperty,
electron: false,
node: nodeProperty,
nwjs: false,
web: browserProperty,
webworker: false,
document: browserProperty,
fetchWasm: browserProperty,
global: nodeProperty,
importScripts: false,
importScriptsInWorker: true,
nodeBuiltins: nodeProperty,
nodePrefixForCoreModules:
nodeProperty &&
!browsers.some((b) => b.startsWith("node 15")) &&
rawChecker({
node: [14, 18]
}),
require: nodeProperty
2020-09-24 19:33:09 +08:00
};
};
module.exports = {
2025-07-03 17:06:45 +08:00
load,
resolve
2020-09-24 19:33:09 +08:00
};