From 59a34d72f34788d0c2885774d84380cead82a07b Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 10 Oct 2020 14:14:22 +0530 Subject: [PATCH 1/8] fix: add compilation lifecycle in watch flag --- packages/webpack-cli/lib/utils/Compiler.js | 22 +++++++++++++ test/watch/watch-flag.test.js | 36 ++++++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/packages/webpack-cli/lib/utils/Compiler.js b/packages/webpack-cli/lib/utils/Compiler.js index d2cd7949816..15279dc855b 100644 --- a/packages/webpack-cli/lib/utils/Compiler.js +++ b/packages/webpack-cli/lib/utils/Compiler.js @@ -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 = {}; @@ -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); diff --git a/test/watch/watch-flag.test.js b/test/watch/watch-flag.test.js index fe83e31f7c7..2581e57f3f0 100644 --- a/test/watch/watch-flag.test.js +++ b/test/watch/watch-flag.test.js @@ -14,12 +14,41 @@ 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 = 0; + return; + } + if (semaphore === 0 && data.includes('index.js')) { + if (version.startsWith('5')) { + for (const word of wordsInStatsv5) { + expect(data).toContain(word); + } + } else { + for (const word of wordsInStatsv4) { + expect(data).toContain(word); + } + } semaphore--; + proc.kill(); + done(); return; } + }); + }); + + 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 (semaphore === 0) { + expect(data).toContain('Compilation starting'); + } + if (semaphore === 1) { + expect(data).toContain('Compilation finished'); + } + if (semaphore === 2) { if (version.startsWith('5')) { for (const word of wordsInStatsv5) { expect(data).toContain(word); @@ -29,11 +58,14 @@ describe('--watch flag', () => { expect(data).toContain(word); } } - semaphore--; + } + if (semaphore === 3) { + expect(data).toContain('watching files for updates'); proc.kill(); done(); return; } + semaphore++; }); }); }); From 761c5281605837175a8c7cd37154e80540746ce6 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 10 Oct 2020 14:34:22 +0530 Subject: [PATCH 2/8] fix: tests --- test/watch/watch-flag.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/watch/watch-flag.test.js b/test/watch/watch-flag.test.js index 2581e57f3f0..af15ad90c89 100644 --- a/test/watch/watch-flag.test.js +++ b/test/watch/watch-flag.test.js @@ -42,6 +42,7 @@ describe('--watch flag', () => { let semaphore = 0; proc.stdout.on('data', (chunk) => { const data = chunk.toString(); + console.log({ data, semaphore }); if (semaphore === 0) { expect(data).toContain('Compilation starting'); } From 5f7e89c6aac5f6aeea1db723bb1a5069fa582067 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 10 Oct 2020 15:19:20 +0530 Subject: [PATCH 3/8] fix: tests --- test/watch/watch-flag.test.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/test/watch/watch-flag.test.js b/test/watch/watch-flag.test.js index af15ad90c89..6722a309074 100644 --- a/test/watch/watch-flag.test.js +++ b/test/watch/watch-flag.test.js @@ -42,14 +42,10 @@ describe('--watch flag', () => { let semaphore = 0; proc.stdout.on('data', (chunk) => { const data = chunk.toString(); - console.log({ data, semaphore }); - if (semaphore === 0) { - expect(data).toContain('Compilation starting'); + if (data.includes('Compilation starting') || data.includes('Compilation finished')) { + semaphore++; } - if (semaphore === 1) { - expect(data).toContain('Compilation finished'); - } - if (semaphore === 2) { + if (semaphore === 2 && data.includes('index.js')) { if (version.startsWith('5')) { for (const word of wordsInStatsv5) { expect(data).toContain(word); @@ -59,14 +55,10 @@ describe('--watch flag', () => { expect(data).toContain(word); } } - } - if (semaphore === 3) { - expect(data).toContain('watching files for updates'); proc.kill(); done(); return; } - semaphore++; }); }); }); From 198be08032a4c8037a209a72038c6911444f42a4 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 10 Oct 2020 15:57:27 +0530 Subject: [PATCH 4/8] fix: tests --- test/watch/watch-flag.test.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/watch/watch-flag.test.js b/test/watch/watch-flag.test.js index 6722a309074..7eb89cc10ec 100644 --- a/test/watch/watch-flag.test.js +++ b/test/watch/watch-flag.test.js @@ -1,9 +1,8 @@ 'use strict'; -const { runAndGetWatchProc } = require('../utils/test-utils'); +const { runAndGetWatchProc, isWebpack5 } = 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']; @@ -20,7 +19,7 @@ describe('--watch flag', () => { return; } if (semaphore === 0 && data.includes('index.js')) { - if (version.startsWith('5')) { + if (isWebpack5) { for (const word of wordsInStatsv5) { expect(data).toContain(word); } @@ -46,7 +45,7 @@ describe('--watch flag', () => { semaphore++; } if (semaphore === 2 && data.includes('index.js')) { - if (version.startsWith('5')) { + if (isWebpack5) { for (const word of wordsInStatsv5) { expect(data).toContain(word); } From e978907420e1ef5f4409bc21ecb0f69c53896254 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 10 Oct 2020 16:14:23 +0530 Subject: [PATCH 5/8] fix: tests --- package.json | 2 +- test/watch/watch-flag.test.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index b284067d621..3f700a86fd1 100644 --- a/package.json +++ b/package.json @@ -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/watch --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", diff --git a/test/watch/watch-flag.test.js b/test/watch/watch-flag.test.js index 7eb89cc10ec..6c066aa1a56 100644 --- a/test/watch/watch-flag.test.js +++ b/test/watch/watch-flag.test.js @@ -41,6 +41,7 @@ describe('--watch flag', () => { let semaphore = 0; proc.stdout.on('data', (chunk) => { const data = chunk.toString(); + console.log({ data, semaphore }); if (data.includes('Compilation starting') || data.includes('Compilation finished')) { semaphore++; } From 9d9d644cd66ea322df3f0a35341e721c3e6260e0 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 10 Oct 2020 16:52:20 +0530 Subject: [PATCH 6/8] fix: tests --- test/watch/watch-flag.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/watch/watch-flag.test.js b/test/watch/watch-flag.test.js index 6c066aa1a56..c478bc60bd7 100644 --- a/test/watch/watch-flag.test.js +++ b/test/watch/watch-flag.test.js @@ -45,7 +45,7 @@ describe('--watch flag', () => { if (data.includes('Compilation starting') || data.includes('Compilation finished')) { semaphore++; } - if (semaphore === 2 && data.includes('index.js')) { + if (data.includes('index.js')) { if (isWebpack5) { for (const word of wordsInStatsv5) { expect(data).toContain(word); From 557684fec040fe024a09343f8d4a9320d5076da4 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 10 Oct 2020 17:03:34 +0530 Subject: [PATCH 7/8] fix: tests --- test/watch/watch-flag.test.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/watch/watch-flag.test.js b/test/watch/watch-flag.test.js index c478bc60bd7..8db5f30da61 100644 --- a/test/watch/watch-flag.test.js +++ b/test/watch/watch-flag.test.js @@ -45,7 +45,7 @@ describe('--watch flag', () => { if (data.includes('Compilation starting') || data.includes('Compilation finished')) { semaphore++; } - if (data.includes('index.js')) { + if (semaphore === 2 && data.includes('index.js')) { if (isWebpack5) { for (const word of wordsInStatsv5) { expect(data).toContain(word); @@ -55,10 +55,12 @@ describe('--watch flag', () => { expect(data).toContain(word); } } - proc.kill(); - done(); return; } + setTimeout(() => { + proc.kill(); + done(); + }, 2000); }); }); }); From 183631298193dda499c7f81add7f1fb6b2069fc7 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 10 Oct 2020 17:17:01 +0530 Subject: [PATCH 8/8] fix: tests --- package.json | 2 +- test/watch/watch-flag.test.js | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 3f700a86fd1..7bfc01215b6 100644 --- a/package.json +++ b/package.json @@ -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/watch --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", diff --git a/test/watch/watch-flag.test.js b/test/watch/watch-flag.test.js index 8db5f30da61..abdd52c2839 100644 --- a/test/watch/watch-flag.test.js +++ b/test/watch/watch-flag.test.js @@ -1,6 +1,6 @@ 'use strict'; -const { runAndGetWatchProc, isWebpack5 } = require('../utils/test-utils'); +const { runAndGetWatchProc, isWebpack5, isWindows } = require('../utils/test-utils'); const { writeFileSync } = require('fs'); const { resolve } = require('path'); @@ -41,11 +41,11 @@ describe('--watch flag', () => { let semaphore = 0; proc.stdout.on('data', (chunk) => { const data = chunk.toString(); - console.log({ data, semaphore }); if (data.includes('Compilation starting') || data.includes('Compilation finished')) { semaphore++; } - if (semaphore === 2 && data.includes('index.js')) { + // TODO Fix on windows + if ((isWindows || semaphore === 2) && data.includes('index.js')) { if (isWebpack5) { for (const word of wordsInStatsv5) { expect(data).toContain(word); @@ -55,12 +55,10 @@ describe('--watch flag', () => { expect(data).toContain(word); } } - return; - } - setTimeout(() => { proc.kill(); done(); - }, 2000); + return; + } }); }); });