简体   繁体   English

流/注入问题

[英]Streams/gulp-inject issue

I'm trying to call gulp-inject with dynamic targets. 我正在尝试使用动态目标调用gulp-inject If an HTML file contains multiple targets each one overwrites the last, so I tried to move them out into another function. 如果一个HTML文件包含多个目标,则每个目标都会覆盖最后一个目标,因此我尝试将它们移到另一个函数中。 Problem is, now gulp-inject doesn't have access to the target (determined by logging where it gets to). 问题是,现在gulp-inject无法访问目标(通过记录到达目标的位置来确定)。

Code below should serve as an example (this is simplified but should show what's happening): 下面的代码应作为示例(已简化,但应显示正在发生的事情):

HTML: HTML:

<body>
  <!-- bundle1:js -->
  <!-- endinject -->

  <!-- bundle2:js -->
  <!-- endinject -->
</body>

Gulpfile.js: Gulpfile.js:

var gulp = require('gulp');
var inject = require('gulp-inject');
var through2 = require('through2');
var bundles = require('./bundles');

var localInject = function () {
    return through2.obj(function (file, enc, cb) {
        if (file.isNull()) {
            return cb(null, file);
        }

        if (file.isStream()) {
            return cb(new gutil.PluginError('gulp-inject', 'Streaming not supported'));
        }

        var scriptBundles = Object.keys(bundles.scripts);
        var referencedScriptBundles = [];

        scriptBundles.forEach(function (bundle) {
            var expression = "(" + bundle + ":\w*)";
            var regex = new RegExp(expression, "ig");
            if (regex.test(file.contents.toString('utf-8'))) {
                //console.log('matched', bundle, 'in', file.path);
                referencedScriptBundles.push(bundle);
            }
        });

        if (referencedScriptBundles.length) {
            referencedScriptBundles.forEach(function(bundle) {
                inject(gulp.src(bundles.scripts[bundle], {
                    read: false
                }), {
                    name: bundle
                });
            });
            cb(null, file);
        } else {
            cb(null, file);
        }
    });
};

gulp.task('inject:dev', function () {
    return gulp.src('./Views/**/*.cshtml', { base: './' })
        .pipe(debug())
        .pipe(localInject())
        .pipe(gulp.dest('.'));
});

Bundles.js: Bundles.js:

module.exports = {
  scripts: {
    bundle1: ['blah/**/*.js'],
    bundle2: ['otherblah/**/*.js']
  }
};

Probably a lack of knowledge on my part of streams, but everything works fine except inside gulp-inject , where it doesn't appear to be getting the file chunks. 我的流媒体部分可能缺乏知识,但是除了gulp-inject内部似乎没有获取文件块之外,其他所有东西都可以正常工作。 The plugin finds the right bundles in my views, and scripts from my bundle file to inject, just doesn't seem to have the reference to the target file. 该插件在我的视图中找到了正确的包,并且从我的包文件中注入了要注入的脚本,只是似乎没有对目标文件的引用。

I'm sure I already tried this before posting, but I fixed it by piping the current stream to inject, and using async.seriesEach. 我敢肯定我已经在发布之前尝试过了,但是我通过管道注入当前流并使用async.seriesEach修复了它。

this.pipe(inject(...));

Always the simple things! 总是简单的事情!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM