【问题标题】:How to clean a project correctly with gulp?如何使用 gulp 正确清理项目?
【发布时间】:2014-06-24 21:29:47
【问题描述】:

gulp page 上有如下示例:

gulp.task('clean', function(cb) {
  // You can use multiple globbing patterns as you would with `gulp.src`
  del(['build'], cb);
});

gulp.task('scripts', ['clean'], function() {
  // Minify and copy all JavaScript (except vendor scripts)
  return gulp.src(paths.scripts)
    .pipe(coffee())
    .pipe(uglify())
    .pipe(concat('all.min.js'))
    .pipe(gulp.dest('build/js'));
});

// Copy all static images
gulp.task('images', ['clean'], function() {
 return gulp.src(paths.images)
    // Pass in options to the task
    .pipe(imagemin({optimizationLevel: 5}))
    .pipe(gulp.dest('build/img'));
});

// the task when a file changes
gulp.task('watch', function() {
  gulp.watch(paths.scripts, ['scripts']);
  gulp.watch(paths.images, ['images']);
});

// The default task (called when you run `gulp` from cli)
gulp.task('default', ['watch', 'scripts', 'images']);

这很好用。但是watch 任务存在一个大问题。如果我更改图像,监视任务会检测到它并运行images 任务。这对clean 任务也有依赖关系(gulp.task('images', **['clean']**, function() {),所以它也会运行。但是我的脚本文件丢失了,因为scripts 任务没有再次启动,clean 任务删除了所有文件。

如何在第一次启动时运行 clean 任务并保留依赖项?

【问题讨论】:

  • 出于这个原因,我们最终在 gulp 之外执行了 clean。我认为的串行关系和实际的依赖之间没有区别。
  • @Mathletics 你用什么在 gulp 之外清理它?
  • @AlanH rm -rf 无论你的输出目录是什么

标签: javascript gulp


【解决方案1】:

您可以让watch触发单独的任务:

gulp.task('clean', function(cb) {
  // You can use multiple globbing patterns as you would with `gulp.src`
  del(['build'], cb);
});

var scripts = function() {
  // Minify and copy all JavaScript (except vendor scripts)
  return gulp.src(paths.scripts)
    .pipe(coffee())
    .pipe(uglify())
    .pipe(concat('all.min.js'))
    .pipe(gulp.dest('build/js'));
};
gulp.task('scripts', ['clean'], scripts);
gulp.task('scripts-watch', scripts);

// Copy all static images
var images = function() {
 return gulp.src(paths.images)
    // Pass in options to the task
    .pipe(imagemin({optimizationLevel: 5}))
    .pipe(gulp.dest('build/img'));
};
gulp.task('images', ['clean'], images);
gulp.task('images-watch', images);

// the task when a file changes
gulp.task('watch', function() {
  gulp.watch(paths.scripts, ['scripts-watch']);
  gulp.watch(paths.images, ['images-watch']);
});

// The default task (called when you run `gulp` from cli)
gulp.task('default', ['watch', 'scripts', 'images']);

【讨论】:

  • 有效,但有点困难。幸运的是,gulp 团队正在开发一个新的task solution
  • 我在清理东西时遇到了麻烦,也许有单独的清理任务比分解监视任务更好。
  • @blues_driven 尝试returning del-call 而不是将cb-callback 传递给它...就像这里:github.com/gulpjs/gulp/blob/…
  • 使用del.sync 否则你会在一些文件仍然被删除的同时添加文件。
  • @yckart 是对的 - “del” 2.0 API 已更改为使用 Promise。
【解决方案2】:

使用 del.sync。它完成了del,然后从任务中返回

gulp.task('clean', function () {
    return $.del.sync([path.join(conf.paths.dist, '/**/*')]);
});

并确保 clean 是任务列表中的第一个任务。例如,

gulp.task('build', ['clean', 'inject', 'partials'], function ()  {
    //....
}

@Ben 我喜欢你分离 clean:css clean:js 任务的方式。这是一个很好的提示

【讨论】:

    【解决方案3】:

    直接使用模块即可,因为 gulp.src 成本高。确保使用 sync() 方法,否则可能会发生冲突。

    gulp.task('clean', function () {
        del.sync(['./build/**']);
    });
    

    如果您想使用 gulp 管道,另一种方法是: https://github.com/gulpjs/gulp/blob/master/docs/recipes/delete-files-folder.md

    var del = require('del');
    var vinylPaths = require('vinyl-paths');
    
    gulp.task('clean:tmp', function () {
      return gulp.src('tmp/*')
        .pipe(vinylPaths(del))
        .pipe(stripDebug())
        .pipe(gulp.dest('dist'));
    });
    

    【讨论】:

      【解决方案4】:

      我遇到了回调方法的问题。回调在 del 操作完成之前运行,导致错误。修复将 del 结果返回给调用者,如下所示:

      // Clean
      gulp.task('clean', function() {
          return del(['public/css', 'public/js', 'public/templates'])
      });
      
      // Build task
      gulp.task('build', ['clean'], function() {
          console.log('building.....');
          gulp.start('styles', 'scripts', 'templates');
      });
      

      【讨论】:

        【解决方案5】:

        我发现了这个问题。我只是在我的任务中清理我的目标文件。像这样:

        gulp.task('img', function() {
            //clean target before all task
            del(['build/img/*']);
            gulp.src(paths.images)
                .pipe(gulp.dest('build/img'));
        });
        

        只需一行即可解决。希望对您有所帮助。

        【讨论】:

        【解决方案6】:

        对于默认任务,我建议使用“run-sequence”以指定顺序运行一系列 gulp 任务, 在这里查看:https://www.npmjs.com/package/run-sequence
        然后我会像往常一样使用“监视”任务。

        对于图像任务,我会添加缓存,因为这是一项繁重的任务,请在此处查看:https://www.npmjs.com/package/gulp-cache

        将它们组合在一起将如下所示:

        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);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-04-03
          • 1970-01-01
          • 1970-01-01
          • 2010-12-10
          • 2023-03-18
          • 1970-01-01
          • 2018-09-02
          • 2021-04-27
          相关资源
          最近更新 更多