javascript - 使用 gulp v4 重构的监视任务不起作用

标签 javascript node.js gulp gulp-watch

我正在重构我的 gulpfile,现在我正在使用 gulp v4,并且 gulp watch 没有运行我的 stylesCompileIncremental 函数时遇到问题。任何帮助或指示将不胜感激。

我的重构包括:

  • 改用函数而不是 gulp.task
  • 根据 docs 使用 seriesparallel
  • 在我的 gulpfile 的底部导出公共(public)任务,即 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 任务未运行):

Refactored output

因此,我的 watch 任务已成功运行,但是当 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 时的原始任务输出是这样的:

Original output

这是 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();
}

虽然不使用旧任务并将其作为函数,但仍然很好。 任何人都可以告诉我为什么我的重构版本不起作用并且对它的外观有任何建议吗?

最佳答案

我现在已经解决了这个问题。 gulp.seriesgulp.parallel 返回函数,因此无需将 stylesCompileIncrementalreloadCss 包装在另一个函数中IE。 devServReloadStyles。 根据布莱恩的评论 here .

所以我的功能:

function devServReloadStyles(cb) {
    gulp.series(stylesCompileIncremental, reloadCss);
cb();

可以只分配给一个变量:

const devServReloadStyles = gulp.series(stylesCompileIncremental, reloadCss);

我的 stylesWatch 任务已经在调用 devServReloadStyles:

// 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();
}

所以运行 gulp stylesWatch 现在运行 stylesCompileIncremental 作业(注意 devServReloadStyles 没有显示,因为它不是函数)。

enter image description here

关于javascript - 使用 gulp v4 重构的监视任务不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54004526/

相关文章:

javascript - 在 PDF 中使用 Javascript 列出 XFA 对象的属性

javascript - 分机号 : Override JsonReader or post-process

node.js - Schema.where().in 之后的数组顺序错误

polymer - 硫化 : inlineCss not working on Polymer 1. 0 项目

javascript - 具有深层次数据的 ReactJS 数据流

javascript - SonarQube 始终显示 Javascript 项目的 0.0% 代码覆盖率

Node.js 解释 REPL 中的错误?

javascript - 使用 ClojureScript 在 Meteor 中编写

node.js - ES6 + jasny-bootstrap - 错误 : Cannot find module jasny-bootstrap?

gulp - 使用文件目录的父目录作为 gulp 中的目标