teambition / merge2

Merge multiple streams into one stream in sequence or parallel (~119M/month downloads).
MIT License
170 stars 14 forks source link

Streams participating in multiple merges get lost #12

Closed alexanderby closed 7 years ago

alexanderby commented 7 years ago

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 7 years ago

commonStream should not be multiple consumed

zensh commented 7 years ago

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);
}));
alexanderby commented 7 years ago

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

zensh commented 7 years ago

Is it work in my way?

alexanderby commented 7 years ago

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.

alexanderby commented 7 years ago

I've tried to create my own stream sequencer based on merge-stream but failed when streams participate in multiple sequences https://github.com/grncdr/merge-stream/issues/29#issuecomment-313325679

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