Skip to content

Commit 0e7b0ee

Browse files
authored
perf: convert runtime paths to lazy-loaded functions (#58)
1 parent 8c44c21 commit 0e7b0ee

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

src/index.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1+
import type { Compiler } from '@rspack/core';
12
import { normalizeOptions } from './options';
3+
import type { NormalizedPluginOptions, PluginOptions } from './options';
24
import {
5+
getRefreshRuntimeDirPath,
6+
getRefreshRuntimePaths,
37
reactRefreshPath,
4-
refreshRuntimeDirPath,
58
refreshUtilsPath,
6-
runtimePaths,
79
} from './paths';
810
import { getAdditionalEntries } from './utils/getAdditionalEntries';
11+
import { getIntegrationEntry } from './utils/getIntegrationEntry';
912
import {
1013
type IntegrationType,
1114
getSocketIntegration,
1215
} from './utils/getSocketIntegration';
1316

14-
import type { Compiler } from '@rspack/core';
15-
import type { NormalizedPluginOptions, PluginOptions } from './options';
16-
import { getIntegrationEntry } from './utils/getIntegrationEntry';
17-
1817
export type { PluginOptions };
1918

2019
function addEntry(entry: string, compiler: Compiler) {
@@ -36,7 +35,13 @@ const PLUGIN_NAME = 'ReactRefreshRspackPlugin';
3635
class ReactRefreshRspackPlugin {
3736
options: NormalizedPluginOptions;
3837

39-
static deprecated_runtimePaths = runtimePaths;
38+
/**
39+
* @deprecated
40+
*/
41+
static get deprecated_runtimePaths() {
42+
return getRefreshRuntimePaths();
43+
}
44+
4045
static loader = 'builtin:react-refresh-loader';
4146

4247
constructor(options: PluginOptions = {}) {
@@ -89,7 +94,9 @@ class ReactRefreshRspackPlugin {
8994
include: this.options.include!,
9095
exclude: {
9196
// biome-ignore lint: exists
92-
or: [this.options.exclude!, [...runtimePaths]].filter(Boolean),
97+
or: [this.options.exclude!, [...getRefreshRuntimePaths()]].filter(
98+
Boolean,
99+
),
93100
},
94101
resourceQuery: this.options.resourceQuery,
95102
dependency: {
@@ -136,7 +143,7 @@ class ReactRefreshRspackPlugin {
136143
new compiler.webpack.ProvidePlugin(providedModules).apply(compiler);
137144

138145
compiler.options.resolve.alias = {
139-
'react-refresh': refreshRuntimeDirPath,
146+
'react-refresh': getRefreshRuntimeDirPath(),
140147
...compiler.options.resolve.alias,
141148
};
142149

src/paths.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,23 @@ export const refreshUtilsPath = path.join(
1212
__dirname,
1313
'../client/refreshUtils.js',
1414
);
15-
export const refreshRuntimeDirPath = path.dirname(
16-
require.resolve('react-refresh', {
17-
paths: [reactRefreshPath],
18-
}),
19-
);
20-
export const runtimePaths = [
15+
16+
let refreshRuntimeDirPath: string;
17+
18+
export function getRefreshRuntimeDirPath() {
19+
if (!refreshRuntimeDirPath) {
20+
refreshRuntimeDirPath = path.dirname(
21+
require.resolve('react-refresh', {
22+
paths: [reactRefreshPath],
23+
}),
24+
);
25+
}
26+
return refreshRuntimeDirPath;
27+
}
28+
29+
export const getRefreshRuntimePaths = () => [
2130
reactRefreshEntryPath,
2231
reactRefreshPath,
2332
refreshUtilsPath,
24-
refreshRuntimeDirPath,
33+
getRefreshRuntimeDirPath(),
2534
];

0 commit comments

Comments
 (0)