fix(v2): wait plugin watching approach (#1390)

* fix(v2): wait plugin watching approach

* Update package.json

* rush fix

* Use 2. x branch

* Update WaitPlugin.js

* yarn.lock

* use polling

* switch chokidar w-> wait-on npm package
This commit is contained in:
Yangshun Tay 2019-04-25 06:18:18 -07:00 committed by Endilie Yacop Sucipto
parent cb8c7125db
commit 6d53264652
4 changed files with 75 additions and 19 deletions

View file

@ -5,8 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/
const fs = require('fs');
const chokidar = require('chokidar');
const path = require('path');
const fs = require('fs-extra');
const waitOn = require('wait-on');
class WaitPlugin {
constructor(options) {
@ -16,18 +17,20 @@ class WaitPlugin {
apply(compiler) {
// Before finishing the compilation step
compiler.hooks.make.tapAsync('WaitPlugin', (compilation, callback) => {
const {filepath} = this;
const watcher = chokidar.watch(filepath, {
awaitWriteFinish: true,
disableGlobbing: true,
});
// To prevent 'waitOn' error on waiting non-existing directory
fs.ensureDirSync(path.dirname(this.filepath));
watcher.on('add', () => {
if (fs.existsSync(filepath)) {
watcher.close();
// Wait until file exist
waitOn({
resources: [this.filepath],
interval: 300,
})
.then(() => {
callback();
}
});
})
.catch(error => {
console.warn(`WaitPlugin error: ${error}`);
});
});
}
}

View file

@ -68,6 +68,7 @@
"static-site-generator-webpack-plugin": "^3.4.2",
"style-loader": "^0.22.1",
"terser-webpack-plugin": "^1.2.3",
"wait-on": "^3.2.0",
"webpack": "^4.29.6",
"webpack-bundle-analyzer": "^3.1.0",
"webpack-dev-server": "^3.2.1",