Skip to content

Commit d7b1ae3

Browse files
authored
fix: rerun onBeforeDevCompile hook correctly with multiple environments (#5980)
1 parent 8e318ea commit d7b1ae3

File tree

2 files changed

+99
-19
lines changed

2 files changed

+99
-19
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { join } from 'node:path';
2+
import {
3+
getRandomPort,
4+
gotoPage,
5+
proxyConsole,
6+
rspackOnlyTest,
7+
} from '@e2e/helper';
8+
import { expect } from '@playwright/test';
9+
import { createRsbuild, type RsbuildPlugin } from '@rsbuild/core';
10+
import fse from 'fs-extra';
11+
12+
const createPlugin = () => {
13+
const names: string[] = [];
14+
15+
const plugin: RsbuildPlugin = {
16+
name: 'test-plugin',
17+
setup(api) {
18+
api.onBeforeEnvironmentCompile(({ environment }) => {
19+
names.push(`BeforeEnvironmentCompile ${environment.name}`);
20+
});
21+
api.onBeforeDevCompile(() => {
22+
names.push('BeforeDevCompile');
23+
});
24+
},
25+
};
26+
27+
return { plugin, names };
28+
};
29+
30+
rspackOnlyTest(
31+
'should run onBeforeDevCompile hook correctly when rebuild in dev with multiple environments',
32+
async ({ page }) => {
33+
process.env.NODE_ENV = 'development';
34+
const cwd = __dirname;
35+
const filePath = join(cwd, 'test-temp-src', 'index.js');
36+
await fse.outputFile(filePath, "console.log('1');");
37+
38+
const port = await getRandomPort();
39+
40+
const { plugin, names } = createPlugin();
41+
42+
const { expectLog, restore, expectBuildEnd } = proxyConsole();
43+
44+
const rsbuild = await createRsbuild({
45+
cwd: __dirname,
46+
rsbuildConfig: {
47+
plugins: [plugin],
48+
server: {
49+
port,
50+
},
51+
environments: {
52+
web: {},
53+
node: {
54+
source: {
55+
entry: {
56+
index: './test-temp-src/index.js',
57+
},
58+
},
59+
},
60+
},
61+
performance: {
62+
printFileSize: false,
63+
},
64+
},
65+
});
66+
67+
const result = await rsbuild.startDevServer();
68+
69+
await gotoPage(page, result);
70+
71+
expect(names.includes('BeforeDevCompile')).toBeTruthy();
72+
expect(names.includes('BeforeEnvironmentCompile node')).toBeTruthy();
73+
expect(names.includes('BeforeEnvironmentCompile web')).toBeTruthy();
74+
75+
names.length = 0;
76+
77+
// rebuild
78+
await fse.outputFile(filePath, "console.log('2');");
79+
await expectLog('building test-temp-src');
80+
await expectBuildEnd();
81+
82+
expect(names).toEqual([
83+
'BeforeDevCompile',
84+
// only recompile the node environment which is affected by the file change
85+
'BeforeEnvironmentCompile node',
86+
]);
87+
88+
await result.server.close();
89+
90+
restore();
91+
92+
process.env.NODE_ENV = 'test';
93+
},
94+
);

packages/core/src/hooks.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -271,22 +271,19 @@ const onBeforeCompile = ({
271271
if (isMultiCompiler(compiler)) {
272272
const { compilers } = compiler;
273273

274-
let doneCompilers = 0;
275-
276274
let waitBeforeCompileDone: Promise<void> | undefined;
277275

276+
// Executed when a watching compilation has been invalidated (re-compilation)
277+
compiler.hooks.invalid.tap(name, () => {
278+
waitBeforeCompileDone = undefined;
279+
});
280+
278281
for (let index = 0; index < compilers.length; index++) {
279282
const compiler = compilers[index];
280-
let compilerDone = false;
281283

282284
(isWatch ? compiler.hooks.watchRun : compiler.hooks.run).tapPromise(
283285
name,
284286
async () => {
285-
if (!compilerDone) {
286-
compilerDone = true;
287-
doneCompilers++;
288-
}
289-
290287
if (!waitBeforeCompileDone) {
291288
waitBeforeCompileDone = beforeCompile?.();
292289
}
@@ -297,17 +294,6 @@ const onBeforeCompile = ({
297294
await beforeEnvironmentCompiler(index);
298295
},
299296
);
300-
301-
compiler.hooks.invalid.tap(name, () => {
302-
if (compilerDone) {
303-
compilerDone = false;
304-
doneCompilers--;
305-
}
306-
307-
if (doneCompilers <= 0) {
308-
waitBeforeCompileDone = undefined;
309-
}
310-
});
311297
}
312298
} else {
313299
(isWatch ? compiler.hooks.watchRun : compiler.hooks.run).tapPromise(

0 commit comments

Comments
 (0)