【问题标题】:Refactored watch task using gulp v4 doesn't work使用 gulp v4 重构的监视任务不起作用
【发布时间】:2019-05-28 23:51:30
【问题描述】:

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

我的重构包括:

  • 切换到使用函数而不是 gulp.task
  • 按照docs 使用seriesparallel
  • 在我的 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


    【解决方案1】:

    我现在已经解决了这个问题。 gulp.seriesgulp.parallel 返回函数,因此无需将 stylesCompileIncrementalreloadCss 包装在另一个函数中,即。 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 不显示,因为它不是函数)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      相关资源
      最近更新 更多