diff --git a/packages/rspack-dev-server/tests/normalizeOptions.test.ts b/packages/rspack-dev-server/tests/normalizeOptions.test.ts index b8aba1c9fcb..c179e36e1f6 100644 --- a/packages/rspack-dev-server/tests/normalizeOptions.test.ts +++ b/packages/rspack-dev-server/tests/normalizeOptions.test.ts @@ -30,12 +30,13 @@ describe("normalize options snapshot", () => { ).toMatchSnapshot(); }); - it("shouldn't have reactRefreshEntry.js by default when rspackFuture.disableReactRefreshByDefault is enabled", async () => { + it("shouldn't have reactRefreshEntry.js by default when in production mode", async () => { const reactRefreshEntry = "/rspack-plugin-react-refresh/client/reactRefreshEntry.js"; const entries1 = await getAdditionEntries( {}, { + mode: "production", entry: ["something"] } ); @@ -43,11 +44,30 @@ describe("normalize options snapshot", () => { const entries2 = await getAdditionEntries( {}, { + mode: "production", entry: ["something"], - plugins: [new ReactRefreshPlugin()] + plugins: [new ReactRefreshPlugin({ forceEnable: true })] } ); expect(entries2["undefined"]).toContain(reactRefreshEntry); + const entries3 = await getAdditionEntries( + {}, + { + mode: "development", + entry: ["something"], + plugins: [new ReactRefreshPlugin()] + } + ); + expect(entries3["undefined"]).toContain(reactRefreshEntry); + const entries4 = await getAdditionEntries( + {}, + { + mode: "production", + entry: ["something"], + plugins: [new ReactRefreshPlugin()] + } + ); + expect(entries4["undefined"]).not.toContain(reactRefreshEntry); }); it("should apply HMR plugin by default", async () => { diff --git a/packages/rspack-plugin-react-refresh/src/index.ts b/packages/rspack-plugin-react-refresh/src/index.ts index 46481c3da01..566e4ecbf26 100644 --- a/packages/rspack-plugin-react-refresh/src/index.ts +++ b/packages/rspack-plugin-react-refresh/src/index.ts @@ -34,6 +34,17 @@ class ReactRefreshRspackPlugin { } apply(compiler: Compiler) { + if ( + // Webpack do not set process.env.NODE_ENV, so we need to check for mode. + // Ref: https://github.com/webpack/webpack/issues/7074 + (compiler.options.mode !== "development" || + // We also check for production process.env.NODE_ENV, + // in case it was set and mode is non-development (e.g. 'none') + (process.env.NODE_ENV && process.env.NODE_ENV === "production")) && + !this.options.forceEnable + ) { + return; + } new compiler.webpack.EntryPlugin(compiler.context, reactRefreshEntryPath, { name: undefined }).apply(compiler); diff --git a/packages/rspack-plugin-react-refresh/src/options.ts b/packages/rspack-plugin-react-refresh/src/options.ts index 3031ea67577..292966d2dd5 100644 --- a/packages/rspack-plugin-react-refresh/src/options.ts +++ b/packages/rspack-plugin-react-refresh/src/options.ts @@ -2,6 +2,7 @@ export type PluginOptions = { include?: string | RegExp | (string | RegExp)[] | null; exclude?: string | RegExp | (string | RegExp)[] | null; library?: string; + forceEnable?: boolean; }; const d = ( @@ -22,5 +23,6 @@ export function normalizeOptions(options: PluginOptions) { d(options, "exclude", /node_modules/i); d(options, "include", /\.([cm]js|[jt]sx?|flow)$/i); d(options, "library"); + d(options, "forceEnable", false); return options; }