2

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

2 Answers 2

8

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'.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks guys. You were both leading to the same thing. Awesome. I was thinking all along that 'clean' would run each time when either css or vendor was called. I learned something today.
1

The Problem ist when you call the default task, gulp randomly chooses the order of the task:

gulp.task('default', ['clean', 'move', 'scripts', 'css']);

To solve this problem each task should have dependencies. For example the move task should be performed after the clean task. So the move taks should look like this:

gulp.task('move', ['clean'], function () { //your code }

for more explanation: https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulptaskname-deps-fn

sorry for my bad english :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.