klei/gulp-inject

Injecting into one stream affects another?

leewz opened this issue · 1 comments

leewz commented

I have code that looks like this:

gulp.task('all', function() {
    var pin = getInputDir();
    var pout = getOutputDirs();

    var files = getInputStreams(pin);
    var outStreams = [];

    var devFiles = prepareDev(files, pin);
    var deployFiles = prepareDeploy(devFiles, pin);
    const ALL_BASES = [pin, pout.dev, pout.deploy];
    outStreams.push(saveBuild(devFiles, pout.dev, {
        ignorePath: ALL_BASES,
    }));
    outStreams.push(saveBuild(deployFiles, pout.deploy, {
        ignorePath: ALL_BASES,
    }));

    return merge(outStreams);
});

where:

  • files holds streams for resource files (e.g. CSS/JS/HTML).
  • prepareDev does nothing at the moment.
  • prepareDeploy concatenates the JS/CSS files (making a new files object).
  • saveBuild uses inject to inject CSS/JS into the HTML files, and saves them.

devFiles has 20+ total CSS/JS files, while deployFiles has two: main.js and main.css. But the code above injects 20+ files into both dev and deploy HTML files.

I get the proper behavior for saving deploy if I don't save dev:

//outStreams.push(saveBuild(devFiles, pout.dev, {
    //ignorePath: ALL_BASES,
//}));

But that means different branches from the same stream are affecting each other. As far as I know, that shouldn't happen.


Here's a sample of prepareDeploy:

var js = files.js.pipe(concat('js/main.js'))
return Object.assign({}, files, {js:js});

And here's saveBuild:

function saveBuild(files, outDir, options) {
    var js = files.js;
    var css = files.css;
    var htmlResources = merge(js, css)
        .pipe(fixPaths()) //ensure absolute paths
    var html = files.html
        .pipe(inject(htmlResources))

    return merge(js, css, html)
        .pipe(gulp.dest(outDir));
}
leewz commented

I was modifying the original file in my fixPaths() code. Now that my code clones, inject properly injects into the deploy version.

I still don't understand why it's injecting files from one fileset into the other.