Skip to content

Commit 464fdd7

Browse files
authored
fix: should not trigger dev hooks when building (#4165)
1 parent 1544c6d commit 464fdd7

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

e2e/cases/plugin-api/plugin-hooks/index.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,39 @@ rspackOnlyTest(
104104
},
105105
);
106106

107+
rspackOnlyTest(
108+
'should run plugin hooks correctly when running build and mode is development',
109+
async () => {
110+
const { plugin, names } = createPlugin();
111+
const rsbuild = await createRsbuild({
112+
cwd: __dirname,
113+
rsbuildConfig: {
114+
mode: 'development',
115+
plugins: [plugin],
116+
},
117+
});
118+
119+
const buildInstance = await rsbuild.build();
120+
121+
await buildInstance.close();
122+
123+
expect(names).toEqual([
124+
'ModifyRsbuildConfig',
125+
'ModifyEnvironmentConfig',
126+
'ModifyBundlerChain',
127+
'ModifyBundlerConfig',
128+
'BeforeCreateCompiler',
129+
'AfterCreateCompiler',
130+
'BeforeBuild',
131+
'BeforeEnvironmentCompile',
132+
'ModifyHTMLTags',
133+
'AfterEnvironmentCompile',
134+
'AfterBuild',
135+
'OnCloseBuild',
136+
]);
137+
},
138+
);
139+
107140
rspackOnlyTest(
108141
'should run plugin hooks correctly when running startDevServer',
109142
async ({ page }) => {

packages/compat/webpack/src/createCompiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export async function createCompiler(options: InitConfigsOptions) {
4646
done(stats as Rspack.Stats);
4747
});
4848

49-
if (context.normalizedConfig?.mode === 'development') {
49+
if (context.command === 'dev') {
5050
helpers.registerDevHook({
5151
compiler,
5252
context,

packages/core/src/createRsbuild.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ export async function createRsbuild(
144144
});
145145

146146
const preview = async (options: PreviewOptions = {}) => {
147+
context.command = 'preview';
148+
147149
if (!getNodeEnv()) {
148150
setNodeEnv('production');
149151
}
@@ -174,9 +176,12 @@ export async function createRsbuild(
174176
};
175177

176178
const build: Build = async (...args) => {
179+
context.command = 'build';
180+
177181
if (!getNodeEnv()) {
178182
setNodeEnv('production');
179183
}
184+
180185
const buildInstance = await providerInstance.build(...args);
181186
return {
182187
...buildInstance,
@@ -188,16 +193,22 @@ export async function createRsbuild(
188193
};
189194

190195
const startDevServer: StartDevServer = (...args) => {
196+
context.command = 'dev';
197+
191198
if (!getNodeEnv()) {
192199
setNodeEnv('development');
193200
}
201+
194202
return providerInstance.startDevServer(...args);
195203
};
196204

197205
const createDevServer: CreateDevServer = (...args) => {
206+
context.command = 'dev';
207+
198208
if (!getNodeEnv()) {
199209
setNodeEnv('development');
200210
}
211+
201212
return providerInstance.createDevServer(...args);
202213
};
203214

packages/core/src/provider/createCompiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
5757
isCompiling = true;
5858
});
5959

60-
if (context.normalizedConfig?.mode === 'production') {
60+
if (context.command === 'build') {
6161
compiler.hooks.run.tap('rsbuild:run', logRspackVersion);
6262
}
6363

@@ -113,7 +113,7 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
113113
},
114114
);
115115

116-
if (context.normalizedConfig?.mode === 'development') {
116+
if (context.command === 'dev') {
117117
registerDevHook({
118118
context,
119119
compiler,

packages/core/src/types/context.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ export type InternalContext = RsbuildContext & {
4444
environments: Record<string, EnvironmentContext>;
4545
/** Only build specified environment. */
4646
specifiedEnvironments?: string[];
47+
/**
48+
* The command type.
49+
*
50+
* - dev: `rsbuild dev` or `rsbuild.startDevServer()`
51+
* - build: `rsbuild build` or `rsbuild.build()`
52+
* - preview: `rsbuild preview` or `rsbuild.preview()`
53+
*/
54+
command?: 'dev' | 'build' | 'preview';
4755
};

0 commit comments

Comments
 (0)