forked from rstackjs/rspack-plugin-react-refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
160 lines (140 loc) · 4.83 KB
/
index.ts
File metadata and controls
160 lines (140 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import type { Compiler } from '@rspack/core';
import { normalizeOptions } from './options';
import type { NormalizedPluginOptions, PluginOptions } from './options';
import {
getRefreshRuntimeDirPath,
getRefreshRuntimePaths,
reactRefreshPath,
refreshUtilsPath,
} from './paths';
import { getAdditionalEntries } from './utils/getAdditionalEntries';
import { getIntegrationEntry } from './utils/getIntegrationEntry';
import {
type IntegrationType,
getSocketIntegration,
} from './utils/getSocketIntegration';
export type { PluginOptions };
function addEntry(entry: string, compiler: Compiler) {
new compiler.rspack.EntryPlugin(compiler.context, entry, {
name: undefined,
}).apply(compiler);
}
function addSocketEntry(sockIntegration: IntegrationType, compiler: Compiler) {
const integrationEntry = getIntegrationEntry(sockIntegration);
if (integrationEntry) {
addEntry(integrationEntry, compiler);
}
}
const PLUGIN_NAME = 'ReactRefreshRspackPlugin';
class ReactRefreshRspackPlugin {
options: NormalizedPluginOptions;
/**
* @deprecated
*/
static get deprecated_runtimePaths() {
return getRefreshRuntimePaths();
}
constructor(options: PluginOptions = {}) {
this.options = normalizeOptions(options);
}
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;
}
const addEntries = getAdditionalEntries({
devServer: compiler.options.devServer,
options: this.options,
});
if (this.options.injectEntry) {
for (const entry of addEntries.prependEntries) {
addEntry(entry, compiler);
}
}
if (
this.options.overlay !== false &&
this.options.overlay.sockIntegration
) {
addSocketEntry(this.options.overlay.sockIntegration, compiler);
}
for (const entry of addEntries.overlayEntries) {
addEntry(entry, compiler);
}
new compiler.rspack.ProvidePlugin({
$ReactRefreshRuntime$: reactRefreshPath,
}).apply(compiler);
if (this.options.injectLoader) {
compiler.options.module.rules.unshift({
test: this.options.test,
// biome-ignore lint: exists
include: this.options.include!,
exclude: {
// biome-ignore lint: exists
or: [this.options.exclude!, [...getRefreshRuntimePaths()]].filter(
Boolean,
),
},
resourceQuery: this.options.resourceQuery,
dependency: {
// Assets loaded via `new URL("static/sdk.js", import.meta.url)` are asset modules
// React Refresh should not be injected for asset modules as they are static resources
not: ['url'],
},
use: this.options.reactRefreshLoader,
});
}
const definedModules: Record<string, string | boolean> = {
// For Multiple Instance Mode
__react_refresh_library__: JSON.stringify(
compiler.rspack.Template.toIdentifier(
this.options.library ||
compiler.options.output.uniqueName ||
compiler.options.output.library,
),
),
__reload_on_runtime_errors__: this.options.reloadOnRuntimeErrors,
};
const providedModules: Record<string, string> = {
__react_refresh_utils__: refreshUtilsPath,
};
if (this.options.overlay === false) {
// Stub errorOverlay module so their calls can be erased
definedModules.__react_refresh_error_overlay__ = false;
definedModules.__react_refresh_socket__ = false;
} else {
if (this.options.overlay.module) {
providedModules.__react_refresh_error_overlay__ = require.resolve(
this.options.overlay.module,
);
}
if (this.options.overlay.sockIntegration) {
providedModules.__react_refresh_socket__ = getSocketIntegration(
this.options.overlay.sockIntegration,
);
}
}
new compiler.rspack.DefinePlugin(definedModules).apply(compiler);
new compiler.rspack.ProvidePlugin(providedModules).apply(compiler);
compiler.options.resolve.alias = {
'react-refresh': getRefreshRuntimeDirPath(),
...compiler.options.resolve.alias,
};
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.additionalTreeRuntimeRequirements.tap(
PLUGIN_NAME,
(_, runtimeRequirements) => {
runtimeRequirements.add(compiler.rspack.RuntimeGlobals.moduleCache);
},
);
});
}
}
export { ReactRefreshRspackPlugin };
export default ReactRefreshRspackPlugin;