dob*_*ler 7 javascript node.js gulp
我需要通过顺序处理不同的源来组合gulp任务,因为它们之间存在依赖关系.
根据文档,这应该在我的合并流中完成,但我看不出如何强制执行订购和序列化它们.
在Gulp 3中对此进行建模的正确方法是什么?
我通常使用函数作为单个构建步骤的容器,然后从构建和监视任务中调用它们:
function buildModule(module) {
var streams = [];
// step one
streams.push(
gulp.src(path.join('./modules', module, '*.js'))
// ... series of chained calls
);
// step two
streams.push(
gulp.src([TMP, ...])
// generate target also using some of the files from step one
);
return eventStream.merge(streams);
}
gulp.task('build:A', [], function () {
return buildModule('A');
});
gulp.task('watch:buildModule', [], function () {
gulp.watch('./modules/**/*.js', function (event) {
if (event.type === 'changed') {
return buildModule(path.basename(path.dirname(event.path)));
}
});
});
gulp.task('default', ['watch:buildModule'], function () {});
Run Code Online (Sandbox Code Playgroud)
Dan*_*nte 16
基本上有三种方法可以做到这一点.
Gulp允许开发人员通过将一组任务名称作为第二个参数来定义依赖任务:
gulp.task('concat', function () {
// ...
});
gulp.task('uglify', ['concat'], function () {
// ...
});
gulp.task('test', ['uglify'], function () {
// ...
});
// Whenever you pass an array of tasks each of them will run in parallel.
// In this case, however, they will run sequentially because they depend on each other
gulp.task('build', ['concat', 'uglify', 'test']);
Run Code Online (Sandbox Code Playgroud)
您还可以使用run-sequence按顺序运行任务数组:
var runSequence = require('run-sequence');
gulp.task('build', function (cb) {
runSequence('concat', 'uglify', 'test', cb);
});
Run Code Online (Sandbox Code Playgroud)
虽然Lazypipe是一个用于创建可重用管道的库,但您可以以某种方式使用它来创建顺序任务.例如:
var preBuildPipe = lazypipe().pipe(jshint);
var buildPipe = lazypipe().pipe(concat).pipe(uglify);
var postBuildPipe = lazypipe().pipe(karma);
gulp.task('default', function () {
return gulp.src('**/*.js')
.pipe(preBuildPipe)
.pipe(buildPipe)
.pipe(postBuildPipe)
.pipe(gulp.dest('dist'));
});
Run Code Online (Sandbox Code Playgroud)