Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我需要将一个参数从另一个任务传递给一个gulp任务,或者用runSequence调用的函数替换一个任务_Javascript_Node.js_Gulp - Fatal编程技术网

Javascript 我需要将一个参数从另一个任务传递给一个gulp任务,或者用runSequence调用的函数替换一个任务

Javascript 我需要将一个参数从另一个任务传递给一个gulp任务,或者用runSequence调用的函数替换一个任务,javascript,node.js,gulp,Javascript,Node.js,Gulp,我有一个gulp文件,用于创建多种考试的Html和Js,比如Ex1、Ex2等 下面是我用来为Ex1创建这些的任务。它硬编码了一个对makeEx1Html任务的调用,另外三个任务,然后是一个函数调用,我可以在其中传递一个参数: gulp.task('make_prod_ex1', function () { runSequence( 'makeEx1Html', 'makeTemplate', 'rename_bundle_css',

我有一个gulp文件,用于创建多种考试的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');
        });
});
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('./'));
    }
}
以下是为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('./'));

});
下面是我可以传递参数的函数:

gulp.task('make_prod_ex1', function () {
    runSequence(
        'makeEx1Html',
        'makeTemplate',
        'rename_bundle_css',
        'rename_bundle_js',
        function () {
            make_prod_index('ex1');
        });
});
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”和“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);
}
在您的gulp任务中,您可以使用上面的
makeExHtml()
函数并传递一个回调,该回调将执行剩余的
runSequence()