Gulp:如何在下载所有lib之后连接bower组件?

时间:2015-06-18 18:41:09

标签: gulp bower

我正在使用" gulp-bower"从bower.json自动安装所有库,我也希望gulp在下载后缩小所有库。这是我的代码:

var gulp = require('gulp'); 
var sass = require('gulp-sass');
var concat = require('gulp-concat');
var bower = require('gulp-bower');
var mainBowerFiles = require('main-bower-files');

gulp.task('bower', function() {
    return bower()
        .pipe(gulp.dest("./bower_components"))
});

gulp.task('minifyBower', function() {
    return gulp.src(mainBowerFiles())
        .pipe(concat('lib.js'))
        .pipe(gulp.dest('dist'))
});

gulp.task('default', ['bower','minifyBower']);

如果我运行此操作,我会收到此错误。

Starting 'bower'...
[11:23:06] Using cwd:  /Users/yizhou/Documents/Yi
[11:23:06] Using bower dir:  ./bower_components
[11:23:06] Starting 'minifyBower'...
[11:23:06] 'minifyBower' errored after 1.53 ms
[11:23:06] Error: Bower components directory does not exist at /Users/yizhou/Documents/Yi/bower_components
    at Error (native)
    at module.exports (/Users/yizhou/Documents/Yi/node_modules/main-bower-files/lib/index.js:76:71)
    at Gulp.<anonymous> (/Users/yizhou/Documents/Yi/gulpfile.js:16:21)
    at module.exports (/Users/yizhou/Documents/Yi/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7)
    at Gulp.Orchestrator._runTask (/Users/yizhou/Documents/Yi/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
    at Gulp.Orchestrator._runStep (/Users/yizhou/Documents/Yi/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
    at Gulp.Orchestrator.start (/Users/yizhou/Documents/Yi/node_modules/gulp/node_modules/orchestrator/index.js:134:8)
    at /usr/local/lib/node_modules/gulp/bin/gulp.js:129:20
    at process._tickCallback (node.js:355:11)
    at Function.Module.runMain (module.js:503:11)
[11:23:06] bower cached git://github.com/jquery/jquery.git#2.1.4
[11:23:06] bower validate 2.1.4 against git://github.com/jquery/jquery.git#~2.1.4
[11:23:07] bower install jquery#2.1.4
[11:23:08] Finished 'bower' after 2 s

1 个答案:

答案 0 :(得分:1)

默认情况下,

gulp以异步方式运行每个任务,以提高性能。如果要按顺序运行,则需要显式声明依赖项:

gulp.task('minifyBower', ['bower'], function() { /* ... */ });

现在minifyBower一直运行,直到bower运行。如果您需要更复杂的内容,则应使用类似run-sequence的内容。