7

我想知道是否有任何方法可以将这两个单独的任务合并为一个。

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))
    ;
});
4

1 回答 1

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));
}

通过这样做,您将合并两个流,并且您的输出文件getTemplateStream将被发送到管道中。

于 2015-02-01T16:01:00.780 回答