Merge pull request #11886 from webpack/issue-11876

fix: parsing of browserslist
This commit is contained in:
Tobias Koppers 2020-11-03 15:36:12 +01:00 committed by GitHub
commit bd44e0cf4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 1264 additions and 53 deletions

View File

@ -181,6 +181,8 @@
"destructure",
"onconnect",
"nwjs",
"redeclaration",
"kaios",
"webassemblyjs",
"fsevents",
@ -218,7 +220,8 @@
"codecov",
"opencollective",
"dependabot",
"browserslist"
"browserslist",
"samsunginternet"
],
"ignoreRegExpList": ["/Author.+/", "/data:.*/", "/\"mappings\":\".+\"/"],
"ignorePaths": ["**/dist/**", "examples/**/README.md"]

View File

@ -75,29 +75,6 @@ const load = (input, context) => {
* @returns {EcmaTargetProperties & PlatformTargetProperties & ApiTargetProperties} target properties
*/
const resolve = browsers => {
/**
* Checks only browser against the browserslist feature query
* @param {string} feature an ES feature to test
* @returns {boolean} true if supports
*/
const browserslistChecker = feature => {
const supportsFeature = browserslist(`supports ${feature}`);
return browsers.every(v => /^node /.test(v) || supportsFeature.includes(v));
};
/**
* Checks only node.js version against a version
* @param {number} major major version
* @param {number} minor minor version
* @returns {boolean} true if supports
*/
const nodeChecker = (major, minor = 0) => {
return browsers.every(v => {
const match = /^node (\d+)(?:\.\d+)?/.exec(v);
if (!match) return true;
const [, v1, v2] = match;
return major === +v1 ? +v2 >= minor : +v1 > major;
});
};
/**
* Checks all against a version number
* @param {Record<string, number | [number, number]>} versions first supported version
@ -105,50 +82,197 @@ const resolve = browsers => {
*/
const rawChecker = versions => {
return browsers.every(v => {
const match = /^([^ ]+) (\d+)(?:\.\d+)?/.exec(v);
if (!match) return false;
const [, name, major, minor] = match;
const version = versions[name];
if (!version) return false;
if (typeof version === "number") return +major >= version;
return version[0] === +major ? +minor >= version[1] : +major > version[0];
const [name, parsedVersion] = v.split(" ");
if (!name) return false;
const requiredVersion = versions[name];
if (!requiredVersion) return false;
const [parsedMajor, parserMinor] =
// safari TP supports all features for normal safari
parsedVersion === "TP"
? [Infinity, Infinity]
: parsedVersion.split(".");
if (typeof requiredVersion === "number") {
return +parsedMajor >= requiredVersion;
}
return requiredVersion[0] === +parsedMajor
? +parserMinor >= requiredVersion[1]
: +parsedMajor > requiredVersion[0];
});
};
const anyNode = browsers.some(b => /^node /.test(b));
const anyBrowser = browsers.some(b => /^(?!node)/.test(b));
const browserProperty = !anyBrowser ? false : anyNode ? null : true;
const nodeProperty = !anyNode ? false : anyBrowser ? null : true;
const es6 = browserslistChecker("es6");
const letConst = browserslistChecker("let");
const arrowFunctions = browserslistChecker("arrow-functions");
const es6DynamicImport = browserslistChecker("es6-module-dynamic-import");
const node6 = nodeChecker(6);
// Internet Explorer Mobile, Blackberry browser and Opera Mini are very old browsers, they do not support new features
const es6DynamicImport = rawChecker({
chrome: 63,
and_chr: 63,
edge: 79,
firefox: 67,
and_ff: 67,
// ie: Not supported
opera: 50,
op_mob: 46,
safari: [11, 1],
ios_saf: [11, 3],
samsung: [8, 2],
android: 63,
and_qq: [10, 4],
// baidu: Not supported
// and_uc: Not supported
// kaios: Not supported
// 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
node: [13, 14]
});
return {
const: letConst && node6,
arrowFunction: arrowFunctions && node6,
forOf: es6 && nodeChecker(5),
destructuring: es6 && node6,
bigIntLiteral: browserslistChecker("bigint") && nodeChecker(10, 4),
module: browserslistChecker("es6-module") && nodeChecker(12, 17),
dynamicImport: es6DynamicImport && nodeChecker(10, 17),
dynamicImportInWorker: es6DynamicImport && nodeChecker(Infinity),
const: rawChecker({
chrome: 49,
and_chr: 49,
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,
and_ff: 36,
// Not supported in for-in and for-of loops
// ie: Not supported
opera: 36,
op_mob: 36,
safari: [10, 0],
ios_saf: [10, 0],
// Before 5.0 supported correctly in strict mode, otherwise supported without block scope
samsung: [5, 0],
android: 37,
and_qq: [10, 4],
// Supported correctly in strict mode, otherwise supported without block scope
// baidu: Not supported
and_uc: [12, 12],
kaios: [2, 5],
node: [6, 0]
}),
arrowFunction: rawChecker({
chrome: 45,
and_chr: 45,
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,
and_ff: 39,
// ie: Not supported,
opera: 32,
op_mob: 32,
safari: 10,
ios_saf: 10,
samsung: [5, 0],
android: 45,
and_qq: [10, 4],
baidu: [7, 12],
and_uc: [12, 12],
kaios: [2, 5],
node: [6, 0]
}),
forOf: rawChecker({
chrome: 38,
and_chr: 38,
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,
and_ff: 51,
// ie: Not supported,
opera: 25,
op_mob: 25,
safari: 7,
ios_saf: 7,
samsung: [3, 0],
android: 38,
// and_qq: Unknown support
// baidu: Unknown support
// and_uc: Unknown support
// kaios: Unknown support
node: [0, 12]
}),
destructuring: rawChecker({
chrome: 49,
and_chr: 49,
edge: 14,
firefox: 41,
and_ff: 41,
// ie: Not supported,
opera: 36,
op_mob: 36,
safari: 8,
ios_saf: 8,
samsung: [5, 0],
android: 49,
// and_qq: Unknown support
// baidu: Unknown support
// and_uc: Unknown support
// kaios: Unknown support
node: [6, 0]
}),
bigIntLiteral: rawChecker({
chrome: 67,
and_chr: 67,
edge: 79,
firefox: 68,
and_ff: 68,
// ie: Not supported,
opera: 54,
op_mob: 48,
safari: 14,
ios_saf: 14,
samsung: [9, 2],
android: 67,
// and_qq: Not supported
// baidu: Not supported
// and_uc: Not supported
// kaios: Not supported
node: [10, 4]
}),
// Support syntax `import` and `export` and no limitations and bugs on Node.js
// Not include `export * as namespace`
module: rawChecker({
chrome: 61,
and_chr: 61,
edge: 16,
firefox: 60,
and_ff: 60,
// ie: Not supported,
opera: 48,
op_mob: 45,
safari: [10, 1],
ios_saf: [10, 3],
samsung: [8, 0],
android: 61,
and_qq: [10, 4],
// baidu: Not supported
// and_uc: Not supported
// kaios: Not supported
// 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
node: [13, 14]
}),
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,
chrome_android: 71,
and_chr: 71,
edge: 79,
firefox: 65,
firefox_android: 65,
and_ff: 65,
// ie: Not supported,
nodejs: 12,
opera: 58,
opera_android: 50,
op_mob: 50,
safari: [12, 1],
safari_ios: [12, 2],
// cspell:word samsunginternet
samsunginternet_android: [10, 0],
webview_android: 71
ios_saf: [12, 2],
samsung: [10, 1],
android: 71,
// and_qq: Unknown support
// baidu: Unknown support
// and_uc: Unknown support
// kaios: Unknown support
node: [12, 0]
}),
browser: browserProperty,

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@ module.exports = {
expect(compilation.outputOptions.environment).toMatchInlineSnapshot(`
Object {
"arrowFunction": true,
"bigIntLiteral": false,
"bigIntLiteral": true,
"const": true,
"destructuring": true,
"dynamicImport": false,

View File

@ -0,0 +1,84 @@
const { resolve } = require("../lib/config/browserslistTargetHandler");
describe("browserslist target", () => {
const tests = [
// IE
["ie 11"],
["ie_mob 11"],
// Edge
["edge 79"],
// Android
["android 4"],
["android 4.1"],
["android 4.4.3-4.4.4"],
["android 81"],
// Chrome
// Browserslist return `chrome` versions for `electron 11.0` query
["chrome 80"],
["and_chr 80"],
// Firefox
["firefox 68"],
["and_ff 68"],
// Opera
["opera 54"],
["op_mob 54"],
// Safari
// Browserslist return `safari` versions for `phantomjs 2.1` query
["safari 10"],
["safari TP"],
["safari 11"],
["safari 12.0"],
["safari 12.1"],
["safari 13"],
["ios_saf 12.0-12.1"],
// Samsung
["samsung 4"],
["samsung 9.2"],
["samsung 11.1-11.2"],
// Opera mini
["op_mini all"],
// BlackBerry
["bb 10"],
// Node
["node 0.10.0"],
["node 0.12.0"],
["node 10.0.0"],
["node 10.17.0"],
["node 12.19.0"],
// UC browsers for Android
["and_uc 12.12"],
// QQ browser
["and_qq 10.4"],
// Kaios
["kaios 2.5"],
// Baidu
["baidu 7.12"],
// Multiple
["firefox 80", "chrome 80"],
["chrome 80", "node 12.19.0"],
// Unknown
["unknown 50"]
];
for (const test of tests) {
it(`${JSON.stringify(test)}`, () => {
expect(resolve(test)).toMatchSnapshot();
});
}
});