【问题标题】:Combining multiple src streams in gulp?在 gulp 中组合多个 src 流?
【发布时间】:2015-02-01 04:13:37
【问题描述】:

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

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

【问题讨论】:

    标签: angularjs gulp gulp-concat


    【解决方案1】:

    确实应该合并它们,因为 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 的输出文件将通过管道发送。

    【讨论】:

    • 太棒了!太感谢了!这正是我一直在寻找的。我很高兴你没有讨论我是否应该做我正在做的事情;我只是想知道 如何 在需要时应用该模式。再次感谢!
    • +1 我通常不会评论对我有用的解决方案,但在过去的六个小时里,我一直在谷歌搜索和阅读文档以弄清楚如何将多个 gulp.src 调用合并在一起,这就是我发现唯一有用的东西。出色的答案!
    • 同上。事件流合并令人困惑,而合并流包做了一些微妙但明显不同的事情。感谢您的记录!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多