1.1.3 • Published 5 years ago

gulp-cache v1.1.3

Weekly downloads
23,841
License
MIT
Repository
github
Last release
5 years ago

gulp-cache

NPM version Node version Dependencies 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 gulp from 'gulp';
import favicons from 'gulp-favicons';
import srcset from 'gulp-srcset';
import cache from 'gulp-cache';

gulp.task('favicon', () =>
    gulp.src('src/favicon.svg')
        .pipe(cache(
            // Target plugin, the output of which will be cached.
            favicons(faviconsConfig),
            // Options for `gulp-cache` plugin.
            {
                // Bucket to store favicons in cache.
                name: 'favicons'
            }
        ))
        .pipe(gulp.dest('./favicons'))
);

gulp.task('images', () =>
    gulp.src('src/**/*.{jpg,png,svg}')
        .pipe(cache(
            // Target plugin, the output of which will be cached.
            srcset(srcsetRules),
            // Options for `gulp-cache` plugin.
            {
                // Bucket to store images in cache.
                name: 'images'
            }
        ))
        .pipe(gulp.dest('./images'))
);
import fs from 'fs';
import gulp from 'gulp';
import jshint from 'gulp-jshint';
import cache from 'gulp-cache';

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

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

gulp.task('lint', () =>
    gulp.src('src/**/*.js')
        .pipe(cache(
            // Target plugin, the output of which will be cached.
            jshint('.jshintrc'),
            // Options for `gulp-cache` plugin.
            {
                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'))
});

API

cache(pluginToCache [, options])

pluginToCache

Target plugin, the output of which will be cached.

options

Options for gulp-cache plugin.

options.fileCache

Optional Where to store the cache objects

options.name

Optional The name of the bucket which stores the cached objects

  • Defaults to default
options.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.

options.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.

options.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.

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.

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 });
const 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 - present Jacob Gable

squareboygulpproject-v1@wwwoda/gulp-tasksjquery-file-explorergulp-essentials@infinitebrahmanuniverse/nolb-gulp-cvicocssldl_devkit@everything-registry/sub-chunk-1805@bloombox/js-client@cadbee/0303-smirnov-av-lb3@cbim-epc-magic/stylessandpapersenasaikou-yeomanvue-page-designer-yttweb-dev-toolsthe-old-manthickshake-imagesthe-subtle-agency-workflowtiny-imageminvanilla-cli-node-toolssnazziestr8r-coretemplatestatictempaw-buildtempaw-functionstempaw-functions-zemeztempaw-zemez-functionssapphiresaytum-autocompleteslingshot-buildsilvvrsnp-gulp-taskssponge-rod@agence-webup/gulpy0303-lb3-paket@gulppress/images@gulppress/scriptsdom-focusdong-gulp-cli@felipecastillo/scss-boilerplate@evokegroup/str8r-core-multilang@knkui/scripts@ministryofjusticecz/frontend@nwetzel/modern-web-dev-build@newsteam/brick@newsteam/cli-tasksfeflow-devkit-mpnative-startkitneto-theme-masterenterprise-portalmeanitmeanstackmodern-web-dev-buildmk-saytum-startmlv1mkbag@wibetter/gulp-project@sunriseweb/symfony-gulp@superskrypt/gulp-tasks@sweetlikepete/brickdynaware-gulp-taskseasy-gulp-by-orelejs_covert_htmlmichaelmodulefront-custosangular-buildfrontend-incubator-kss-templategamexaym-gulpazk-devgrafxbangergrommet-toolboxlucify-build-toolsopine-imagesgulp-ajswebgulp-wp-toolkitgulp-tasks-buildbloomboxoxyz-buildgulp-plugins-dcvbalm-lightgulp-angular-projectgulp-bitrix-templates-toolkit@outlinejs/gulp-tasks@tamland/clilight-scriptslifterlsl-physxlossy-imageminadventurine-testnode-build-web-appgalactica-tokenargon-design-system-freeargon-m2appirio-gulp-tasksboiler-roombooks-fe-polymer-dev-sampleh5-templeteghost-helm
1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.5.0

6 years ago

0.4.6

7 years ago

0.4.5

8 years ago

0.4.4

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.0

9 years ago

0.2.10

9 years ago

0.2.9

9 years ago

0.2.8

9 years ago

0.2.7

9 years ago

0.2.6

9 years ago

0.2.5

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.11

10 years ago

0.1.10

10 years ago

0.1.9

10 years ago

0.1.8

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago