因此,我有以下GulpJS任务,这些任务都与安装和复制bower文件有关:
gulp.task('bower-install', function() {
var command = 'bower install';
gutil.log(gutil.colors.cyan('running command:'), command);
return gulp.src('', {read: false})
.pipe(shell([
command
]));
});
gulp.task('bower-copy', function() {
return gulp.src(gulpConfig.bowerCopy.map(function(item) {
return './bower_components/' + item;
}), {base: './bower_components'})
.pipe(gulp.dest('./htdocs/components'));
});
gulp.task('bower-copy-clean', function() {
return gulp.src('./bower_components')
.pipe(clean());
});
gulp.task('bower-clean', function() {
return gulp.src('./htdocs/components')
.pipe(clean());
});
gulp.task('bower', 'Download and move bower packages', function(done) {
runSequence(
'bower-install',
'bower-clean',
'bower-copy',
'bower-copy-clean',
done
);
});我是这样做的,因为我需要这些任务一个接一个地运行。当我运行gulp bower时,所有东西都按预期工作,我想构造这段代码,以便唯一公开的任务是bower,因为所有的bower-*都没有意义自己运行。
是否有任何方法编写此代码,以便所有bower-*任务都一个接一个地运行,但只公开bower任务?
发布于 2014-07-13 00:07:01
因为gulpfile只是一个普通的节点应用程序,通常当我们陷入这样的困境时,问“如果我们没有吞咽怎么办?”这是我的解决方案:
var async = require('async');
var del = require('del');
var spawn = require('child_process').spawn;
function bowerInstall(cb) {
var command = 'bower install';
gutil.log(gutil.colors.cyan('running command:'), command);
var childProcess = spawn('bower', ['install'], {
cwd: process.cwd(),
stdio: 'inherit'
}).on('close', cb);
});
function bowerCopy(cb) {
gulp.src(gulpConfig.bowerCopy.map(function(item) {
return './bower_components/' + item;
}), {base: './bower_components'})
.pipe(gulp.dest('./htdocs/components'))
.on('end', cb);
});
function bowerCopyClean(cb) {
del('./bower_components', cb);
});
function bowerClean() {
del('./htdocs/components', cb);
});
gulp.task('bower', 'Download and move bower packages', function(done) {
async.series([
bowerInstall,
bowerClean,
bowerCopy,
bowerCopyClean
], done);
});请注意,我还通过不将整个bower_components和htdocs/components目录读入ram,从而大大加快了构建速度。
https://stackoverflow.com/questions/24717948
复制相似问题