How to clean a project correctly with gulp?

后端 未结 6 1693
悲哀的现实
悲哀的现实 2020-12-25 10:45

On the gulp page there is the following example:

gulp.task(\'clean\', function(cb) {
  // You can use multiple globbing patterns as you would with `gulp.src`         


        
6条回答
  •  暖寄归人
    2020-12-25 10:52

    for the default task I would suggest using 'run-sequence' to run a sequence of gulp tasks in a specified order, check it out here: https://www.npmjs.com/package/run-sequence
    then I would use the 'watch' task as I normally do.

    for the images task I would add caching since it's an heavy task, check it out here: https://www.npmjs.com/package/gulp-cache

    combining it all together will look something like the following:

    var gulp = require('gulp');
    var runSequence = require('run-sequence');
    var del = require('del');
    var cache = require('gulp-cache');
    
    // Clean build folder function:
    function cleanBuildFn() {
        return del.sync(paths.build);
    }
    
    // Build function:
    function buildFn(cb) {
        runSequence(
            'clean:build', // run synchronously first
            ['scripts, 'images'], // then run rest asynchronously
            'watch',
            cb
        );
    }
    
    // Scripts function:
    function scriptsFn() {
      return gulp.src(paths.scripts)
        .pipe(coffee())
        .pipe(uglify())
        .pipe(concat('all.min.js'))
        .pipe(gulp.dest('build/js'));
    }
    
    // Images function with caching added:
    function imagesFn() {
        gulp.src(paths.source + '/images/**/*.+(png|jpg|gif|svg)')
        .pipe(cache(imagemin({optimizationLevel: 5})))
        .pipe(gulp.dest(paths.build + '/images'));
    }
    
    // Clean tasks:
    gulp.task('clean:build', cleanBuildFn);
    
    // Scripts task:
    gulp.task('scripts', scriptsFn);
    
    // Images task:
    gulp.task('images', imagesFn);
    
    // Watch for changes on files:
    gulp.task('watch', function() {
        gulp.watch(paths.source + '/images/**/*.+(png|jpg|gif|svg)', ['images']);
        gulp.watch(paths.source + '/scripts/**/*.js', ['scripts']);
    });
    
    // Default task:
    gulp.task('default', buildFn);

提交回复
热议问题