gpt4 book ai didi

javascript - 如何使用 gulp 正确清理项目?

转载 作者:IT王子 更新时间:2023-10-29 03:06:08 26 4
gpt4 key购买 nike

关于 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 任务。这也依赖于 (gulp.task('images', **['clean']**, function() {) clean 任务,所以这个也运行。但是我的脚本文件丢失了,因为 scripts 任务没有再次启动并且 clean 任务删除了所有文件。

我怎样才能在第一次启动时运行清理任务并保留依赖项?

最佳答案

您可以让单独的任务由 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']);

关于javascript - 如何使用 gulp 正确清理项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24396659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com