Skip to content

Commit 02b6d21

Browse files
authored
fix: add compilation lifecycle in watch instance (#1903)
1 parent ceaeefb commit 02b6d21

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"prepsuite": "node scripts/prepareSuite.js",
3737
"pretest": "yarn build && yarn lint && yarn prepsuite",
3838
"test": "jest --reporters=default --reporters=jest-junit",
39-
"test:cli": "jest test/ --reporters=default --reporters=jest-junit --forceExit",
39+
"test:cli": "jest test --reporters=default --reporters=jest-junit --forceExit",
4040
"test:packages": "jest packages/ --reporters=default --reporters=jest-junit --forceExit",
4141
"test:ci": "yarn test:cli && yarn test:packages",
4242
"test:watch": "jest test/ packages/ --watch",

packages/webpack-cli/lib/utils/Compiler.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ const logger = require('./logger');
44
const { writeFileSync } = require('fs');
55
const bailAndWatchWarning = require('./warnings/bailAndWatchWarning');
66

7+
const assignWatchHooks = (compiler) => {
8+
compiler.hooks.watchRun.tap('watchInfo', (compilation) => {
9+
const compilationName = compilation.name || '';
10+
logger.raw(`\nCompilation ${compilationName} starting…\n`);
11+
});
12+
compiler.hooks.done.tap('watchInfo', (compilation) => {
13+
const compilationName = compilation.name || '';
14+
logger.raw(`\nCompilation ${compilationName} finished\n`);
15+
});
16+
};
17+
18+
const watchInfo = (compiler) => {
19+
if (compiler.compilers) {
20+
compiler.compilers.map((comp) => {
21+
assignWatchHooks(comp);
22+
});
23+
} else {
24+
assignWatchHooks(compiler);
25+
}
26+
};
27+
728
class Compiler {
829
constructor() {
930
this.compilerOptions = {};
@@ -151,6 +172,7 @@ class Compiler {
151172
});
152173
process.stdin.resume();
153174
}
175+
watchInfo(this.compiler);
154176
await this.invokeWatchInstance(lastHash, options, outputOptions, watchOptions);
155177
} else {
156178
return await this.invokeCompilerInstance(lastHash, options, outputOptions);

test/watch/watch-flag.test.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict';
22

3-
const { runAndGetWatchProc } = require('../utils/test-utils');
3+
const { runAndGetWatchProc, isWebpack5, isWindows } = require('../utils/test-utils');
44
const { writeFileSync } = require('fs');
55
const { resolve } = require('path');
6-
const { version } = require('webpack');
76

87
const wordsInStatsv4 = ['Hash', 'Version', 'Time', 'Built at:', 'main.js'];
98
const wordsInStatsv5 = ['asset', 'index.js', 'compiled successfully'];
@@ -14,13 +13,13 @@ describe('--watch flag', () => {
1413
let semaphore = 1;
1514
proc.stdout.on('data', (chunk) => {
1615
const data = chunk.toString();
17-
if (semaphore === 1 && data.includes('watching files for updates')) {
16+
if (data.includes('watching files for updates')) {
1817
writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');`);
19-
semaphore--;
18+
semaphore = 0;
2019
return;
2120
}
22-
if (semaphore === 0) {
23-
if (version.startsWith('5')) {
21+
if (semaphore === 0 && data.includes('index.js')) {
22+
if (isWebpack5) {
2423
for (const word of wordsInStatsv5) {
2524
expect(data).toContain(word);
2625
}
@@ -36,4 +35,30 @@ describe('--watch flag', () => {
3635
}
3736
});
3837
});
38+
39+
it('should print compilation lifecycle', (done) => {
40+
const proc = runAndGetWatchProc(__dirname, ['--watch'], false, '', true);
41+
let semaphore = 0;
42+
proc.stdout.on('data', (chunk) => {
43+
const data = chunk.toString();
44+
if (data.includes('Compilation starting') || data.includes('Compilation finished')) {
45+
semaphore++;
46+
}
47+
// TODO Fix on windows
48+
if ((isWindows || semaphore === 2) && data.includes('index.js')) {
49+
if (isWebpack5) {
50+
for (const word of wordsInStatsv5) {
51+
expect(data).toContain(word);
52+
}
53+
} else {
54+
for (const word of wordsInStatsv4) {
55+
expect(data).toContain(word);
56+
}
57+
}
58+
proc.kill();
59+
done();
60+
return;
61+
}
62+
});
63+
});
3964
});

0 commit comments

Comments
 (0)