javascript Gulp.js 以特定顺序阻塞运行任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24661724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:11:20  来源:igfitidea点击:

Gulp.js run tasks in specific order blocking

javascriptgulp

提问by Justin

Using gulp.jsI have three tasks (uglify, buildHTML, rmRevManifest)I'd like to run as part of a parent build task. The code I have works, excepts it runs the tasks in parallel (i.e. order is not preserved). How can I have the tasks block and not run the next until the previous finishes?

使用gulp.js我有三个任务(uglify、buildHTML、rmRevManifest)我想作为父构建任务的一部分运行。我的代码有效,除了它并行运行任务(即不保留顺序)。我怎样才能阻止任务并且在上一个完成之前不运行下一个?

I.E. right now the execution order comes back as:

IE 现在执行顺序返回为:

[11:50:17] gulp-notify: [Gulp notification] Deleted 'rev-manifest.json'.
[11:50:17] gulp-notify: [Gulp notification] Created 'build/index.html'.
[11:50:17] gulp-notify: [Gulp notification] Uglified JavaScript.

The order matters, and uglifyshould run first, then buildHTML, and finally rmRevManifest.

订单事宜,以及丑化应该首先运行,然后buildHTML,最后rmRevManifest

gulp.task('build', ['uglify', 'buildHTML', 'rmRevManifest'], function(cb) {
});

gulp.task('uglify', function(cb) {
    gulp.src('client/js/source/**/*.js')
        .pipe(concat('app'))
        .pipe(ngmin())
        .pipe(uglify())
        .pipe(rev())
        .pipe(rename({
            extname: ".min.js"
        }))
        .pipe(gulp.dest('client/js'))
        .pipe(rev.manifest())
        .pipe(gulp.dest('client/js'))
        .pipe(notify('Uglified JavaScript.'));
});

gulp.task('buildHTML', function(cb) {
    gulp.src('client/views/index.html')
        .pipe(replace(/app\-[a-fA-F0-9]\.min\.js/, 'app-.min.js'))
        .pipe(gulp.dest('client/build'))
        .pipe(notify('Created \'build/index.html\'.'));
});

gulp.task('rmRevManifest', function(cb) {
    gulp.src('client/js/rev-manifest.json', { read: false })
        .pipe(rimraf())
        .pipe(notify('Deleted \'rev-manifest.json\'.'));
});

回答by Konstantin Tarkus

An example of setting up dependencies in Gulp 3.0. In this example 3 tasks depend on the 'clean' task which should be executed first:

在 Gulp 3.0 中设置依赖项的示例。在这个例子中,3 个任务依赖于应该首先执行的“clean”任务:

// Include Gulp
var gulp = require('gulp');
var task = {};

// Clean up
gulp.task('clean', function () { .. });

// HTML pages
gulp.task('pages', task.pages = function () { ... });
gulp.task('pages:clean', ['clean'], task.pages);

// CSS style sheets
gulp.task('styles', task.styles = function () { ... });
gulp.task('styles:clean', ['clean'], task.styles);

// JavaScript files
gulp.task('scripts', task.scripts = function () { ... });
gulp.task('scripts:clean', ['clean'], task.scripts);

// Bundling and optimization
gulp.task('build', ['pages:clean', 'styles:clean', 'scripts:clean']);

With run-sequenceit would be equal to:

对于运行序列,它将等于:

// Include Gulp & utilities
var gulp = require('gulp');
var runSequence = require('run-sequence');

// Clean up
gulp.task('clean', function () { .. });

// HTML pages
gulp.task('pages', function () { ... });

// CSS style sheets
gulp.task('styles', function () { ... });

// JavaScript files
gulp.task('scripts', function () { ... });

// Bundling and optimization
gulp.task('build', ['clean'], function (cb) {
    runSequence(['pages', 'styles', 'scripts'], cb);
});

P.S.: In the upcoming Gulp 4.0 the dependency system will be much better.

PS:在即将到来的 Gulp 4.0 中,依赖系统会好很多。

回答by Mercury

The real answer: you need to set up dependencies which require the other tasks to run first.

真正的答案:您需要设置需要其他任务首先运行的依赖项。

The easy answer: there's an npm module to do exactly what you need called run sequence.

简单的答案:有一个 npm 模块可以完全满足您的需要,称为run sequence

回答by Justin

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

gulp.task('build', function(cb) {
     runSequence('uglify',
                 'buildHTML',
                 'rmRevManifest',
                 cb);
});

    gulp.task('uglify', function() {
        return gulp.src('client/js/source/**/*.js')
               .pipe(concat('app'))
               .pipe(ngmin())
               .pipe(uglify())
               .pipe(rev())
               .pipe(rename({
                    extname: ".min.js"
                }))
               .pipe(gulp.dest('client/js'))
               .pipe(rev.manifest())
               .pipe(gulp.dest('client/js'));
    });

    gulp.task('buildHTML', function() {
        var revManifest = require('./client/js/rev-manifest.json');

        return gulp.src('client/views/index.html')
               .pipe(replace(/app\-[a-fA-F0-9]\.min\.js/, 'app-' + revManifest.app + '.min.js'))
               .pipe(gulp.dest('client/build'));
    });

    gulp.task('rmRevManifest', function() {
        return gulp.src('client/js/rev-manifest.json', { read: false })
               .pipe(rimraf());
    });