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

时间:2014-07-09 18:58:30

标签: javascript gulp

使用 gulp.js 我有三个任务(uglify,buildHTML,rmRevManifest)我想作为父版本任务的一部分运行。我所使用的代码,除了它并行执行任务(即不保留订单)。如何阻止任务,直到上一次完成后才运行下一步?

即。现在执行顺序又回来了:

[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.

顺序很重要,首先应运行 uglify ,然后运行 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\'.'));
});

3 个答案:

答案 0 :(得分:9)

在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']);

使用 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);
});

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

答案 1 :(得分:6)

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

答案很简单:有一个npm模块可以完全满足您的需求run sequence

答案 2 :(得分:3)

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