npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-cache-files

v0.0.2

Published

Gulp plugin for cache files

Downloads

11

Readme

gulp-cache-files

bitHound Dependencies Build Status

A disk based files caching task for gulp. This plugin based on file mtime by default (could use another file property after some configurations see below) it writes json file with formatted relative path as key and mtime as value. One of the main features of this plugin is that it can deal with projects where source folder is dist folder (it could be useful for saving disk space when you have a lot of images in project).

Installation

Install package with NPM and add it to your development dependencies:

npm install --save-dev gulp-cache-files

Examples

Basic example of using the cache to manage image minification with the imagemin module.

var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var cacheFiles = require('gulp-cache-files');

gulp.task('images', function() {
  return gulp.src('./images/*.{jpg,png,jpeg,gif,svg}', {read: false})
    // Specify the location and name of the cache file
    //or use it without parameter (creates default file)
    .pipe(cacheFiles.filter('./mycache/manifest.json'))
    .pipe(imagemin({
      verbose: true
    }))
    .pipe(gulp.dest('./images/'))
    .pipe(cacheFiles.manifest());
});

{read: false} option used to do not load file contents, which increases performance (file contents will be passed through the pipeline inside filter function)

This will create a cache file named manifest.json of all files passed through the pipeline to be excluded from subsequent runs. If a file that has been cached is updated, the cache will recognize this and pass the file through to update it in the manifest.json file.

*cache file (manifest.json) could be added to version control for saving time of other developers

When you need 2 or more uses of gulp-cache-files module you should use require('gulp-cache-files').Prototype as on example below:

var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var jshint = require('gulp-jshint');
var CacheFiles = require('gulp-cache-files').Prototype;

gulp.task('images', function() {
  var cacheFiles = new CacheFiles();
  return gulp.src('./images/*.{jpg,png,jpeg,gif,svg}', {read: false})
    .pipe(cacheFiles.filter())
    .pipe(imagemin({
      verbose: true
    }))
    .pipe(gulp.dest('./images/'))
    .pipe(cacheFiles.manifest());
});

gulp.task('jshint', function() {
  var cacheFiles = new CacheFiles();
  return gulp.src('scripts/*.js')
    .pipe(cacheFiles.filter('./js-cache/manifest.json'))
    .pipe(jshint())
    .pipe(cacheFiles.manifest())
});

Use of manifest function separately from filter function:

var gulp = require('gulp');
var cacheFiles = require('gulp-cache-files');

gulp.task('store-mtimes', function() {
  return gulp.src('./files/**/*', {read: false})
    .pipe(cacheFiles.manifest('files-mtimes.json'))
    .pipe(gulp.dest('.'));
});

it just writes file.stat.mtime.getTime() as value and formatted* relative path as key into file 'files-mtimes.json'

*formatted means that it replaces Windows backslashes with such rule path.replace(/\\/g, '/')

Compare custom file properties:

var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var cacheFiles = require('gulp-cache-files');
var Q = require('kew');
var fs = require('fs');

gulp.task('images', function() {
  return gulp.src('./images/*.{jpg,png,jpeg,gif,svg}', {read: false})
    .pipe(cacheFiles.filter({
      dest: './size-cache.json',
      comparator: function(criteria, file){
        //if cached size property (criteria) is same
        //as size of current file at moment when it passing through stream
        //then file is cached and it is not going through stream
        return criteria === file.stat.size;
      }
    }))
    .pipe(imagemin({
      verbose: true
    }))
    .pipe(gulp.dest('./images/'))
    .pipe(cacheFiles.manifest({
      criteria: function (file) {
        //returns current file size value which will be written in size-cache.json
        return Q.nfcall(fs.stat, file.path).then(function(stats) {
          return stats.size;
        });
      }
    }));
});

API

filter(dest)

  • dest - string Path to cache file (manifest.json).

    default: - './gulp-cache-files/manifest.json'

filter(options)

  • options.dest - string As above.

  • options.comparator - function Compares cached file property with uncached (by default it is file.stat.mtime)

    options.comparator(criteria, file [, manifest])

    criteria - Cached value from cache file (manifest.json)

    file - File object passed through stream

    manifest - (optional) Object where formatted relative path is a key and file property as a value {"fixtures/foo.png":1497680208602,"fixtures/bar.png":1497680208623}

    default:

    function(criteria, file){
      return criteria === file.stat.mtime.getTime();
    }

manifest(name)

  • name - string name of cache file. (works only if used in separate gulp task without filter function above)

    default: - 'manifest.json'

manifest(options)

  • options.name - string As above.

  • options.criteria - function Returns property or kew promise with this property which will be stored in cache file as value {"fixtures/foo.png":1497680208602}

    options.criteria(file)

    file - File object passed through stream

    default:

    function(file) {
      return Q.nfcall(fs.stat, file.path).then(function(stats) {
        return stats.mtime.getTime();
      });
    }

    Q here is just initialized instance of kew framework (var Q = require('kew');)

License

MIT