gulp-community / gulp-cached

A simple in-memory file cache for gulp
MIT License
452 stars 23 forks source link

Added unpipeOnCache option #21

Closed ghost closed 7 years ago

ghost commented 7 years ago

Added an unpipeOnCache option to unpipe all writable streams. Added so modules like gulp-imagemin will not generate any output when they are cached.

yocontra commented 7 years ago

Could you elaborate more on what case this is useful for?

ghost commented 7 years ago

I actually made two super careless mistakes by accident when I was refactoring something for another project. Not sure how those commits actually wound up there. The errors have been fixed.

SSSCE:

gulp.src(__dirname + "/imgs/**/*")
    .pipe(cache("imgs", {"unpipeOnCache": true}))
    .pipe(imagemin())
    .pipe(gulp.dest(__dirname + "/public/imgs/"), {"overwrite": true}); 

Some plugins like imagemin will continue to output gulpimagemin: Minified X image even when the resource has been cached. Setting unpipeOnCache will terminate the rest of the pipes if the resource has been cached.

yocontra commented 7 years ago

If a file has already passed through on the last run it won't be passed downstream - so your issue seems like the cache is being missed, because gulp-imagemin is getting the file and logging it still.

Does using the optimizeMemory flag fix this?

ghost commented 7 years ago

Nope setting the optimizeMemory flag does not. The module gulp-imagemin is still receiving from upstream but it's not optimizing it. It just outputs the below showing that after its initial run it doesn't optimize again.

[17:39:50] gulp-imagemin: Minified 1 image (saved 4.17 kB - 67.3%)
[17:39:58] Starting 'buildAssets'...
[17:39:58] Finished 'buildAssets' after 4.42 ms
[17:39:58] gulp-imagemin: Minified 0 images
[17:40:02] gulp-imagemin: Minified 62 images (saved 2.68 MB - 31.1%)
[17:40:04] Starting 'buildAssets'...
[17:40:04] Finished 'buildAssets' after 9.58 ms
[17:40:04] gulp-imagemin: Minified 0 images

There is a watcher on the source directory and the task buildAssets is run whenever that directory changes and I haven't noticed any differences in file creation dates or checksums.

I'm pretty sure it's due to this part in gulp-imagemin

        const percent = totalBytes > 0 ? (totalSavedBytes / totalBytes) * 100 : 0;
        let msg = `Minified ${totalFiles} ${plur('image', totalFiles)}`;

        if (totalFiles > 0) {
            msg += chalk.gray(` (saved ${prettyBytes(totalSavedBytes)} - ${percent.toFixed(1).replace(/\.0$/, '')}%)`);
        }

        gutil.log('gulp-imagemin:', msg);
        cb();

It serves as the argument for through2's flushFunction. So any gulp module that uses flushFunctions will execute their flushFunctions even if the file is cached.

yocontra commented 7 years ago

@Xtrinity Yeah, it isn't actually minifying anything though. What's the problem, you want the log message that says 0 to go away?

Couple options:

You should be able to wrap imagemin in gulp-if to only load the plugin in the case of a file:

.pipe(gif(true, () => imagemin()))

Closing this since it isn't an issue with this package.