diff --git a/examples/nestjs/rspack.config.js b/examples/nestjs/rspack.config.js index e428bda8943..c20cc070310 100644 --- a/examples/nestjs/rspack.config.js +++ b/examples/nestjs/rspack.config.js @@ -11,11 +11,6 @@ const config = { minimize: false }, externalsType: "commonjs", - builtins: { - react: { - refresh: false, - } - }, plugins: [ !process.env.BUILD && new RunScriptWebpackPlugin({ @@ -64,6 +59,11 @@ const config = { } callback(); } - ] + ], + experiments: { + rspackFuture: { + disableReactRefreshByDefault: true + } + } }; module.exports = config; diff --git a/packages/rspack-dev-server/src/server.ts b/packages/rspack-dev-server/src/server.ts index 3177cf06beb..38b98401524 100644 --- a/packages/rspack-dev-server/src/server.ts +++ b/packages/rspack-dev-server/src/server.ts @@ -154,11 +154,19 @@ export class RspackDevServer extends WebpackDevServer { "Make sure to disable HMR for production by setting `devServer.hot` to `false` in the configuration." ); } + // enable hot by default compiler.options.devServer ??= {}; compiler.options.devServer.hot = true; + // enable react.development by default compiler.options.builtins.react ??= {}; - compiler.options.builtins.react.refresh ??= true; compiler.options.builtins.react.development ??= true; + // enable react.refresh is rspackFuture.disableReactRefreshByDefault is enabled + if ( + !compiler.options.experiments.rspackFuture + .disableReactRefreshByDefault + ) { + compiler.options.builtins.react.refresh ??= true; + } if (compiler.options.builtins.react.refresh) { new ReactRefreshPlugin().apply(compiler); } diff --git a/packages/rspack-dev-server/tests/__snapshots__/normalizeOptions.test.ts.snap b/packages/rspack-dev-server/tests/__snapshots__/normalizeOptions.test.ts.snap index b1998299d23..323e73bf105 100644 --- a/packages/rspack-dev-server/tests/__snapshots__/normalizeOptions.test.ts.snap +++ b/packages/rspack-dev-server/tests/__snapshots__/normalizeOptions.test.ts.snap @@ -140,6 +140,43 @@ exports[`normalize options snapshot react-refresh client added when react/refres } `; +exports[`normalize options snapshot shouldn't have reactRefreshEntry.js by default when rspackFuture.disableReactRefreshByDefault is enabled 1`] = ` +{ + "main": [ + "/./placeholder.js", + ], + "undefined": [ + "/webpack-dev-server/client/index.js?protocol=ws%3A&hostname=0.0.0.0&&pathname=%2Fws&logging=info&overlay=true&reconnect=10&hot=true&live-reload=true", + "/webpack/hot/dev-server.js", + ], +} +`; + +exports[`normalize options snapshot shouldn't have reactRefreshEntry.js by default when rspackFuture.disableReactRefreshByDefault is enabled 2`] = ` +{ + "main": [ + "/./placeholder.js", + ], + "undefined": [ + "/webpack-dev-server/client/index.js?protocol=ws%3A&hostname=0.0.0.0&&pathname=%2Fws&logging=info&overlay=true&reconnect=10&hot=true&live-reload=true", + "/webpack/hot/dev-server.js", + ], +} +`; + +exports[`normalize options snapshot shouldn't have reactRefreshEntry.js by default when rspackFuture.disableReactRefreshByDefault is enabled 3`] = ` +{ + "main": [ + "/./placeholder.js", + ], + "undefined": [ + "/rspack-plugin-react-refresh/client/reactRefreshEntry.js", + "/webpack-dev-server/client/index.js?protocol=ws%3A&hostname=0.0.0.0&&pathname=%2Fws&logging=info&overlay=true&reconnect=10&hot=true&live-reload=true", + "/webpack/hot/dev-server.js", + ], +} +`; + exports[`normalize options snapshot shouldn't have reactRefreshEntry.js when react.refresh is false 1`] = ` { "main": [ diff --git a/packages/rspack-dev-server/tests/normalizeOptions.test.ts b/packages/rspack-dev-server/tests/normalizeOptions.test.ts index 0bfacc5e68b..a6ae462c7f4 100644 --- a/packages/rspack-dev-server/tests/normalizeOptions.test.ts +++ b/packages/rspack-dev-server/tests/normalizeOptions.test.ts @@ -60,6 +60,52 @@ describe("normalize options snapshot", () => { ); }); + it("shouldn't have reactRefreshEntry.js by default when rspackFuture.disableReactRefreshByDefault is enabled", async () => { + await matchAdditionEntries( + {}, + { + entry: ["something"], + experiments: { + rspackFuture: { + disableReactRefreshByDefault: true + } + } + } + ); + await matchAdditionEntries( + {}, + { + entry: ["something"], + builtins: { + react: { + refresh: false + } + }, + experiments: { + rspackFuture: { + disableReactRefreshByDefault: true + } + } + } + ); + await matchAdditionEntries( + {}, + { + entry: ["something"], + builtins: { + react: { + refresh: true + } + }, + experiments: { + rspackFuture: { + disableReactRefreshByDefault: true + } + } + } + ); + }); + it("react.development and react.refresh should be true by default when hot enabled", async () => { const compiler = createCompiler({ entry: ENTRY, diff --git a/packages/rspack/src/config/defaults.ts b/packages/rspack/src/config/defaults.ts index b41e23a3d36..700a91185c2 100644 --- a/packages/rspack/src/config/defaults.ts +++ b/packages/rspack/src/config/defaults.ts @@ -178,6 +178,7 @@ const applyExperimentsDefaults = ( D(experiments.rspackFuture, "newResolver", false); D(experiments.rspackFuture, "newTreeshaking", false); D(experiments.rspackFuture, "disableTransformByDefault", false); + D(experiments.rspackFuture, "disableReactRefreshByDefault", false); } }; diff --git a/packages/rspack/src/config/zod.ts b/packages/rspack/src/config/zod.ts index 5042dd71446..fc51c594feb 100644 --- a/packages/rspack/src/config/zod.ts +++ b/packages/rspack/src/config/zod.ts @@ -937,7 +937,8 @@ export type IncrementalRebuildOptions = z.infer< const rspackFutureOptions = z.strictObject({ newResolver: z.boolean().optional(), newTreeshaking: z.boolean().optional(), - disableTransformByDefault: z.boolean().optional() + disableTransformByDefault: z.boolean().optional(), + disableReactRefreshByDefault: z.boolean().optional() }); export type RspackFutureOptions = z.infer; diff --git a/packages/rspack/tests/__snapshots__/Defaults.unittest.ts.snap b/packages/rspack/tests/__snapshots__/Defaults.unittest.ts.snap index 455ec57f2fa..86baf929a31 100644 --- a/packages/rspack/tests/__snapshots__/Defaults.unittest.ts.snap +++ b/packages/rspack/tests/__snapshots__/Defaults.unittest.ts.snap @@ -25,6 +25,7 @@ exports[`snapshots should have the correct base config 1`] = ` "lazyCompilation": false, "newSplitChunks": true, "rspackFuture": { + "disableReactRefreshByDefault": false, "disableTransformByDefault": false, "newResolver": false, "newTreeshaking": false,