Skip to content

Commit

Permalink
feat: disableReactRefreshByDefault future flag (#4196)
Browse files Browse the repository at this point in the history
* feat: disbaleReactRefreshByDefault future flag

* update example

* update snap
  • Loading branch information
ahabhgk authored Sep 18, 2023
1 parent 7e0ffe6 commit f4fb014
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 8 deletions.
12 changes: 6 additions & 6 deletions examples/nestjs/rspack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ const config = {
minimize: false
},
externalsType: "commonjs",
builtins: {
react: {
refresh: false,
}
},
plugins: [
!process.env.BUILD &&
new RunScriptWebpackPlugin({
Expand Down Expand Up @@ -64,6 +59,11 @@ const config = {
}
callback();
}
]
],
experiments: {
rspackFuture: {
disableReactRefreshByDefault: true
}
}
};
module.exports = config;
10 changes: 9 additions & 1 deletion packages/rspack-dev-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
"<prefix>/./placeholder.js",
],
"undefined": [
"<prefix>/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",
"<prefix>/webpack/hot/dev-server.js",
],
}
`;

exports[`normalize options snapshot shouldn't have reactRefreshEntry.js by default when rspackFuture.disableReactRefreshByDefault is enabled 2`] = `
{
"main": [
"<prefix>/./placeholder.js",
],
"undefined": [
"<prefix>/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",
"<prefix>/webpack/hot/dev-server.js",
],
}
`;

exports[`normalize options snapshot shouldn't have reactRefreshEntry.js by default when rspackFuture.disableReactRefreshByDefault is enabled 3`] = `
{
"main": [
"<prefix>/./placeholder.js",
],
"undefined": [
"<prefix>/rspack-plugin-react-refresh/client/reactRefreshEntry.js",
"<prefix>/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",
"<prefix>/webpack/hot/dev-server.js",
],
}
`;

exports[`normalize options snapshot shouldn't have reactRefreshEntry.js when react.refresh is false 1`] = `
{
"main": [
Expand Down
46 changes: 46 additions & 0 deletions packages/rspack-dev-server/tests/normalizeOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down
3 changes: 2 additions & 1 deletion packages/rspack/src/config/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof rspackFutureOptions>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f4fb014

Please sign in to comment.