-
-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
202 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const WebpackCLITestPlugin = require('../../utils/webpack-cli-test-plugin'); | ||
|
||
module.exports = { | ||
mode: 'development', | ||
devtool: false, | ||
plugins: [new WebpackCLITestPlugin(['mode'], false, 'hooks.compilation.taps')], | ||
watch: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('watch flag test'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
'use strict'; | ||
|
||
const stripAnsi = require('strip-ansi'); | ||
const { runAndGetWatchProc, isWebpack5 } = require('../../utils/test-utils'); | ||
const { writeFileSync } = require('fs'); | ||
const { resolve } = require('path'); | ||
|
||
const wordsInStatsv4 = ['Hash', 'Built at:', 'main.js']; | ||
const wordsInStatsv5 = ['asset', 'index.js', 'compiled successfully']; | ||
|
||
describe('watch variable', () => { | ||
it('should pass `WEBPACK_WATCH` env variable and recompile upon file change using the `watch` command', (done) => { | ||
const proc = runAndGetWatchProc(__dirname, ['watch', '--mode', 'development'], false, '', true); | ||
|
||
let modified = false; | ||
|
||
proc.stdout.on('data', (chunk) => { | ||
const data = stripAnsi(chunk.toString()); | ||
|
||
expect(data).not.toContain('FAIL'); | ||
|
||
if (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); | ||
} | ||
} | ||
|
||
if (!modified) { | ||
process.nextTick(() => { | ||
writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');`); | ||
}); | ||
|
||
modified = true; | ||
} else { | ||
proc.kill(); | ||
done(); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
it('should pass `WEBPACK_WATCH` env variable and recompile upon file change using the `--watch` option', (done) => { | ||
const proc = runAndGetWatchProc(__dirname, ['--watch', '--mode', 'development'], false, '', true); | ||
|
||
let modified = false; | ||
|
||
proc.stdout.on('data', (chunk) => { | ||
const data = stripAnsi(chunk.toString()); | ||
|
||
expect(data).not.toContain('FAIL'); | ||
|
||
if (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); | ||
} | ||
} | ||
|
||
if (!modified) { | ||
process.nextTick(() => { | ||
writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');`); | ||
}); | ||
|
||
modified = true; | ||
} else { | ||
proc.kill(); | ||
done(); | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const isInProcess = process.env.WEBPACK_WATCH; | ||
|
||
class CustomTestPlugin { | ||
constructor(isInEnvironment) { | ||
this.isInEnvironment = isInEnvironment; | ||
} | ||
apply(compiler) { | ||
compiler.hooks.done.tap('testPlugin', () => { | ||
if (!isInProcess && this.isInEnvironment) { | ||
console.log('PASS'); | ||
} else { | ||
console.log('FAIL'); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = (env) => { | ||
return { | ||
mode: 'development', | ||
devtool: false, | ||
plugins: [new CustomTestPlugin(env.WEBPACK_WATCH)], | ||
}; | ||
}; |