Coder Social home page Coder Social logo

gulp-cache's Introduction

gulp-cache

NPM version Node version Dependency status Build status Coverage status

A temp file based caching proxy task for gulp.

Install

npm i -D gulp-cache
# or
yarn add -D gulp-cache

Usage

import fs from 'fs';
import gulp from 'gulp';
import jshint from 'gulp-jshint';
import cache from 'gulp-cache';

gulp.task('lint', () =>
    gulp.src('./lib/*.js')
        .pipe(cache(jshint('.jshintrc'), {
            key: makeHashKey,
            // What on the result indicates it was successful
            success(jshintedFile) {
                return jshintedFile.jshint.success;
            },
            // What to store as the result of the successful action
            value(jshintedFile) {
                // Will be extended onto the file object on a cache hit next time task is ran
                return {
                    jshint: jshintedFile.jshint
                };
            }
        }))
        .pipe(jshint.reporter('default'))
});

const jsHintVersion = '2.4.1',
    jshintOptions = fs.readFileSync('.jshintrc');

function makeHashKey(file) {
    // Key off the file contents, jshint version and options
    return `${file.contents.toString('utf8')}${jshintVersion}${jshintOptions}`;
}

Clearing the cache

If you find yourself needing to clear the cache, there is a handy dandy cache.clearAll() method:

import cache from 'gulp-cache';

gulp.task('clear', () =>
    cache.clearAll()
);

You can then run it with gulp clear.

Options

fileCache

[Optional] Where to store the cache objects

name

[Optional] The name of the bucket which stores the cached objects

  • Defaults to default

key

[Optional] What to use to determine the uniqueness of an input file for this task.

  • Can return a string or a Promise that resolves to a string.

  • The result of this method is converted to a unique MD5 hash automatically; no need to do this yourself.

  • Defaults to file.contents if a Buffer, or undefined if a Stream.

success

[Optional] How to determine if the resulting file was successful.

  • Must return a truthy value that is used to determine whether to cache the result of the task. Promise is supported.

  • Defaults to true, so any task results will be cached.

value

[Optional] What to store as the cached result of the task.

  • Can be a function that returns an Object or a Promise that resolves to an Object.

  • Can also be set to a string that will be picked of the task result file.

  • The result of this method is run through JSON.stringify and stored in a temp file for later retrieval.

  • Defaults to 'contents' which will grab the resulting file.contents and store them as a string.

One-to-many caching

To support one-to-many caching in Your Gulp-plugin, you should:

  • Use clone method, to save _cachedKey property:
const outputFile1 = inputFile.clone({ contents: false }),
    outputFile2 = inputFile.clone({ contents: false });

outputFile1.contents = new Buffer(...);
outputFile2.contents = new Buffer(...);

const outputFiles = [
    outputFile1,
    outputFile2,
    ...
];
  • Or, do it manually:
const outputFiles = [
    new Vinyl({..., _cachedKey: inputFile._cachedKey}),
    new Vinyl({..., _cachedKey: inputFile._cachedKey}),
    ...
];

License

The MIT License (MIT)

Copyright (c) 2014 - 2017 Jacob Gable

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.