Skip to content

Commit dcd495a

Browse files
authored
feat: support JSX preserve (#1244)
1 parent 6e40c26 commit dcd495a

File tree

44 files changed

+1180
-285
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1180
-285
lines changed

biome.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"**",
2222
"!**/*.vue",
2323
"!**/.rslib/**/*",
24-
"!tests/e2e/react-component/public/umd/**/*"
24+
"!tests/e2e/react-component/public/umd/**/*",
25+
"!tests/integration/preserve-jsx/default/src/Component2.tsx"
2526
],
2627
"indentStyle": "space"
2728
},

examples/module-federation/mf-host/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"devDependencies": {
1515
"@module-federation/rsbuild-plugin": "^0.19.1",
1616
"@rsbuild/core": "~1.5.11",
17-
"@rsbuild/plugin-react": "^1.4.0",
17+
"@rsbuild/plugin-react": "^1.4.1",
1818
"@types/react": "^19.1.13",
1919
"@types/react-dom": "^19.1.9",
2020
"typescript": "^5.9.2"

examples/module-federation/mf-react-component/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"@module-federation/enhanced": "^0.19.1",
2222
"@module-federation/rsbuild-plugin": "^0.19.1",
2323
"@module-federation/storybook-addon": "^4.0.30",
24-
"@rsbuild/plugin-react": "^1.4.0",
24+
"@rsbuild/plugin-react": "^1.4.1",
2525
"@rslib/core": "workspace:*",
2626
"@types/react": "^19.1.13",
2727
"http-server": "^14.1.1",

examples/module-federation/mf-remote/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"devDependencies": {
1515
"@module-federation/rsbuild-plugin": "^0.19.1",
1616
"@rsbuild/core": "~1.5.11",
17-
"@rsbuild/plugin-react": "^1.4.0",
17+
"@rsbuild/plugin-react": "^1.4.1",
1818
"@types/react": "^19.1.13",
1919
"@types/react-dom": "^19.1.9",
2020
"typescript": "^5.9.2"

examples/react-component-bundle-false/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build": "rslib build"
99
},
1010
"devDependencies": {
11-
"@rsbuild/plugin-react": "^1.4.0",
11+
"@rsbuild/plugin-react": "^1.4.1",
1212
"@rsbuild/plugin-sass": "^1.4.0",
1313
"@rslib/core": "workspace:*",
1414
"@types/react": "^19.1.13",

examples/react-component-bundle/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build": "rslib build"
99
},
1010
"devDependencies": {
11-
"@rsbuild/plugin-react": "^1.4.0",
11+
"@rsbuild/plugin-react": "^1.4.1",
1212
"@rsbuild/plugin-sass": "^1.4.0",
1313
"@rslib/core": "workspace:*",
1414
"@types/react": "^19.1.13",

examples/react-component-umd/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"build": "rslib build"
88
},
99
"devDependencies": {
10-
"@rsbuild/plugin-react": "^1.4.0",
10+
"@rsbuild/plugin-react": "^1.4.1",
1111
"@rsbuild/plugin-sass": "^1.4.0",
1212
"@rslib/core": "workspace:*",
1313
"@types/react": "^19.1.13",

packages/core/src/config.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ export function composeMinifyConfig(config: LibConfig): EnvironmentConfig {
397397
js: true,
398398
css: false,
399399
jsOptions: {
400+
// Tweaked based on https://github.com/web-infra-dev/rspack/blob/e350b76163c976ee48da54c29bbb9d72153738d7/crates/rspack_plugin_swc_js_minimizer/src/lib.rs#L40.
401+
test: /\.[cm]?jsx?(\?.*)?$/,
400402
minimizerOptions: {
401403
mangle: false,
402404
// MF assets are loaded over the network, which means they will not be compressed by the project. Therefore, minifying them is necessary.
@@ -838,6 +840,45 @@ const fixJsModuleTypePlugin = (): RsbuildPlugin => ({
838840
},
839841
});
840842

843+
const BundlePlugin = (): RsbuildPlugin => ({
844+
name: 'rslib:bundle',
845+
setup(api) {
846+
api.onBeforeBuild({
847+
order: 'post',
848+
handler: ({ bundlerConfigs }) => {
849+
if (bundlerConfigs) {
850+
for (const config of bundlerConfigs) {
851+
if (config?.module?.parser?.javascript?.jsx === true) {
852+
logger.error(
853+
'Bundle mode does not support preserving JSX syntax. Set "bundle" to "false" or change the JSX runtime to `automatic` or `classic`. Check out ' +
854+
color.green(
855+
'https://rslib.rs/guide/solution/react#jsx-transform',
856+
) +
857+
' for more details.',
858+
);
859+
process.exit(1);
860+
}
861+
}
862+
}
863+
},
864+
});
865+
},
866+
});
867+
868+
const composeBundleConfig = (
869+
bundle: LibConfig['bundle'],
870+
): { rsbuildConfig: EnvironmentConfig } => {
871+
if (bundle) {
872+
return {
873+
rsbuildConfig: {
874+
plugins: [BundlePlugin()],
875+
},
876+
};
877+
}
878+
879+
return { rsbuildConfig: {} };
880+
};
881+
841882
const composeShimsConfig = (
842883
format: Format,
843884
shims?: Shims,
@@ -1676,6 +1717,7 @@ async function composeLibRsbuildConfig(
16761717
redirect = {},
16771718
umdName,
16781719
} = config;
1720+
const { rsbuildConfig: bundleConfig } = composeBundleConfig(bundle);
16791721
const { rsbuildConfig: shimsConfig, enabledShims } = composeShimsConfig(
16801722
format,
16811723
shims,
@@ -1761,6 +1803,7 @@ async function composeLibRsbuildConfig(
17611803
const printFileSizeConfig = composePrintFileSizeConfig(bundle, target);
17621804

17631805
return mergeRsbuildConfig(
1806+
bundleConfig,
17641807
formatConfig,
17651808
// outputConfig,
17661809
shimsConfig,

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
931931
toplevel: true
932932
}
933933
},
934-
extractComments: true
934+
extractComments: true,
935+
test: /\\.[cm]?jsx?(\\?.*)?$/
935936
}
936937
)
937938
],
@@ -1627,7 +1628,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
16271628
toplevel: true
16281629
}
16291630
},
1630-
extractComments: true
1631+
extractComments: true,
1632+
test: /\\.[cm]?jsx?(\\?.*)?$/
16311633
}
16321634
)
16331635
],
@@ -2230,7 +2232,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
22302232
toplevel: true
22312233
}
22322234
},
2233-
extractComments: true
2235+
extractComments: true,
2236+
test: /\\.[cm]?jsx?(\\?.*)?$/
22342237
}
22352238
)
22362239
],
@@ -2831,7 +2834,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
28312834
toplevel: true
28322835
}
28332836
},
2834-
extractComments: true
2837+
extractComments: true,
2838+
test: /\\.[cm]?jsx?(\\?.*)?$/
28352839
}
28362840
)
28372841
],
@@ -3401,7 +3405,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
34013405
toplevel: false
34023406
}
34033407
},
3404-
extractComments: true
3408+
extractComments: true,
3409+
test: /\\.[cm]?jsx?(\\?.*)?$/
34053410
}
34063411
)
34073412
],
@@ -3559,6 +3564,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
35593564
"mangle": false,
35603565
"minify": false,
35613566
},
3567+
"test": /\\\\\\.\\[cm\\]\\?jsx\\?\\(\\\\\\?\\.\\*\\)\\?\\$/,
35623568
},
35633569
},
35643570
"overrideBrowserslist": [
@@ -3572,6 +3578,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
35723578
},
35733579
},
35743580
"plugins": [
3581+
{
3582+
"name": "rslib:bundle",
3583+
"setup": [Function],
3584+
},
35753585
{
35763586
"name": "rsbuild:disable-url-parse",
35773587
"setup": [Function],
@@ -3835,6 +3845,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
38353845
"mangle": false,
38363846
"minify": false,
38373847
},
3848+
"test": /\\\\\\.\\[cm\\]\\?jsx\\?\\(\\\\\\?\\.\\*\\)\\?\\$/,
38383849
},
38393850
},
38403851
"overrideBrowserslist": [
@@ -3848,6 +3859,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
38483859
},
38493860
},
38503861
"plugins": [
3862+
{
3863+
"name": "rslib:bundle",
3864+
"setup": [Function],
3865+
},
38513866
{
38523867
"name": "rsbuild:cjs-import-meta-url-shim",
38533868
"setup": [Function],
@@ -4099,6 +4114,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
40994114
"mangle": false,
41004115
"minify": false,
41014116
},
4117+
"test": /\\\\\\.\\[cm\\]\\?jsx\\?\\(\\\\\\?\\.\\*\\)\\?\\$/,
41024118
},
41034119
},
41044120
"overrideBrowserslist": [
@@ -4112,6 +4128,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
41124128
},
41134129
},
41144130
"plugins": [
4131+
{
4132+
"name": "rslib:bundle",
4133+
"setup": [Function],
4134+
},
41154135
{
41164136
"name": "rsbuild:fix-js-module-type",
41174137
"setup": [Function],
@@ -4338,6 +4358,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
43384358
"minify": false,
43394359
"module": true,
43404360
},
4361+
"test": /\\\\\\.\\[cm\\]\\?jsx\\?\\(\\\\\\?\\.\\*\\)\\?\\$/,
43414362
},
43424363
},
43434364
"overrideBrowserslist": [
@@ -4351,6 +4372,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
43514372
},
43524373
},
43534374
"plugins": [
4375+
{
4376+
"name": "rslib:bundle",
4377+
"setup": [Function],
4378+
},
43544379
{
43554380
"name": "rsbuild:fix-js-module-type",
43564381
"setup": [Function],
@@ -4522,6 +4547,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
45224547
"mangle": false,
45234548
"minify": true,
45244549
},
4550+
"test": /\\\\\\.\\[cm\\]\\?jsx\\?\\(\\\\\\?\\.\\*\\)\\?\\$/,
45254551
},
45264552
},
45274553
"overrideBrowserslist": [
@@ -4540,6 +4566,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
45404566
},
45414567
},
45424568
"plugins": [
4569+
{
4570+
"name": "rslib:bundle",
4571+
"setup": [Function],
4572+
},
45434573
{
45444574
"name": "rsbuild:fix-js-module-type",
45454575
"setup": [Function],

packages/core/tests/config.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ describe('minify', () => {
354354
"mangle": false,
355355
"minify": false,
356356
},
357+
"test": /\\\\\\.\\[cm\\]\\?jsx\\?\\(\\\\\\?\\.\\*\\)\\?\\$/,
357358
},
358359
}
359360
`);

0 commit comments

Comments
 (0)