如何用gulp正确清理项目?

Jan*_*mes 43 javascript gulp

gulp页面上有以下示例:

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']);
Run Code Online (Sandbox Code Playgroud)

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

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

Ben*_*Ben 41

您可以通过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']);
Run Code Online (Sandbox Code Playgroud)

  • @blues_driven尝试`return`ing了`del`调用,而不是传递`cb`回调到它......至于这里:https://github.com/gulpjs/gulp/blob/a2c9e695ecf3600f21fa731e705fd1a0503632d9/docs/recipes/ delete-files-folder.md (3认同)

小智 19

使用del.sync.它完成del然后从任务返回

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

并确保清洁是任务列表中的第一项任务.例如,

gulp.task('build', ['clean', 'inject', 'partials'], function ()  {
    //....
}
Run Code Online (Sandbox Code Playgroud)

@Ben我喜欢你分离干净的方式:css clean:js tasks.这是一个很好的提示


mag*_*ter 5

只需直接使用该模块,因为gulp.src成本很高.确保使用sync()方法,否则可能会有冲突.

gulp.task('clean', function () {
    del.sync(['./build/**']);
});
Run Code Online (Sandbox Code Playgroud)

如果你想使用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'));
});
Run Code Online (Sandbox Code Playgroud)