From 95d63b4c483090a4af16b493afad5753b38e04f7 Mon Sep 17 00:00:00 2001 From: Gengkun Date: Mon, 25 Sep 2023 16:33:43 +0800 Subject: [PATCH] fix: avoid double react refresh transform in rspack-dev-server (#4243) fix: double react refresh transform --- packages/rspack-dev-server/src/server.ts | 16 +++++++++++++++- .../rspack-plugin-react-refresh/src/index.js | 4 +++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/rspack-dev-server/src/server.ts b/packages/rspack-dev-server/src/server.ts index 53d3f584997..3506b9534d6 100644 --- a/packages/rspack-dev-server/src/server.ts +++ b/packages/rspack-dev-server/src/server.ts @@ -166,7 +166,21 @@ export class RspackDevServer extends WebpackDevServer { // enable react.refresh by default compiler.options.builtins.react.refresh ??= true; if (compiler.options.builtins.react.refresh) { - new ReactRefreshPlugin().apply(compiler); + const runtimePaths = ReactRefreshPlugin.deprecated_runtimePaths; + new compiler.webpack.EntryPlugin( + compiler.context, + runtimePaths[0], + { + name: undefined + } + ).apply(compiler); + new compiler.webpack.ProvidePlugin({ + $ReactRefreshRuntime$: runtimePaths[1] + }).apply(compiler); + compiler.options.module.rules.unshift({ + include: runtimePaths, + type: "js" + }); } } } else if (compiler.options.builtins.react.refresh) { diff --git a/packages/rspack-plugin-react-refresh/src/index.js b/packages/rspack-plugin-react-refresh/src/index.js index 75a4b19a7a9..567436fee99 100644 --- a/packages/rspack-plugin-react-refresh/src/index.js +++ b/packages/rspack-plugin-react-refresh/src/index.js @@ -18,9 +18,9 @@ const refreshRuntimeDirPath = path.dirname( }) ); const runtimePaths = [ + reactRefreshEntryPath, reactRefreshPath, refreshUtilsPath, - reactRefreshEntryPath, refreshRuntimeDirPath ]; @@ -61,3 +61,5 @@ module.exports = class ReactRefreshRspackPlugin { }); } }; + +module.exports.deprecated_runtimePaths = runtimePaths;