hparra / gulp-rename

Rename files easily
MIT License
692 stars 73 forks source link

File name changes propagate backwards in the stream pipeline #31

Closed radekn closed 9 years ago

radekn commented 10 years ago

gulp-rename seems to mutate its input, which means means that changes propagate backwards in the stream pipeline. Consider following code:

var rename = require('gulp-rename');

var scripts = gulp.src([
    'src/script.js'
]);

var minified = scripts
.pipe(minify())
.pipe(rename({
    suffix: '.min'
}));

mergeStreams(scripts, minified)
.pipe(gulp.dest('build'));

mergeStreams is a function that pipes the sources into one destination stream and closes it when all sources end, and minify just returns a stream that minifies JS files.

This pipeline should write two files: build/script.js - an unchanged copy of source file, and build/script.min.js - a processed source file. But instead, it tries to write both files as build/script.min.js.

gulp-rename did not just output its input file with changed name, it changed the name of the input file itself. If gulp-rename made a shallow copy of the input file and didn't mutate the input file itself, this problem wouldn't have occurred.

crissdev commented 9 years ago

@radekn You must create two separate streams for the same glob, otherwise the same File instance will be used. I'm not aware of any gulp plugin that will clone the input file. Does any plugin do that?

var scripts = gulp.src(['src/scripts.js']);
var minified = gulp.src(['src/scripts.js']).pipe(minify())
    .pipe(rename({
        suffix: '.min'
    }));
radekn commented 9 years ago

@crissdev I don't know if any gulp plugin actually handles it that way. To me it seems like a waste because it limits the flexibility of streams API, so I asked a question about this in gulpjs/gulp#754. So far it seems that the official stance is that the current behavior is acceptable. Personally I don't agree, but if it's fine for everyone else, so be it.

shinnn commented 9 years ago

As @crissdev said, and already concluded in https://github.com/gulpjs/gulp/issues/754 and https://github.com/terinjokes/gulp-uglify/issues/62.