到目前为止,我有以下代码:
gulp.task('make_prod_aa', function () {
makeAppHtml(config.srcAalHtml, function () {
runSequence(
'makeTemplate',
'make_css_bundle',
'rename_css_bundle',
'make_js_bundle',
'rename_js_bundle',
function () {
makeAppIndex('index-aa.html');
});
});
});
它允许我将参数传递到两个函数中,并按顺序运行函数和任务。现在,我想将参数传递到第三个任务中。我希望能够将config.aaTemplates
参数传递给任务makeTemplate,它在这里:
gulp.task('makeTemplate', function () {
return gulp.src(config.aaTemplates)
.pipe(print(function (file) {
return "Added file " + file + " to template";
}))
.pipe(minifyHTML({ collapseWhitespace: true }))
.pipe(templateCache({ standalone: true, root: '/app' }))
.pipe(gulp.dest(config.destPartials))
.pipe(gzip(gzip_options))
.pipe(gulp.dest(config.destPartials));
});
如果能给我什么建议,我将不胜感激。
我认为除了使用JS闭包让所有在序列中运行的任务共享相同的配置之外,没有其他选择了。证明
例如:
var config = require('path/to/config');
function makAppAppHtml () {
runSequence(
'makeTemplate',
'make_css_bundle',
'rename_css_bundle',
'make_js_bundle',
'rename_js_bundle',
function () {
makeAppIndex('index-aa.html');
});
}
function makeTemplate () {
return gulp.src(config.aaTemplates)
.pipe(print(function (file) {
return "Added file " + file + " to template";
}))
.pipe(minifyHTML({ collapseWhitespace: true }))
.pipe(templateCache({ standalone: true, root: '/app' }))
.pipe(gulp.dest(config.destPartials))
.pipe(gzip(gzip_options))
.pipe(gulp.dest(config.destPartials));
});
}
gulp.task('make_prod_aa', makAppAppHtml);
gulp.task('makeTemplate', makeTemplate);
makeAppHtml
和makeTemplate
都可以访问config对象,因为它是在外部范围中定义的,同样的技巧也可以应用于函数范围,对于更详细的例子,我推荐JavaScriptGarden文章。