【发布时间】:2019-05-28 23:51:30
【问题描述】:
我正在重构我的 gulpfile,现在我正在使用 gulp v4,并且遇到了 gulp watch 无法运行我的 stylesCompileIncremental 函数的问题。任何帮助或指示将不胜感激。
我的重构包括:
- 切换到使用函数而不是 gulp.task
- 按照docs 使用
series和parallel - 在我的 gulpfile 底部导出公共任务,即
exports.stylesWatch = stylesWatch; - 在函数中添加回调告诉 Gulp 函数完成
受影响任务的代码如下(目录路径存储在 package.json 文件中,因此 pathConfig.ui... 值):
// Compile only particular Sass file that has import of changed file
function stylesCompileIncremental(cb) {
sassCompile({
source: getResultedFilesList(changedFilePath),
dest: pathConfig.ui.core.sass.dest,
alsoSearchIn: [pathConfig.ui.lib.resources]
});
cb();
}
// Compile all Sass files and watch for changes
function stylesWatch(cb) {
createImportsGraph();
var watcher = gulp.watch(pathConfig.ui.core.sass.src + '**/*.scss', gulp.parallel(devServReloadStyles));
watcher.on('change', function(event) {
changedFilePath = event;
});
cb();
}
// reload css separated into own function. No callback needed as returning event stream
function reloadCss() {
return gulp.src(generateFilePath)
.pipe($.connect.reload()); // css only reload
}
function devServReloadStyles(cb) {
gulp.series(stylesCompileIncremental, reloadCss);
cb();
}
当我使用重构的代码运行gulp stylesWatch 时,我得到以下输出(注意stylesCompileIncremental 任务未运行):
所以我的手表任务运行成功,但是当 devServReloadStyles 运行 stylesCompileIncremental 函数无法启动时出现问题。
重构前的原始代码(使用 gulp v3 时)如下:
// Compile only particular Sass file that has import of changed file
gulp.task('styles:compile:incremental', () => {
return sassCompile({
source: getResultedFilesList(changedFilePath),
dest: pathConfig.ui.core.sass.dest,
alsoSearchIn: [pathConfig.ui.lib.resources]
});
});
// Compile all Sass files and watch for changes
gulp.task('styles:watch', () => {
createImportsGraph();
gulp.watch(
pathConfig.ui.core.sass.src + '**/*.scss',
['devServ:reload:styles']
).on('change', event => changedFilePath = event.path);
});
// Reload the CSS links right after 'styles:compile:incremental' task is returned
gulp.task('devServ:reload:styles', ['styles:compile:incremental'], () => {
return gulp.src(generateFilePath) // css only reload
.pipe($.connect.reload());
});
运行styles:watch时的原始任务输出是这样的:
这是stylesCompileIncremental 中使用的 sassCompile 变量,我目前还没有更改。
/**
* Configurable Sass compilation
* @param {Object} config
*/
const sassCompile = config => {
const sass = require('gulp-sass');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const postProcessors = [
autoprefixer({
flexbox: 'no-2009'
})
];
return gulp.src(config.source)
.pipe($.sourcemaps.init({
loadMaps: true,
largeFile: true
}))
.pipe(sass({
includePaths: config.alsoSearchIn,
sourceMap: false,
outputStyle: 'compressed',
indentType: 'tab',
indentWidth: '1',
linefeed: 'lf',
precision: 10,
errLogToConsole: true
}))
.on('error', function (error) {
$.util.log('\x07');
$.util.log(error.message);
this.emit('end');
})
.pipe(postcss(postProcessors))
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest(config.dest));
};
更新
这是由于我的 devServReloadStyles 函数存在问题,尽管我仍然不确定原因。如果我更改我的stylesWatch 函数以使用原始devServ:reload:styles 任务stylesCompileIncremental 将运行。
// Compile all Sass files and watch for changes
function stylesWatch(cb) {
createImportsGraph();
var watcher = gulp.watch(pathConfig.ui.core.sass.src + '**/*.scss', gulp.parallel('devServ:reload:styles'));
watcher.on('change', function(event) {
changedFilePath = event;
});
cb();
}
不使用旧任务并将其作为函数仍然会很好。 谁能告诉我为什么我的重构版本不起作用并对它的外观有何建议?
【问题讨论】:
标签: javascript node.js gulp gulp-watch