在gulp中组合多个src流?

cor*_*ore 7 angularjs gulp gulp-concat

我想知道是否有办法将这两个单独的任务合二为一.

concat-js任务需要在运行之前存在生成的文件.该任务cache-angular-templates生成该文件.生成的文件需要包含在concat输出中.完成后concat-js,可以删除文件 - 不再需要它.

这似乎是我应该以某种方式能够管中使用的流cache-angular-tempaltes进流concat-js用途.

gulp.task('concat-js', ['cache-angular-templates'], function () {
    var concatOutputPath = path.dirname(paths.compiledScriptsFile),
        concatOutputFileName = path.basename(paths.compiledScriptsFile),
        jsFiles = [].concat(
            paths.libScripts,
            paths.appScripts,
            paths.templateScriptFile,
            notpath(paths.compiledScriptsFile),
            notpath(paths.specMockScripts),
            notpath(paths.specScripts)
        );

    return gulp
        .src(jsFiles)
        .pipe(buildTools.concat(concatOutputFileName))
        .pipe(gulp.dest(concatOutputPath))
        .on('end', function () {
            del(paths.templateScriptFile);
        })
    ;
});

gulp.task('cache-angular-templates', function () {
    var cacheOutputPath = path.dirname(paths.templateScriptFile),
        cacheOutputFileName = path.basename(paths.templateScriptFile);

    var options = {
        root: '/' + cacheOutputPath,
        standalone: true,
        filename: cacheOutputFileName
    };

    return gulp
        .src(paths.templates)
        .pipe(buildTools.angularTemplatecache(options))
        .pipe(gulp.dest(cacheOutputPath))
    ;
});
Run Code Online (Sandbox Code Playgroud)

Cai*_*nha 14

确实你应该合并它们,因为Gulp的一个想法是消除中间临时文件.

实现它的方法之一是:

  1. 转换cache-angular-templates为返回模板流的函数,让我们调用它getTemplateStream;
  2. .pipe(gulp.dest(cacheOutputPath))从中取出;
  3. 用于event-stream在将流连接到主任务之前合并流.你的主要任务将是这样的:
var es = require('event-stream');

gulp.task('concat-js', function () {
    var concatOutputPath = path.dirname(paths.compiledScriptsFile),
        concatOutputFileName = path.basename(paths.compiledScriptsFile),
        jsFiles = [].concat(
            paths.libScripts,
            paths.appScripts,
            notpath(paths.compiledScriptsFile),
            notpath(paths.specMockScripts),
            notpath(paths.specScripts)
        );

    return es.merge(gulp.src(jsFiles), getTemplateStream())
        .pipe(buildTools.concat(concatOutputFileName))
        .pipe(gulp.dest(concatOutputPath));
});

function getTemplateStream() {
    var options = {
        root: '/' + cacheOutputPath,
        standalone: true,
        filename: cacheOutputFileName
    };

    return gulp
        .src(paths.templates)
        .pipe(buildTools.angularTemplatecache(options));
}
Run Code Online (Sandbox Code Playgroud)

通过执行此操作,您将合并两个流,并将您的输出文件getTemplateStream发送到管道.

  • 真棒!非常感谢!这正是我所寻找的.我很高兴你没有考虑我是否*应该做我正在做的事情; 我只想知道*如何*在需要时应用模式.再次感谢! (3认同)