Skip to content

Commit c481851

Browse files
authored
perf: replace require.resolve with path.join (#56)
1 parent fa44154 commit c481851

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import path from 'node:path';
21
import { normalizeOptions } from './options';
3-
import { reactRefreshPath, refreshUtilsPath, runtimePaths } from './paths';
2+
import {
3+
reactRefreshPath,
4+
refreshRuntimeDirPath,
5+
refreshUtilsPath,
6+
runtimePaths,
7+
} from './paths';
48
import { getAdditionalEntries } from './utils/getAdditionalEntries';
59
import {
610
type IntegrationType,
@@ -131,9 +135,8 @@ class ReactRefreshRspackPlugin {
131135
new compiler.webpack.DefinePlugin(definedModules).apply(compiler);
132136
new compiler.webpack.ProvidePlugin(providedModules).apply(compiler);
133137

134-
const refreshPath = path.dirname(require.resolve('react-refresh'));
135138
compiler.options.resolve.alias = {
136-
'react-refresh': refreshPath,
139+
'react-refresh': refreshRuntimeDirPath,
137140
...compiler.options.resolve.alias,
138141
};
139142

src/options.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'node:path';
12
import type { RuleSetCondition } from '@rspack/core';
23
import type { IntegrationType } from './utils/getSocketIntegration';
34

@@ -107,8 +108,8 @@ const d = <K extends keyof PluginOptions>(
107108

108109
const normalizeOverlay = (options: PluginOptions['overlay']) => {
109110
const defaultOverlay: OverlayOptions = {
110-
entry: require.resolve('../client/errorOverlayEntry.js'),
111-
module: require.resolve('../client/overlay/index.js'),
111+
entry: path.join(__dirname, '../client/errorOverlayEntry.js'),
112+
module: path.join(__dirname, '../client/overlay/index.js'),
112113
sockIntegration: 'wds',
113114
};
114115
if (!options) {

src/paths.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import path from 'node:path';
22

3-
export const reactRefreshPath = require.resolve('../client/reactRefresh.js');
4-
export const reactRefreshEntryPath = require.resolve(
3+
export const reactRefreshPath = path.join(
4+
__dirname,
5+
'../client/reactRefresh.js',
6+
);
7+
export const reactRefreshEntryPath = path.join(
8+
__dirname,
59
'../client/reactRefreshEntry.js',
610
);
7-
export const refreshUtilsPath = require.resolve('../client/refreshUtils.js');
11+
export const refreshUtilsPath = path.join(
12+
__dirname,
13+
'../client/refreshUtils.js',
14+
);
815
export const refreshRuntimeDirPath = path.dirname(
916
require.resolve('react-refresh', {
1017
paths: [reactRefreshPath],

src/utils/getSocketIntegration.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
import path from 'node:path';
2+
13
export type IntegrationType = 'wds' | 'whm' | (string & {});
24

35
export function getSocketIntegration(integrationType: IntegrationType) {
46
let resolvedSocketIntegration: string;
57
switch (integrationType) {
68
case 'wds': {
7-
resolvedSocketIntegration = require.resolve('./sockets/WDSSocket');
9+
resolvedSocketIntegration = path.join(
10+
__dirname,
11+
'./sockets/WDSSocket.js',
12+
);
813
break;
914
}
1015
case 'whm': {
11-
resolvedSocketIntegration = require.resolve('./sockets/WHMEventSource');
16+
resolvedSocketIntegration = path.join(
17+
__dirname,
18+
'./sockets/WHMEventSource.js',
19+
);
1220
break;
1321
}
1422
default: {

0 commit comments

Comments
 (0)