Skip to content

Commit b969687

Browse files
authored
fix: treat a bare-major browserslist target as <major>.0 (#22280)
Browserslist emits targets like `safari 10` with no minor component. In `browserslistTargetHandler`, when a feature's first supported version is a `[major, minor]` pair, the missing minor coerced to `NaN`, so the equal-major branch (`NaN >= requiredMinor`) was always false and the feature was wrongly reported unsupported. Treat a missing minor as `0`, so `safari 10` resolves the same as `safari 10.0` and matches features introduced at `<major>.0`.
1 parent d7237bf commit b969687

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Treat a browserslist target given at a bare major version (e.g. `safari 10`) as `<major>.0` when a feature's first supported version is a `[major, minor]` pair, so it is no longer reported as unsupported.

‎lib/config/browserslistTargetHandler.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const resolve = (browsers) => {
122122
return Number(parsedMajor) >= requiredVersion;
123123
}
124124
return requiredVersion[0] === Number(parsedMajor)
125-
? Number(parserMinor) >= requiredVersion[1]
125+
? Number(parserMinor || 0) >= requiredVersion[1]
126126
: Number(parsedMajor) > requiredVersion[0];
127127
});
128128
const anyNode = browsers.some((b) => b.startsWith("node "));

‎test/unitCases/__snapshots__/target-browserslist.unittest.js.snap‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ Object {
12661266
"asyncFunction": false,
12671267
"bigIntLiteral": false,
12681268
"browser": true,
1269-
"const": false,
1269+
"const": true,
12701270
"destructuring": true,
12711271
"document": true,
12721272
"dynamicImport": false,
@@ -1281,7 +1281,7 @@ Object {
12811281
"importMetaDirnameAndFilename": false,
12821282
"importScripts": false,
12831283
"importScriptsInWorker": true,
1284-
"let": false,
1284+
"let": true,
12851285
"logicalAssignment": false,
12861286
"methodShorthand": true,
12871287
"module": false,

‎test/unitCases/target-browserslist.unittest.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,27 @@ describe("browserslist target", () => {
123123
});
124124
}
125125

126+
// A browser listed at a bare major version (no minor component, e.g.
127+
// `safari 10`, which browserslist emits) must be treated as `<major>.0`, so
128+
// it matches a feature whose first supported version is given as a
129+
// `[major, minor]` pair. Otherwise the missing minor coerces to `NaN` and
130+
// the equal-major comparison wrongly reports the feature as unsupported.
131+
describe("bare major version equals `<major>.0`", () => {
132+
it("resolves `safari 10` the same as `safari 10.0`", () => {
133+
expect(resolve(["safari 10"])).toEqual(resolve(["safari 10.0"]));
134+
});
135+
136+
it("supports `const`/`let` at the exact required major", () => {
137+
const result = resolve(["safari 10"]);
138+
expect(result.const).toBe(true);
139+
expect(result.let).toBe(true);
140+
});
141+
142+
it("still rejects a major below the required one", () => {
143+
expect(resolve(["safari 9"]).const).toBe(false);
144+
});
145+
});
146+
126147
describe("load", () => {
127148
const context = path.join(
128149
__dirname,

0 commit comments

Comments
 (0)