From fd59dfd69b4af793b1593d258b002d85bfc279f0 Mon Sep 17 00:00:00 2001 From: hardfist Date: Mon, 8 Jan 2024 10:02:24 +0800 Subject: [PATCH 1/3] fix: not enable rspack-plugin-react-refresh when mode set to 'production' --- packages/rspack-plugin-react-refresh/src/index.ts | 11 +++++++++++ packages/rspack-plugin-react-refresh/src/options.ts | 2 ++ 2 files changed, 13 insertions(+) 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; } From df84b6e9b5ddf4e8a012317fb1830b2a20bdf81b Mon Sep 17 00:00:00 2001 From: hardfist Date: Mon, 8 Jan 2024 10:41:14 +0800 Subject: [PATCH 2/3] chore: update test case --- .../rspack-dev-server/tests/normalizeOptions.test.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/rspack-dev-server/tests/normalizeOptions.test.ts b/packages/rspack-dev-server/tests/normalizeOptions.test.ts index b8aba1c9fcb..5725d039ea4 100644 --- a/packages/rspack-dev-server/tests/normalizeOptions.test.ts +++ b/packages/rspack-dev-server/tests/normalizeOptions.test.ts @@ -30,7 +30,7 @@ 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( @@ -44,10 +44,18 @@ describe("normalize options snapshot", () => { {}, { entry: ["something"], - plugins: [new ReactRefreshPlugin()] + plugins: [new ReactRefreshPlugin({ forceEnable: true })] } ); expect(entries2["undefined"]).toContain(reactRefreshEntry); + const entries3 = await getAdditionEntries( + {}, + { + entry: ["something"], + plugins: [new ReactRefreshPlugin()] + } + ); + expect(entries2["undefined"]).not.toContain(reactRefreshEntry); }); it("should apply HMR plugin by default", async () => { From efe0a4db9ab4e4ba347a2c44084b84b8fac0f334 Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Mon, 8 Jan 2024 15:15:07 +0800 Subject: [PATCH 3/3] fix --- .../tests/normalizeOptions.test.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/rspack-dev-server/tests/normalizeOptions.test.ts b/packages/rspack-dev-server/tests/normalizeOptions.test.ts index 5725d039ea4..c179e36e1f6 100644 --- a/packages/rspack-dev-server/tests/normalizeOptions.test.ts +++ b/packages/rspack-dev-server/tests/normalizeOptions.test.ts @@ -36,6 +36,7 @@ describe("normalize options snapshot", () => { const entries1 = await getAdditionEntries( {}, { + mode: "production", entry: ["something"] } ); @@ -43,6 +44,7 @@ describe("normalize options snapshot", () => { const entries2 = await getAdditionEntries( {}, { + mode: "production", entry: ["something"], plugins: [new ReactRefreshPlugin({ forceEnable: true })] } @@ -51,11 +53,21 @@ describe("normalize options snapshot", () => { const entries3 = await getAdditionEntries( {}, { + mode: "development", entry: ["something"], plugins: [new ReactRefreshPlugin()] } ); - expect(entries2["undefined"]).not.toContain(reactRefreshEntry); + 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 () => {