Gulp任务不等待另一个,而不是连续运行

时间:2015-03-10 06:52:06

标签: node.js gulp

我有以下gulp文件:

'use strict';

var gulp = require('gulp');
var jshint = require('gulp-jshint');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rev = require('gulp-rev');
var usemin = require('gulp-usemin');
var ngmin = require('gulp-ngmin');
var path = require('path');
var rimraf = require('gulp-rimraf');


var bases = {
 app: 'app/',
 dist: 'dist/'
};

var paths = {
  scripts: ['scripts/**/*.js', '!scripts/libs/**/*.js'],
  styles: ['styles/**/*.css'],
  html: ['index.html', '404.html'],
  views: ['views/**/*.html']
};


gulp.task('copy', function() {
  return gulp.src(paths.views, {cwd: bases.app, read:false})
  .pipe(gulp.dest(bases.dist + '/views/'));
});

gulp.task('clean', function() {
  return gulp.src(bases.dist, { read: false }) // much faster
    .pipe(rimraf());
});


gulp.task('usemin', [], function () {
  return gulp.src('./*.html', {cwd: bases.app})
    .pipe(usemin({
      js: [/*uglify(), rev()*/]
    }))
    .pipe(gulp.dest(bases.dist));
});

// Rerun the task when a file changes
gulp.task('watch', function() {
  gulp.watch(path.join(bases.app, paths.scripts[0]), ['usemin']);
});

gulp.task('default', ['clean', 'usemin', 'copy']);

根据文档,在 clean 任务中返回像我一样的流, copy usemin 应该等待干净完成。

从控制台可以看出,它们不是

[23:33:22] Starting 'clean'...
[23:33:22] Starting 'usemin'...
[23:33:22] Starting 'copy'...
[23:33:22] Finished 'clean' after 82 ms
[23:33:22] Finished 'copy' after 9.92 ms
[23:33:22] Finished 'usemin' after 200 ms
[23:33:22] Starting 'default'...
[23:33:22] Finished 'default' after 9.76 μs

预期结果如下:

[23:33:22] Starting 'clean'...
[23:33:22] Finished 'clean' after 82 ms
[23:33:22] Starting 'usemin'...
[23:33:22] Starting 'copy'...
[23:33:22] Finished 'copy' after 9.92 ms
[23:33:22] Finished 'usemin' after 200 ms
[23:33:22] Starting 'default'...
[23:33:22] Finished 'default' after 9.76 μs

那么,我做错了什么?

1 个答案:

答案 0 :(得分:0)

由于zaynetro的暗示,我认为显而易见......

gulp.task('default', ['clean', 'usemin', 'copy'], function() {})的语法只是意味着,默认取决于 clean usemin copy ,不管他们的顺序如何。它们将同时处理。

如果您真的希望控制任务执行顺序,则必须将其分解为依赖于 clean 的子任务,就像我自己的情况一样:

'use strict';

var gulp = require('gulp');
var jshint = require('gulp-jshint');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rev = require('gulp-rev');
var usemin = require('gulp-usemin');
var ngmin = require('gulp-ngmin');
var path = require('path');
var rimraf = require('gulp-rimraf');


var bases = {
 app: 'app/',
 dist: 'dist/'
};

var paths = {
  scripts: ['scripts/**/*.js', '!scripts/libs/**/*.js'],
  styles: ['styles/**/*.css'],
  html: ['index.html', '404.html'],
  views: ['views/**/*.html']
};


gulp.task('copy', ['clean'], function() {
  return gulp.src(paths.views, {cwd: bases.app, read:false})
  .pipe(gulp.dest(bases.dist + '/views/'));
});

gulp.task('clean', function() {
  return gulp.src(bases.dist, { read: false }) // much faster
    .pipe(rimraf());
});


gulp.task('usemin', ['clean'], function () {
  return gulp.src('./*.html', {cwd: bases.app})
    .pipe(usemin({
      js: [/*uglify(), rev()*/]
    }))
    .pipe(gulp.dest(bases.dist));
});

// Rerun the task when a file changes
gulp.task('watch', function() {
  gulp.watch(path.join(bases.app, paths.scripts[0]), ['usemin']);
});

gulp.task('default', ['usemin', 'copy']);