diff --git a/src/index.ts b/src/index.ts index 8c57cfb..5927b99 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,20 +1,19 @@ +import type { Compiler } from '@rspack/core'; import { normalizeOptions } from './options'; +import type { NormalizedPluginOptions, PluginOptions } from './options'; import { + getRefreshRuntimeDirPath, + getRefreshRuntimePaths, reactRefreshPath, - refreshRuntimeDirPath, refreshUtilsPath, - runtimePaths, } from './paths'; import { getAdditionalEntries } from './utils/getAdditionalEntries'; +import { getIntegrationEntry } from './utils/getIntegrationEntry'; import { type IntegrationType, getSocketIntegration, } from './utils/getSocketIntegration'; -import type { Compiler } from '@rspack/core'; -import type { NormalizedPluginOptions, PluginOptions } from './options'; -import { getIntegrationEntry } from './utils/getIntegrationEntry'; - export type { PluginOptions }; function addEntry(entry: string, compiler: Compiler) { @@ -36,7 +35,13 @@ const PLUGIN_NAME = 'ReactRefreshRspackPlugin'; class ReactRefreshRspackPlugin { options: NormalizedPluginOptions; - static deprecated_runtimePaths = runtimePaths; + /** + * @deprecated + */ + static get deprecated_runtimePaths() { + return getRefreshRuntimePaths(); + } + static loader = 'builtin:react-refresh-loader'; constructor(options: PluginOptions = {}) { @@ -89,7 +94,9 @@ class ReactRefreshRspackPlugin { include: this.options.include!, exclude: { // biome-ignore lint: exists - or: [this.options.exclude!, [...runtimePaths]].filter(Boolean), + or: [this.options.exclude!, [...getRefreshRuntimePaths()]].filter( + Boolean, + ), }, resourceQuery: this.options.resourceQuery, dependency: { @@ -136,7 +143,7 @@ class ReactRefreshRspackPlugin { new compiler.webpack.ProvidePlugin(providedModules).apply(compiler); compiler.options.resolve.alias = { - 'react-refresh': refreshRuntimeDirPath, + 'react-refresh': getRefreshRuntimeDirPath(), ...compiler.options.resolve.alias, }; diff --git a/src/paths.ts b/src/paths.ts index 3c9ebb8..60ca9a8 100644 --- a/src/paths.ts +++ b/src/paths.ts @@ -12,14 +12,23 @@ export const refreshUtilsPath = path.join( __dirname, '../client/refreshUtils.js', ); -export const refreshRuntimeDirPath = path.dirname( - require.resolve('react-refresh', { - paths: [reactRefreshPath], - }), -); -export const runtimePaths = [ + +let refreshRuntimeDirPath: string; + +export function getRefreshRuntimeDirPath() { + if (!refreshRuntimeDirPath) { + refreshRuntimeDirPath = path.dirname( + require.resolve('react-refresh', { + paths: [reactRefreshPath], + }), + ); + } + return refreshRuntimeDirPath; +} + +export const getRefreshRuntimePaths = () => [ reactRefreshEntryPath, reactRefreshPath, refreshUtilsPath, - refreshRuntimeDirPath, + getRefreshRuntimeDirPath(), ];