Search code examples
cssgulpgulp-concatgulp-lessgulp-default

Referencing two gulp tasks within a default


I have a gulp 'default' task where I want to clean a folder before gulp continues to build my minified CSS and JS. This 'clean' task needs to run only once per default task. But I am having issues trying to get default task to reference the real build tasks. So here is my gulpfile:

    var gulp = require('gulp');

    // including our plugins
    var clean = require('gulp-clean');
    var less = require('gulp-less');
    var util = require('gulp-util');
    var lsourcemaps = require('gulp-sourcemaps');
    var rename = require('gulp-rename');
    var filesize = require('gulp-filesize');
    var ugly = require('gulp-uglify');
    var path = require('path');
    var plumber = require('gulp-plumber');
    var minifyCSS = require('gulp-minify-css');
    var concat = require('gulp-concat');
    // DEFAULT TASK
    gulp.task('default', ['clean'], function() {
    .pipe(gulp.task('vendor'))
    .pipe(gulp.task('css'))
});
    // strips public folder for a build operation nice and clean ** obliterates! **
    gulp.task('clean', function() {
        return gulp.src('public/**', {read: false})
        .pipe(clean());
    });
    // javascript vendor builds
    gulp.task('vendor', function() {
        return gulp.src(['bower_comps/angular/angular.js', 'bower_comps/angular-bootstrap/ui-bootstrap.js', 'bower_comps/angular-bootstrap/ui-bootstrap-tpls.js'])
        //.pipe(filesize())
        .pipe(ugly())
        .pipe(concat('vendor.min.js'))
        .pipe(gulp.dest('public/js'))
    });
    // builds CSS
    gulp.task('css', function() {
        return gulp.src('bower_comps/bootstrap-less/less/bootstrap.less')
        .pipe(lsourcemaps.init())
        .pipe(plumber({
            errorHandler: function(err) {
                console.log(err);
                this.emit('end')
            }
        }))
        .pipe(less({
            paths: [path.join(__dirname, 'less', 'includes')]
        }))
        .pipe(minifyCSS())
        .pipe(rename('site.min.css'))
        .pipe(lsourcemaps.write('./maps'))
        .pipe(gulp.dest('public/css/'))
        .pipe(filesize())
    });

So how am I going about this wrong?? Each of the individual tasks will run on their own just fine "gulp css", "gulp vendor". Its just when I put them into a default task (master task) with a 'pre-requisite' task of my clean that I run into problems.

Tony


Solution

  • Try these for your tasks:

    gulp.task('clean', function() {
        // Insert cleaning code here
    });
    
    gulp.task('vendor', ['clean'], function() {
        // Insert your 'vendor' code here
    });
    
    gulp.task(‘css’, ['clean'], function() {
        // insert your 'css' code here
    });
    
    gulp.task('build', [‘vendor’, ‘css’]);
    
    gulp.task('default', ['build']);
    

    'vendor' and 'css' will run concurrently only after 'clean' has finished. 'clean' will only run once despite being a prerequisite for both 'vendor' and 'css'.