Skip to content

Commit d74404e

Browse files
authored
feat(plugin-react): complete preserve React runtime (#6240)
1 parent 28d4580 commit d74404e

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

packages/plugin-react/src/react.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ export const applyBasicReactSupport = (
4949
return mergeEnvironmentConfig(extraConfig, config);
5050
});
5151

52+
if (options.swcReactOptions?.runtime === 'preserve') {
53+
api.modifyBundlerChain((chain) => {
54+
chain.module.parser.merge({
55+
javascript: {
56+
jsx: true,
57+
},
58+
});
59+
});
60+
}
61+
5262
api.modifyBundlerChain(
5363
async (chain, { CHAIN_ID, environment, isDev, target }) => {
5464
const { config } = environment;

packages/plugin-react/tests/__snapshots__/index.test.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ exports[`plugins/react > should not apply splitChunks rule when strategy is not
9595
}
9696
`;
9797

98+
exports[`plugins/react > should set \`parser.javascript.jsx\` to \`true\` when using \`preserve\` react runtime 1`] = `
99+
{
100+
"exportsPresence": "error",
101+
"inlineConst": false,
102+
"jsx": true,
103+
"typeReexportsPresence": "tolerant",
104+
}
105+
`;
106+
98107
exports[`plugins/react > should set transpilation scope for react refresh plugin correctly 1`] = `
99108
ReactRefreshRspackPlugin {
100109
"options": {

packages/plugin-react/tests/index.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ describe('plugins/react', () => {
3939
expect(matchRules(config, 'a.js')).toMatchSnapshot();
4040
});
4141

42+
it('should set `parser.javascript.jsx` to `true` when using `preserve` react runtime', async () => {
43+
const rsbuild = await createStubRsbuild({
44+
rsbuildConfig: {},
45+
});
46+
47+
rsbuild.addPlugins([
48+
pluginReact({
49+
swcReactOptions: {
50+
runtime: 'preserve',
51+
},
52+
}),
53+
]);
54+
const config = await rsbuild.unwrapConfig();
55+
expect(config.module.parser.javascript).toMatchSnapshot();
56+
});
57+
4258
it('should not apply react refresh when dev.hmr is false', async () => {
4359
const rsbuild = await createStubRsbuild({
4460
rsbuildConfig: {

0 commit comments

Comments
 (0)