teambition/merge2

Streams participating in multiple merges get lost

alexanderby opened this issue · 6 comments

I have to build multiple CSS files and create bundles of all that files with multiple themes. Some file is included in multiple bundles. But after script is run that file is missing in concatenation results. When I replace merge2 with merge-stream it works, but concatenated files have random order.

const gulp = require('gulp');
const merge = require('merge2');
const less = require('gulp-less');
const concat = require('gulp-concat');
const rename = require('gulp-rename');

gulp.task('build', () => {
    const files = ['file1', 'file2', 'file3'];
    const themes = ['light', 'dark'];

    const getThemedStream = (theme, file) => {
        return gulp.src(`./${file}.less`)
            .pipe(less())
            .pipe(rename(`${file}.${theme}.css`))
            .pipe(gulp.dest('./result'));
    };

    const getConcatStream = (theme, streams) => {
        return merge(...streams)
            .pipe(concat(`merged.${theme}.css`))
            .pipe(gulp.dest('./result'));
    };

    const commonStream = gulp.src(`./common.less`)
        .pipe(less())
        .pipe(rename(`common.css`))
        .pipe(gulp.dest('./result'));

    return merge(commonStream, ...themes.map((theme) => {
        const streams = files.map((file) => getThemedStream(theme, file));
        streams.push(commonStream);
        return getConcatStream(theme, streams);
    }));
});
zensh commented

commonStream should not be multiple consumed

zensh commented

Please try:

//...

const getCommonStream = () => {
  return gulp.src(`./common.less`)
    .pipe(less())
    .pipe(rename(`common.css`))
    .pipe(gulp.dest('./result'));
}

return merge(getCommonStream(), ...themes.map((theme) => {
    const streams = files.map((file) => getThemedStream(theme, file));
    streams.push(getCommonStream());
    return getConcatStream(theme, streams);
}));

But commonStream will be recompiled multiple times with the same result, which is not efficient.

zensh commented

Is it work in my way?

Your sample works. Unfortunately I keep loosing some files in my production build (not a problem for merge-stream). I'll try to provide you a sample to reproduce that.

I've tried to create my own stream sequencer based on merge-stream but failed when streams participate in multiple sequences grncdr/merge-stream#29 (comment)

Finally I solved my problem by splitting my Gulp tasks into multiple sub-tasks and running them with run-sequence.