Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add compilation lifecycle in watch instance #1903

Merged
merged 8 commits into from Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -36,7 +36,7 @@
"prepsuite": "node scripts/prepareSuite.js",
"pretest": "yarn build && yarn lint && yarn prepsuite",
"test": "jest --reporters=default --reporters=jest-junit",
"test:cli": "jest test/ --reporters=default --reporters=jest-junit --forceExit",
"test:cli": "jest test --reporters=default --reporters=jest-junit --forceExit",
"test:packages": "jest packages/ --reporters=default --reporters=jest-junit --forceExit",
"test:ci": "yarn test:cli && yarn test:packages",
"test:watch": "jest test/ packages/ --watch",
Expand Down
22 changes: 22 additions & 0 deletions packages/webpack-cli/lib/utils/Compiler.js
Expand Up @@ -4,6 +4,27 @@ const logger = require('./logger');
const { writeFileSync } = require('fs');
const bailAndWatchWarning = require('./warnings/bailAndWatchWarning');

const assignWatchHooks = (compiler) => {
compiler.hooks.watchRun.tap('watchInfo', (compilation) => {
const compilationName = compilation.name || '';
logger.raw(`\nCompilation ${compilationName} starting…\n`);
});
compiler.hooks.done.tap('watchInfo', (compilation) => {
const compilationName = compilation.name || '';
logger.raw(`\nCompilation ${compilationName} finished\n`);
});
};

const watchInfo = (compiler) => {
if (compiler.compilers) {
compiler.compilers.map((comp) => {
assignWatchHooks(comp);
});
} else {
assignWatchHooks(compiler);
}
};

class Compiler {
constructor() {
this.compilerOptions = {};
Expand Down Expand Up @@ -151,6 +172,7 @@ class Compiler {
});
process.stdin.resume();
}
watchInfo(this.compiler);
await this.invokeWatchInstance(lastHash, options, outputOptions, watchOptions);
} else {
return await this.invokeCompilerInstance(lastHash, options, outputOptions);
Expand Down
37 changes: 31 additions & 6 deletions test/watch/watch-flag.test.js
@@ -1,9 +1,8 @@
'use strict';

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

const wordsInStatsv4 = ['Hash', 'Version', 'Time', 'Built at:', 'main.js'];
const wordsInStatsv5 = ['asset', 'index.js', 'compiled successfully'];
Expand All @@ -14,13 +13,13 @@ describe('--watch flag', () => {
let semaphore = 1;
proc.stdout.on('data', (chunk) => {
const data = chunk.toString();
if (semaphore === 1 && data.includes('watching files for updates')) {
if (data.includes('watching files for updates')) {
writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');`);
semaphore--;
semaphore = 0;
return;
}
if (semaphore === 0) {
if (version.startsWith('5')) {
if (semaphore === 0 && data.includes('index.js')) {
if (isWebpack5) {
for (const word of wordsInStatsv5) {
expect(data).toContain(word);
}
Expand All @@ -36,4 +35,30 @@ describe('--watch flag', () => {
}
});
});

it('should print compilation lifecycle', (done) => {
const proc = runAndGetWatchProc(__dirname, ['--watch'], false, '', true);
let semaphore = 0;
proc.stdout.on('data', (chunk) => {
const data = chunk.toString();
if (data.includes('Compilation starting') || data.includes('Compilation finished')) {
semaphore++;
}
// TODO Fix on windows
if ((isWindows || semaphore === 2) && data.includes('index.js')) {
if (isWebpack5) {
for (const word of wordsInStatsv5) {
expect(data).toContain(word);
}
} else {
for (const word of wordsInStatsv4) {
expect(data).toContain(word);
}
}
proc.kill();
done();
return;
}
});
});
});