迁移到 Gulp 4 我遇到了严重的问题:使用 gulp.watch 或 gulp-watch 时对更改没有反应。
import gulp from 'gulp';
// import {get as browserSync} from 'browser-sync';
import watch from 'gulp-watch';
// const bs = browserSync('server');
var bSync = require('browser-sync').create();
gulp.task('browser-sync', function() {
bSync.init({
server: {
baseDir: "../"
},
notify: true
});
});
gulp.task('watch', gulp.parallel('browser-sync', ()=> {
global.watch = true;
gulp.watch('app/{styles,blocks}/**/*.+(less|styl)', gulp.series('styles', 'styles:lint', bSync.reload('styles/app-min.css')));
gulp.watch(['app/+(images|fonts)/**/*', 'app/*.html'], gulp.parallel('copy', bSync.reload)); // or watch( instead of gulp.watch(
gulp.start('scripts:watch');
bSync.reload();
}));
和脚本代码:
import gulp from 'gulp';
import errorHandler from 'gulp-plumber-error-handler';
import makeWebpackConfig from '../webpack.conf.js';
import webpack from 'webpack';
const {NODE_ENV, NOTIFY} = process.env;
const isDebug = NODE_ENV !== 'production';
const scriptsErrorHandler = errorHandler('Error in \'scripts\' task');
function runWebpack(watch = false) {
return function (callback) {
const webpackConfig = makeWebpackConfig({
watch,
debug: isDebug,
sourcemaps: isDebug,
notify: NOTIFY
});
return webpack(webpackConfig, (error, stats) => {
const jsonStats = stats.toJson();
if (jsonStats.errors.length) {
jsonStats.errors.forEach(message => {
scriptsErrorHandler.call({emit() {}}, {message});
});
}
if (watch === false) callback();
});
};
}
gulp.task('watch', runWebpack(false));
gulp.task('scripts:watch', runWebpack(true));
我的任务位于任务文件夹内。为了加载任务,默认任务使用 gulp-hub 模块:
var HubRegistry = require('gulp-hub');
gulp.registry(new HubRegistry(['*.js']));
这里的问题在哪里?