是否可以有一个没有回报的Gulp任务?
我尝试创建一个简单的构建任务,通知用户成功,但我之前编写的所有其他任务都会返回一些内容并使用管道来处理相关操作。
从下面的代码中可以看出,我已经完成了一系列任务,我希望在两个不同的构建过程中执行这些任务 - dev
和production
- 以及在两者结束时,我有一个简单的process:build
任务,它只会触发重新加载任务,并使用gulp-notify
发出成功通知。
使用以下代码在命令行上运行gulp
时,实际上没有任何内容中断,构建过程也很顺利,但process:build
任务本身似乎无法触发。< / p>
// 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);
});