Is it possible to have a Gulp task which doesn't have a return?
I'm trying to create a simple build tasks which notifies the user on success, but all the other tasks I've written previously return something and use pipes to handle associated actions.
As you can see from the below code, I've got a series of tasks which I'd like to perform in two different build processes - dev
and production
- and at the end of both, I've got a simple process:build
task which just fires off a reload task and sends out a success notification using gulp-notify
.
When running gulp
on the command line with the following code, nothing actually breaks and the build process goes along just fine, but the process:build
task itself doesn't seem to fire.
// 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);
});