Gulp.js按特定顺序阻止运行任务

Jus*_*tin 8 javascript gulp

使用gulp.js我有三个任务(uglify,buildHTML,rmRevManifest)我想作为父构建任务的一部分运行.我工作的代码,除了它并行运行任务(即不保留顺序).如何阻止任务,直到上一次完成后才运行下一个任务?

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.
Run Code Online (Sandbox Code Playgroud)

订单事宜,以及丑化应该首先运行,然后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\'.'));
});
Run Code Online (Sandbox Code Playgroud)

Kon*_*kus 9

在Gulp 3.0中设置依赖关系的示例.在这个例子中,3个任务依赖于应该首先执行的"干净"任务:

// 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']);
Run Code Online (Sandbox Code Playgroud)

使用run-sequence,它将等于:

// 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);
});
Run Code Online (Sandbox Code Playgroud)

PS:在即将推出的Gulp 4.0中,依赖系统会好得多.


小智 6

真正的答案是:您需要设置依赖项,这些依赖项需要先运行其他任务.

简单的答案:有一个npm模块可以完全满足您的运行顺序.