我需要将一个参数从另一个任务传递给gullow任务,或者用runSequence调用的函数替换一个任务
I need to pass a parameter to a gulp task from another task or replace a task with a function called by runSequence
我有一个gump文件,用于创建多种检查的Html和Js,Ex1、Ex2等
这是我用来为Ex1创建这些的任务。它已经硬编码了对makeEx1Html任务和其他三个任务的调用,然后是一个函数调用,我可以在其中传递一个参数:
gulp.task('make_prod_ex1', function () {
runSequence(
'makeEx1Html',
'makeTemplate',
'rename_bundle_css',
'rename_bundle_js',
function () {
make_prod_index('ex1');
});
});
以下是Ex1:的硬编码任务
gulp.task('makeEx1Html', function () {
return gulp.src(config.srcEx1Html, { base: process.cwd() })
.pipe(print(function (file) {
return "Found file " + file;
}))
.pipe(rename({ basename: 'base' }))
.pipe(gulp.dest('./'));
});
这是我可以传递参数的函数:
function make_prod_index(name) {
return function () {
gulp.src('index.html')
.pipe(htmlreplace({
'css': 'content/bundles/css.min.css',
'js': 'content/bundles/js.min.js'
}))
.pipe(eol())
.pipe(lec({ eolc: 'CRLF' }))
.pipe(replace('content/bundles/css.min.css', 'content/bundles/css-' + md5File('content/bundles/css.min.css') + '.min.css.gz'))
.pipe(replace('content/bundles/js.min.js', 'content/bundles/js-' + md5File('content/bundles/js.min.js') + '.min.js.gz'))
.pipe(rename('index-' + name + '.html'))
.pipe(gulp.dest('./'));
}
}
我希望避免像"makeEx1Html"answers"makeEx2Html"等特定任务,但我不确定如何做到这一点。
请注意,所有这些任务都需要按顺序运行,这就是我使用runSequence的原因。
如果有任何建议,我将不胜感激。理想情况下,我希望使Html成为一个可以向其传递参数的函数,但我不确定如何将其适应我的需求。
理想情况下,我希望使Html成为一个函数,我可以将参数传递给
您可以这样做,除了您不仅将参数传递给函数,还将在函数完成时调用回调cb
:
function makeExHtml(files, cb) {
return gulp.src(files, { base: process.cwd() })
.pipe(print(function (file) {
return "Found file " + file;
}))
.pipe(rename({ basename: 'base' }))
.pipe(gulp.dest('./'))
.on('end', cb);
}
在您的gump任务中,您可以使用上面的makeExHtml()
函数并传递一个回调,该回调将执行您的runSequence()
:的其余部分
gulp.task('make_prod_ex1', function () {
makeExHtml(config.srcEx1Html, function() {
runSequence(
'makeTemplate',
'rename_bundle_css',
'rename_bundle_js',
function () {
make_prod_index('ex1');
});
});
});
相关文章:
- 如何在另一个任务中正确使用延迟的gump任务返回的值
- 从另一个grunt文件加载grunt任务
- 我需要将一个参数从另一个任务传递给gullow任务,或者用runSequence调用的函数替换一个任务
- 从gullow任务运行一个npm脚本
- 循环一个单调乏味的任务
- 一个咕哝的任务能运行其他咕哝的任务吗
- 我如何在谷歌Chrome上打开一个新的弹出窗口,它可以在不经过Windows'任务栏
- 如何让一个按钮根据点击次数执行多个任务?(Javascript)
- 如何将外部 JSON 数据从上一个任务中生成的文件传递到任务
- 一个 HTML 表单,其中包含用于不同任务的多个提交按钮
- 节点群集未将任务分派给另一个可用的工作线程
- 一个 Gulp 任务中的多个同步流
- 我的 gulp usemin 任务缺少一个 js 文件
- 在另一个任务之前异步运行 grunt 任务
- 在Laravel Elixir中创建一个任务,以指定的顺序运行Gulp任务
- 从另一个任务访问Grunt当前任务
- 编写的第一个Grunt插件-即使Grunt.registerMultiTask()名称正确,也找不到任务
- 与另一个grunt任务共享来自自定义grunt任务的数据
- 如何从另一个gull文件.js导入所有任务
- 如何定义一个(内部)任务,而不把它列在“grunt——help”中