Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct ECMA version browserslist mapping #301

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 76 additions & 84 deletions packages/core/src/utils/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,112 +56,104 @@ export const ESX_TO_BROWSERSLIST: Record<
> &
Record<LatestEcmaVersions, (target: RsbuildConfigOutputTarget) => string[]> =
{
es5: {
chrome: '5.0.0',
edge: '12.0.0',
firefox: '2.0.0',
ie: '9.0.0',
ios: '6.0.0',
node: '0.4.0',
opera: '10.10.0',
safari: '3.1.0',
},
es6: {
Chrome: '63.0.0',
Edge: '79.0.0',
Firefox: '67.0.0',
iOS: '13.0.0',
Node: ['node > 12.20.0 and node < 13.0.0', 'node > 13.2.0'],
Opera: '50.0.0',
Safari: '13.0.0',
chrome: '63.0.0',
edge: '79.0.0',
firefox: '67.0.0',
ios: '13.0.0',
node: '13.2.0',
opera: '50.0.0',
safari: '13.0.0',
},
es2015: {
Chrome: '63.0.0',
Edge: '79.0.0',
Firefox: '67.0.0',
iOS: '13.0.0',
Node: '10.0.0',
Opera: '50.0.0',
Safari: '13.0.0',
chrome: '63.0.0',
edge: '79.0.0',
firefox: '67.0.0',
ios: '13.0.0',
node: '13.2.0',
opera: '50.0.0',
safari: '13.0.0',
},
es2016: {
Chrome: '52.0.0',
Edge: '14.0.0',
Firefox: '52.0.0',
iOS: '10.3.0',
Node: '7.0.0',
Opera: '39.0.0',
Safari: '10.1.0',
chrome: '63.0.0',
edge: '79.0.0',
firefox: '67.0.0',
ios: '13.0.0',
node: '13.2.0',
opera: '50.0.0',
safari: '13.0.0',
},
es2017: {
Chrome: '55.0.0',
Edge: '15.0.0',
Firefox: '52.0.0',
iOS: '11.0.0',
Node: '7.6.0',
Opera: '42.0.0',
Safari: '11.0.0',
chrome: '63.0.0',
edge: '79.0.0',
firefox: '67.0.0',
ios: '13.0.0',
node: '13.2.0',
opera: '50.0.0',
safari: '13.0.0',
},
es2018: {
Chrome: '64.0.0',
Edge: '79.0.0',
Firefox: '78.0.0',
iOS: '16.4.0',
Node: [
'node > 18.20.0 and node < 19.0.0',
'node > 20.12.0 and node < 21.0.0',
'node > 21.3.0',
],
Opera: '51.0.0',
Safari: '16.4.0',
chrome: '64.0.0',
edge: '79.0.0',
firefox: '78.0.0',
ios: '16.4.0',
node: '13.2.0',
opera: '51.0.0',
safari: '16.4.0',
},
es2019: {
Chrome: '66.0.0',
Edge: '79.0.0',
Firefox: '58.0.0',
iOS: '11.3.0',
Node: '10.0.0',
Opera: '53.0.0',
Safari: '11.1.0',
chrome: '66.0.0',
edge: '79.0.0',
firefox: '78.0.0',
ios: '16.4.0',
node: '13.2.0',
opera: '53.0.0',
safari: '16.4.0',
},
es2020: {
Chrome: '91.0.0',
Edge: '91.0.0',
Firefox: '80.0.0',
iOS: '14.5.0',
Node: '16.1.0',
Opera: '77.0.0',
Safari: '14.1.0',
chrome: '91.0.0',
edge: '91.0.0',
firefox: '80.0.0',
ios: '16.4.0',
node: '16.1.0',
opera: '77.0.0',
safari: '16.4.0',
},
es2021: {
Chrome: '85.0.0',
Edge: '85.0.0',
Firefox: '79.0.0',
iOS: '14.0.0',
Node: '15.0.0',
Opera: '71.0.0',
Safari: '14.0.0',
chrome: '91.0.0',
edge: '91.0.0',
firefox: '80.0.0',
ios: '16.4.0',
node: '16.1.0',
opera: '77.0.0',
safari: '16.4.0',
},
es2022: {
Chrome: '91.0.0',
Edge: '94.0.0',
Firefox: '93.0.0',
iOS: '16.4.0',
Node: '16.11.0',
Opera: '80.0.0',
Safari: '16.4.0',
chrome: '91.0.0',
firefox: '93.0.0',
ios: '16.4.0',
node: '16.11.0',
safari: '16.4.0',
},
es2023: {
Chrome: '74.0.0',
Edge: '79.0.0',
Firefox: '67.0.0',
iOS: '13.4.0',
Node: '12.5.0',
Opera: '62.0.0',
Safari: '13.1.0',
chrome: '91.0.0',
firefox: '93.0.0',
ios: '16.4.0',
node: '16.11.0',
safari: '16.4.0',
},
es2024: calcEsnextBrowserslistByTarget,
esnext: calcEsnextBrowserslistByTarget,
es5: {
Chrome: '5.0.0',
Edge: '12.0.0',
Firefox: '2.0.0',
ie: '9.0.0',
iOS: '6.0.0',
Node: '0.4.0',
Opera: '10.10.0',
Safari: '3.1.0',
},
} as const;

