Skip to content

Commit e19f99b

Browse files
authored
fix: syntax could handle combined pattern (#163)
1 parent 1bcd189 commit e19f99b

File tree

6 files changed

+74
-15
lines changed

6 files changed

+74
-15
lines changed

e2e/cases/syntax/__snapshots__/index.test.ts.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,14 @@ function bar() {} /*#__PURE__*/
3434
export { Foo };
3535
"
3636
`;
37+
38+
exports[`should downgrade class private method with output.syntax config 2`] = `
39+
"class Foo {
40+
constructor(){
41+
this.#bar();
42+
}
43+
#bar() {}
44+
}
45+
export { Foo };
46+
"
47+
`;

e2e/cases/syntax/config/rslib.config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,28 @@ export default defineConfig({
55
lib: [
66
generateBundleEsmConfig({
77
output: {
8+
distPath: { root: 'dist/esm/0' },
89
syntax: 'es2015',
910
},
1011
}),
12+
generateBundleEsmConfig({
13+
output: {
14+
distPath: { root: 'dist/esm/1' },
15+
syntax: ['es2022'],
16+
},
17+
}),
1118
generateBundleCjsConfig({
1219
output: {
20+
distPath: { root: 'dist/cjs/0' },
1321
syntax: ['node 20'],
1422
},
1523
}),
24+
generateBundleCjsConfig({
25+
output: {
26+
distPath: { root: 'dist/cjs/1' },
27+
syntax: ['node 20', 'es5'],
28+
},
29+
}),
1630
],
1731
source: {
1832
entry: {

e2e/cases/syntax/index.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ test('should downgrade class private method with output.syntax config', async ()
1616
const fixturePath = join(__dirname, 'config');
1717
const { entries } = await buildAndGetResults(fixturePath);
1818

19-
expect(entries.esm).toMatchSnapshot();
20-
expect(entries.esm).not.toContain('#bar');
19+
expect(entries.esm0).toMatchSnapshot();
20+
expect(entries.esm0).not.toContain('#bar');
2121

22-
expect(entries.cjs).toContain('#bar');
22+
expect(entries.esm1).toMatchSnapshot();
23+
expect(entries.esm1).toContain('#bar');
24+
25+
expect(entries.cjs0).toContain('#bar');
26+
expect(entries.cjs1).not.toContain('#bar');
2327
});

packages/core/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ const composeSyntaxConfig = (
439439
target?: string,
440440
): RsbuildConfig => {
441441
// Defaults to ESNext, Rslib will assume all of the latest JavaScript and CSS features are supported.
442+
442443
if (syntax) {
443444
return {
444445
tools: {

packages/core/src/utils/syntax.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,27 +125,34 @@ export const transformSyntaxToBrowserslist = (
125125
): NonNullable<
126126
NonNullable<RsbuildConfig['output']>['overrideBrowserslist']
127127
> => {
128-
// only single esX is allowed
129-
if (typeof syntax === 'string' && syntax.toLowerCase().startsWith('es')) {
130-
if (syntax.toLowerCase() in ESX_TO_BROWSERSLIST) {
131-
return Object.entries(ESX_TO_BROWSERSLIST[syntax]).flatMap(
132-
([engine, version]) => {
128+
const handleSyntaxItem = (
129+
syntaxItem: EcmaScriptVersion | string,
130+
): string[] => {
131+
if (
132+
typeof syntaxItem === 'string' &&
133+
syntaxItem.toLowerCase().startsWith('es')
134+
) {
135+
if (syntaxItem.toLowerCase() in ESX_TO_BROWSERSLIST) {
136+
return Object.entries(
137+
ESX_TO_BROWSERSLIST[syntaxItem as EcmaScriptVersion],
138+
).flatMap(([engine, version]) => {
133139
if (Array.isArray(version)) {
134140
return version;
135141
}
136142

137143
return `${engine} >= ${version}`;
138-
},
139-
);
144+
});
145+
}
146+
147+
throw new Error(`Unsupported ES version: ${syntaxItem}`);
140148
}
141149

142-
throw new Error(`Unsupported ES version: ${syntax}`);
143-
}
150+
return [syntaxItem];
151+
};
144152

145-
// inline browserslist query
146153
if (Array.isArray(syntax)) {
147-
return syntax;
154+
return syntax.flatMap(handleSyntaxItem);
148155
}
149156

150-
throw new Error(`Unsupported syntax: ${syntax}`);
157+
return handleSyntaxItem(syntax);
151158
};

packages/core/tests/syntax.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,26 @@ describe('Correctly resolve syntax', () => {
4949
]
5050
`);
5151
});
52+
53+
test('combined', async () => {
54+
expect(
55+
transformSyntaxToBrowserslist(['Chrome 123', 'es5']),
56+
).toMatchInlineSnapshot(`
57+
[
58+
"Chrome 123",
59+
"Chrome >= 5.0.0",
60+
"Edge >= 12.0.0",
61+
"Firefox >= 2.0.0",
62+
"ie >= 9.0.0",
63+
"iOS >= 6.0.0",
64+
"Node >= 0.4.0",
65+
"Opera >= 10.10.0",
66+
"Safari >= 3.1.0",
67+
]
68+
`);
69+
70+
expect(transformSyntaxToBrowserslist(['es5'])).toEqual(
71+
transformSyntaxToBrowserslist('es5'),
72+
);
73+
});
5274
});

0 commit comments

Comments
 (0)