gulp-data icon indicating copy to clipboard operation
gulp-data copied to clipboard

multiple output streams need a new file or data is overwritten

Open mwoodruff opened this issue 9 years ago • 1 comments

Data is overwritten on the file object, which breaks piping from a single stream multiple times. For example, the below task will always have file.data.foo = "baz" Perhaps the file object should be cloned before the data is extended? Or perhaps an option for that behaviour would be useful? Is there a better way to get that effect I'm missing?

gulp.task("test", function() {
    var a = gulp.src(files)
        .pipe(data(function(file) { return { foo: "bar" }; }));

    var b = a
        .pipe(rename("something.else"))
        .pipe(data(function(file) { return { foo: "baz" }; }));

    return merge(a, b)
        .pipe(gulp.dest("./dist/"));
}

mwoodruff avatar Mar 26 '15 13:03 mwoodruff

It doesn't look like the file.clone() by its self actually deep clones the data. For the record, this fixes my immediate issues:

function clonedata() {
    return through.obj(function (file, enc, cb) {
        var f = file.clone();
        f.data = JSON.parse(JSON.stringify(f.data));
        cb(null, f);
    });
}

gulp.task("test", function() {
    var a = gulp.src(files)
        .pipe(data(function(file) { return { foo: "bar" }; }));

    var b = a
        .pipe(clonedata())
        .pipe(rename("something.else"))
        .pipe(data(function(file) { return { foo: "baz" }; }));

    return merge(a, b)
        .pipe(gulp.dest("./dist/"));
}

mwoodruff avatar Mar 26 '15 13:03 mwoodruff