export function transformSyntaxToRspackTarget(
Expand Down
14 changes: 7 additions & 7 deletions packages/core/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,13 @@ describe('syntax', () => {
composedRsbuildConfig[0].config.output?.overrideBrowserslist,
).toMatchInlineSnapshot(`
[
"Chrome >= 63.0.0",
"Edge >= 79.0.0",
"Firefox >= 67.0.0",
"iOS >= 13.0.0",
"Node >= 10.0.0",
"Opera >= 50.0.0",
"Safari >= 13.0.0",
"chrome >= 63.0.0",
"edge >= 79.0.0",
"firefox >= 67.0.0",
"ios >= 13.0.0",
"node >= 13.2.0",
"opera >= 50.0.0",
"safari >= 13.0.0",
]
`);
});
Expand Down
100 changes: 75 additions & 25 deletions packages/core/tests/syntax.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,85 @@
import { describe, expect, test } from 'vitest';
import {
ESX_TO_BROWSERSLIST,
transformSyntaxToBrowserslist,
transformSyntaxToRspackTarget,
} from '../src/utils/syntax';

const compareSemver = (a: string, b: string) => {
const [aMajor, aMinor, aPatch] = a.split('.').map(Number);
const [bMajor, bMinor, bPatch] = b.split('.').map(Number);

if (aMajor !== bMajor) {
return aMajor - bMajor;
}
if (aMinor !== bMinor) {
return aMinor - bMinor;
}
return aPatch - bPatch;
};

describe('ESX_TO_BROWSERSLIST', () => {
test('some ECMA version queries should be the same', () => {
expect(ESX_TO_BROWSERSLIST.es6).toStrictEqual(ESX_TO_BROWSERSLIST.es2015);
expect(ESX_TO_BROWSERSLIST.esnext).toStrictEqual(
ESX_TO_BROWSERSLIST.es2024,
);
});

test('ECMA version mapped browserslist queries should increments', () => {
const sortedVersions = [
'es5',
'es6',
'es2015',
'es2016',
'es2017',
'es2018',
'es2019',
'es2020',
'es2021',
'es2022',
'es2023',
'es2024',
'esnext',
];

for (let i = 1; i < sortedVersions.length; i++) {
const prev = sortedVersions[i - 1];
const current = sortedVersions[i];
for (const query of Object.keys(ESX_TO_BROWSERSLIST[current])) {
const prevQuery = ESX_TO_BROWSERSLIST[prev][query];
const currQuery = ESX_TO_BROWSERSLIST[current][query];
if (prevQuery && currQuery) {
expect(compareSemver(currQuery, prevQuery)).toBeGreaterThanOrEqual(0);
}
}
}
});
});

describe('transformSyntaxToBrowserslist', () => {
test('esX', () => {
expect(transformSyntaxToBrowserslist('es6')).toMatchInlineSnapshot(`
expect(transformSyntaxToBrowserslist('es2015')).toMatchInlineSnapshot(`
[
"Chrome >= 63.0.0",
"Edge >= 79.0.0",
"Firefox >= 67.0.0",
"iOS >= 13.0.0",
"node > 12.20.0 and node < 13.0.0",
"node > 13.2.0",
"Opera >= 50.0.0",
"Safari >= 13.0.0",
"chrome >= 63.0.0",
"edge >= 79.0.0",
"firefox >= 67.0.0",
"ios >= 13.0.0",
"node >= 13.2.0",
"opera >= 50.0.0",
"safari >= 13.0.0",
]
`);

expect(transformSyntaxToBrowserslist('es2018')).toMatchInlineSnapshot(`
[
"Chrome >= 64.0.0",
"Edge >= 79.0.0",
"Firefox >= 78.0.0",
"iOS >= 16.4.0",
"node > 18.20.0 and node < 19.0.0",
"node > 20.12.0 and node < 21.0.0",
"node > 21.3.0",
"Opera >= 51.0.0",
"Safari >= 16.4.0",
"chrome >= 64.0.0",
"edge >= 79.0.0",
"firefox >= 78.0.0",
"ios >= 16.4.0",
"node >= 13.2.0",
"opera >= 51.0.0",
"safari >= 16.4.0",
]
`);

Expand Down Expand Up @@ -84,14 +134,14 @@ describe('transformSyntaxToBrowserslist', () => {
).toMatchInlineSnapshot(`
[
"Chrome 123",
"Chrome >= 5.0.0",
"Edge >= 12.0.0",
"Firefox >= 2.0.0",
"chrome >= 5.0.0",
"edge >= 12.0.0",
"firefox >= 2.0.0",
"ie >= 9.0.0",
"iOS >= 6.0.0",
"Node >= 0.4.0",
"Opera >= 10.10.0",
"Safari >= 3.1.0",
"ios >= 6.0.0",
"node >= 0.4.0",
"opera >= 10.10.0",
"safari >= 3.1.0",
]
`);

Expand Down
9 changes: 0 additions & 9 deletions tests/integration/syntax/__fixtures__/src/foo.ts

This file was deleted.

12 changes: 11 additions & 1 deletion tests/integration/syntax/__fixtures__/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
export { Foo } from './foo';
export class Foo {
constructor() {
this.#bar();
}

#bar() {}
}

export function foo(options: unknown = {}): void {
console.log(options);
}
16 changes: 13 additions & 3 deletions tests/integration/syntax/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ exports[`should downgrade class private method by default 1`] = `
}
#bar() {}
}
export { Foo };
function foo(options = {}) {
console.log(options);
}
export { Foo, foo };
"
`;

Expand All @@ -31,7 +34,11 @@ class Foo {
}
}
function bar() {}
export { Foo };
function foo() {
let options = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
console.log(options);
}
export { Foo, foo };
"
`;

Expand All @@ -42,6 +49,9 @@ exports[`should downgrade class private method with output.syntax config 2`] = `
}
#bar() {}
}
export { Foo };
function foo(options = {}) {
console.log(options);
}
export { Foo, foo };
"
`;
Loading