简体   繁体   English

Gulp:如何按顺序编写任务?

[英]Gulp: how to compose tasks sequentially?

I would need to compose gulp tasks by sequentially processing different sources as there are dependencies between them. 我将需要通过顺序处理不同的源来组成gulp任务,因为它们之间存在依赖性。

Based on the documentation this should be done my merging streams but I see no way on how to enforce to order and serialize them. 根据文档,这应该在合并流中完成,但是我看不到如何强制执行命令和序列化它们。

What is the proper way to model this in Gulp 3 ? 在Gulp 3中对此建模的正确方法是什么?

I typically use functions as containers for the individual build steps and then invoke them from the build and watch tasks: 我通常将函数用作各个构建步骤的容器,然后从构建中调用它们并监视任务:

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 () {});

There are basically three ways to do that. 基本上有三种方法可以做到这一点。

1. Defining dependent tasks 1.定义相关任务

Gulp allows developers to define dependent tasks by passing an array of task names as second arguments: 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']);

2. Using run-sequence 2.使用运行顺序

You can also use run-sequence to run an array of tasks sequentially: 您还可以使用运行序列顺序运行一系列任务:

var runSequence = require('run-sequence');

gulp.task('build', function (cb) {
    runSequence('concat', 'uglify', 'test', cb);
});

3. Using lazypipe 3.使用懒人

Although Lazypipe is a library to create reusable pipelines, you can somehow use it to create sequential tasks. 尽管Lazypipe是用于创建可重用管道的库,但是您可以通过某种方式使用它来创建顺序任务。 For example: 例如:

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

This little module may help: stream-series . 这个小模块可能会有所帮助: stream-series

Just replace eventStream.merge(streams) with: 只需将eventStream.merge(streams)替换为:

var series = require('stream-series');
// ...
return series(streams);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM