1

是否有可能有一个没有回报的 Gulp 任务?

我正在尝试创建一个简单的构建任务来通知用户成功,但我之前编写的所有其他任务都会返回一些东西并使用管道来处理相关的操作。

正如您从下面的代码中看到的那样,我有一系列任务,我想在两个不同的构建过程中执行 -dev并且production- 在两者结束时,我有一个简单的process:build任务,它会触发重新加载任务并使用gulp-notify.

当使用gulp以下代码在命令行上运行时,实际上没有任何中断,并且构建过程进行得很好,但process:build任务本身似乎没有触发。

// Define our dependencies
var gulp = require('gulp'),
    del = require('del'),
    vinylPaths = require('vinyl-paths'),
    runSequence = require('run-sequence'),
    jpegtran = require('imagemin-jpegtran'),
    pngcrush = require('imagemin-pngcrush'),
    pngquant = require('imagemin-pngquant'),
    svgo = require('imagemin-svgo'),
    gulpLoadPlugins = require('gulp-load-plugins'),
    plugins = gulpLoadPlugins({ camelize: true });

//===============================================
// File specification
//===============================================

// Define the source and compliation paths for our files
var paths = {
    styles: {
        src: './inc/scss',
        files: './inc/scss/**.scss',
        dest: './public/inc/css'
    },
    images: {
        src: './inc/img',
        files: './inc/img/**.**',
        dest: './public/inc/img'
    }
};

//===============================================
// Error reporting
//===============================================

// Notify error
var onError = function (err) {
    plugins.util.beep();
    console.log(err);
    plugins.notify().write(err);
    this.emit('end');
};

//===============================================
// Compliation tasks
//===============================================

gulp.task('compile:scss', function() {
    return gulp.src(paths.styles.files)
        .pipe(plugins.plumber({
            errorHandler: onError
        }))
        .pipe(plugins.sourcemaps.init())
        .pipe(plugins.sass())
        .pipe(plugins.sourcemaps.write('/'))
        .pipe(gulp.dest(paths.styles.dest));
});

gulp.task('minify:css', function() {
  return gulp.src(['./public/inc/css/**.css', '!./public/inc/css/**.min.css'])
    .pipe(plugins.plumber({
        errorHandler: onError
    }))
    .pipe(plugins.cssmin())
    .pipe(plugins.rename({ suffix: '.min' }))
    .pipe(gulp.dest(paths.styles.dest));
});

gulp.task('compress:images', function() {
    return gulp.src(paths.images.files)
        .pipe(plugins.imagemin({
            progressive: true,
            interlaced: true,
            svgoPlugins: [{removeViewBox: false}],
            use: [
                jpegtran(),
                pngcrush(),
                pngquant(),
                svgo()
            ]
    }));
});

gulp.task('move:images', function() {
    return gulp.src(paths.images.files)
        .pipe(gulp.dest(paths.images.dest));
});

gulp.task('process:build', function() {
    plugins.livereload();
    plugins.notify({ message: "Build updated"});
});

//===============================================
// Sequenced tasks
//===============================================

// Run our sequenced tasks outlined above
gulp.task('dev', function(cb) {
    runSequence(
    'compile:scss',
    'move:images',
    'process:build', cb);
});

gulp.task('production', function(cb) {
    runSequence(
    'compile:scss',
    'minify:css',
    'compress:images',
    'move:images',
    'process:build', cb);
});

//===============================================
// Watch tasks
//===============================================

// Watch specified folders for changes
gulp.task('watch', function() {
    plugins.livereload.listen();

    var watcher = gulp.watch(['./inc/scss/**/**.scss', './inc/img/*.*'], ['dev']);

    watcher.on('change', function(event) {
        console.log(
            '[watcher] File ' + event.path.replace(/.*(?=sass)/,'') + ' was ' + event.type + ', compiling...'
        );
    });
});

//===============================================
// Tasks to use
//===============================================

// This is the default task - which is run when `gulp` is run
gulp.task('default', function(cb) {
    runSequence('dev', 'watch', cb);
});

gulp.task('production', function(cb) {
    runSequence('production', 'watch', cb);
});
4

0 回答 0