Open ivancuric opened 8 years ago
Nevermind, it was a sync task. Removing js
as a dependency task (which was always running) fixed it.
Replaced it with gulp.start('js')
However, now .on('end', reload)
serves no purpose, since it will never trigger. If I enable gulp.watch(src.js).on('change', browserSync.reload)
it will run in async and reload the page before webpack is finished compiling.
Can webpack when using watch: true
somehow signal to gulp that it's done and trigger a reload?
Just, add handler, and trigger brwser sync from them. Something like that:
....
let webpackChangeHandler = function (err, stats) {
$.util.log(stats.toString({
colors: $.util.colors.supportsColor,
chunks: false,
hash: false,
version: false
}));
browserSync.reload();
firstBuildReady = true;
};
....
return gulp.src('')
.pipe(plumber({errorHandler: conf.errorHandler('webpack-stream')}))
.pipe(webpackStream(webpackOptions, null, webpackChangeHandler))
.pipe(gulp.dest('valamar/static/js/dist/'))
.on('data', () => {
if (firstBuildReady && callback) callback()
});
Not sure if I follow to be honest. :\ I guess I'll just add a watch to the dist folder as well.
@ivancuric
Can webpack when using watch: true somehow signal to gulp that it's done and trigger a reload?
When using webpack-stream
that is possible by passing a callback as the 3rd parameter, as in @zaqqaz's example above:
.pipe(webpackStream(webpackOptions, null, webpackChangeHandler))
That function will be called when the compilation is done, so you can call BrowserSync.reload()
there to get the updated code.
@zaqqaz A complete example would be lovely.
When using watch:true
in webpack, you probably want to run it as a parallel gulp task.
webpack.config.js:
module.exports = {
...
watch: true,
...
}
gulpfile.js:
gulp.task('js', (done) => {
return gulp.src([
...
])
.pipe(webpackStream(webpackConfig, null, (err, stats) => {
browserSync.reload()
done()
}))
.pipe(gulp.dest(...))
})
gulp.task('default', gulp.parallel(['sass', 'js']))
Pass done
callback from gulp task js, trigger browsersync reload, then call done
in webpack stream handler to exit the gulp task properly.
Use gulp.parallel
, so now webpack watch is a task on its own, independent from other gulp tasks you have in your pipeline.
Hope it helps.
If I have
watch: true
enabled in the webpack config, BrowserSync won't start for some reason. Note, I don't have watching enabled in gulp.JS task:
Default gulp task:
Webpack config: