Search code examples
gulp

gulp task throwing error on second time run


I have two folders both of which contain some html template files. I need to minify these files to separate folders.

folder structure

|src
|--clientTemplates
|----abc.html
|----xyz.html
|--serverTemplates
|----abc.html
|----xyz.html

required destination folder

|dist
|--client
|----abc.html
|----xyz.html
|--server
|----abc.html
|----xyz.html

following is my gulpfile where I have my tasks defined for the

var gulp = require('gulp');
var htmlmin = require('gulp-htmlmin');
var replace = require('gulp-replace');
var del = require('del');
var minOptions = {
    collapseWhitespace: true,
    minifyJS: { output: { quote_style: 1 } },
    minifyCSS: true
};    

gulp.task('clean', function(done) {
    del(['dist'], done());
});

gulp.task('minify:serverTemplates', function() {
    return gulp
        .src('src/serverTemplates/*.html')
        .pipe(htmlmin(minOptions))
        .pipe(replace('\\', '\\\\'))
        .pipe(replace('"', '\\"'))
        .pipe(gulp.dest('dist/server'));
});

gulp.task('minify:clientTemplates', function() {
    return gulp
        .src('src/clientTemplates/*.html')
        .pipe(htmlmin(minOptions))
        .pipe(gulp.dest('dist/client'));
});

gulp.task(
    'default',
    gulp.series('clean', 'minify:serverTemplates', 'minify:clientTemplates', function inSeries(done) {
        done();
    })
);

when I run the gulp command it works fine for the first time, but throws errors on alternate runs. running gulp command first time running gulp command first time- output

running gulp command second time running gulp command second time- output

can't figure out what exactly is wrong there. Also is there a way to run the two minification task parallel once the clean task has finished?

thanks for the help.


Solution

  • The callback you pass to del is wrong. Just return the promise:

    gulp.task('clean', function() {
      return del(['dist']);
    });
    

    As for running the minification tasks in parallel, use gulp.parallel:

    gulp.task(
      'default',
      gulp.series(
        'clean',
        gulp.parallel('minify:serverTemplates', 'minify:clientTemplates')
      )
    );