如何用gulp正确清理项目
How to clean a project correctly with 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']);
这工作得很好。但是watch
任务有一个大问题。如果我更改了映像,watch任务会检测到它并运行images
任务。它在clean
任务上也有一个依赖项(gulp.task('images', **['clean']**, function() {
),所以它也可以运行。但是我的脚本文件丢失了,因为scripts
任务没有再次启动,clean
任务删除了所有文件。
如何在第一次启动时运行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']);
使用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任务分开的方式。这是一个很好的提示
直接使用该模块作为gulp。SRC是昂贵的。确保使用sync()方法,否则可能会发生冲突。
gulp.task('clean', function () {
del.sync(['./build/**']);
});
另一种方法,如果你想使用吞咽管道: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'));
});
我有一个回调方法的问题。回调在删除操作完成之前正在运行,从而导致错误。修复方法是将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');
});
我发现这个问题。我只是清理我的任务中的目标文件。这样的:
gulp.task('img', function() {
//clean target before all task
del(['build/img/*']);
gulp.src(paths.images)
.pipe(gulp.dest('build/img'));
});
只用一行来解它。
对于默认任务,我建议使用'run-sequence'来按照指定的顺序运行一系列gulp任务。点击这里查看:https://www.npmjs.com/package/run-sequence
那么我会像往常一样使用"watch"任务。
对于图像任务,我将添加缓存,因为它是一个繁重的任务,查看这里: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);
- 正在将数据主题添加到所有项目
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 如何检查管道中未定义的项目
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 学生搜索项目jquery/javascript
- 如何用更合适的内容替换document.write
- 如何获取不属于我项目的上一页的URL
- Dojo:访问dijit.form.Select中单击的项目
- 角度的项目列表 ng 重复,ng 单击显示全宽描述
- 我需要为我的朋友在这个项目上提供帮助
- 如何从 HTML 查询中删除项目
- 在Meteor项目中安装并包含npm模块后出错
- 当在Ember中点击一个项目时,我如何将一个活动类添加到项目列表中
- 将单击事件添加到附加的项目中
- 通过另一个php应用程序将我的项目推送到Github存储库中
- 我应该如何用外部脚本构建Meteor项目
- 如何用gulp正确清理项目
- 如何用jQuery包装一些项目
- 如何用tinysortjs对项目进行排序
- 同一类的多个项目,如何用jQuery区分它们