Git Product home page Git Product logo

gulp-cached's Introduction

Build Status

NPM version

Information

Packagegulp-cached
Description A simple in-memory file cache for gulp
Node Version >= 0.9

Usage

This keeps an in-memory cache of files (and their contents) that have passed through it. If a file has already passed through on the last run it will not be passed downstream. This means you only process what you need and save time + resources.

Take this example:

var cache = require('gulp-cached');

gulp.task('lint', function(){
  return gulp.src('files/*.js')
    .pipe(cache('linting'))
    .pipe(jshint())
    .pipe(jshint.reporter())
});

gulp.task('watch', function(){
  gulp.watch('files/*.js', ['lint']);
});

gulp.task('default', ['watch','lint']);
  • User saves files/a.js and the lint task is called
    • the files do not exist in the cache yet
    • files/a.js and files/b.js are linted
  • User saves files/b.js and the lint task is called
    • the contents of the file changed from the previous value
    • files/b.js is linted
  • User saves files/a.js and the lint task is called
    • the contents of the file have not changed from the previous value
    • nothing is linted

So the first run will emit all of the items downstream. Runs after that will only emit if it has been changed from the last file that passed through it with the same path.

Please note that this will not work with plugins that operate on sets of files (concat for example).

cache(cacheName[, opt])

Creates a new cache hash or uses an existing one.

Cache key = file.path + file.contents

If a file exists in the cache, it is ignored.

If a file doesn't exist in the cache, it is passed through as is and added to the cache.

The last cache for this path is cleared so if you modify a file to a, then to b, then back to a all 3 will be a cache miss.

Possible options

optimizeMemory - Uses md5 instead of storing the whole file contents. Better if you are worried about large files and their effect on memory consumption. Default is false. In my experience this doesn't make much of a difference (only 25mb vs 26mb) but with a lot of files or a few large files this could be a big deal.

Clearing the cache

Clearing the whole cache

cache.caches = {};

Clearing a specific

delete cache.caches['cache name yo'];

LICENSE

(MIT License)

Copyright (c) 2015 Fractal [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

gulp-cached's People

Contributors

jjclark1982 avatar nick11roberts avatar pdehaan avatar stephenlacy avatar yocontra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gulp-cached's Issues

Livereload not working when using gulp-cached?

I'm trying to implement gulp-cached to speed up my sass task but I can't seem to get it to work with livereload. Is this not meant to work?

It seems to cache it as the task goes from 4 seconds on initial run to 20 something milliseconds on subsequent runs but it's not passed to the reload pipe.

This is my SASS task:

gulp.task('styles', function () {
    return gulp.src( paths.scss )
        .pipe( cache( 'sass' ) )
        .pipe( scss( options.scss ).on( 'error', gutil.log ) )
        .pipe( autoprefix( options.autoprefix ) )
        .pipe( gulp.dest( dests.css ) )
        .pipe( connect.reload() )
        .pipe( notify( { message: 'CSS task complete.' } ) );
});

Here's my full gulp file: http://hastebin.com/ihicukigaw.coffee

cache destination name

gulp.task('indexfiles', function() {
    return gulp.src('./**/index.src.php')
        .pipe(inlinesource())
        .pipe(usemin({
            inlinecss: [ minifyCss, 'concat' ],
            inlinejs: [uglify(), 'concat']
        }))
        .pipe(cache('index'))
        .pipe(gulpif(
            (process.env.ENV !== 'development'),
            replace('"/assets/', '"https://cdn/assets/')
        ))
        .pipe(rename({
            basename: 'index'
        }))
        .pipe(gulp.dest('.'));
});

Is there any way of telling the module that the outcome will be "index.php" because of the rename?
My source files are in the same directory as the output - source is index.src.php and output is index.php

Thanks!

Single entry .less file

Hi,

i have 'main' index.less file which @import some additional files like

@import a.less
@import b.less
@import c. less and so...

So i have a gulp watcher

gulp.watch([less folder path *.less], ['styles-frontend']);

on all less files, but my build function is like:

gulp.task('styles-frontend', function() {
    return gulp.src(path.stylesFrontend.src)
        .pipe(cache('styles-frontend'))
        .pipe(sourcemaps.init())
        .pipe(prefixer({
            browsers: ['last 5 versions']
        }))
        .pipe(less({
            errLogToConsole: true,
            plugins: [cleancss]
        }))
        .pipe(cssmin())
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest(path.stylesFrontend.dest))
});

The problem is that gulp-cached 'caching' only the 'index.less' file for it's name and contents, so if i update b.less - the cache will return the same file so nothing will change at all.

Any suggestion to run cache for every file?

Cached file location/name causes problems with gulp.watch

The cached file is stored in the same directory as the source file, with the same extension. As a result, something like gulp.watch('some-dir/**/*.scss', ['lint-scss']) causes gulp to watch the cached files as well as the original source. This has been causing problems for me. Every time a cache file is deleted, gulp fires of the lint-scss task, resulting in an Input file did not exist or was not readable error.

Update:
Ok, it looks like the error is being caused by the gulp.src() that leads off the lint-scss task, rather than the watch itself. the lint-scss task uses the same 'some-dir/**/*.scss' glob that is passed to gulp.watch, and the inclusion of the cache files in that stream seams to be what is causing the error.

Cache files being included in the gulp.watch glob is also problematic, however, as changes to cache files are trigger additional, redundant executions of the lint-scss task.

Support for ignoring certain files

I am currently using gulp-cached with typescript so that I can speed up the compilation of my code.

However the index of my application references a dts.d.ts file, which is the source definitions for the compiler. And since this file never changes, it is never processed by gulp-cached, which causes the compiler to break down and not work.

Now I could add a reference to my dts.d.ts file in every single one of my source files, but that is tedious and annoying. So I currently use a hacky way to always process the definition file.

var cache = $.cached.caches['compile-typescript'];
 if (cache) {
   delete cache[''typings/dts.d.ts']
 }

It works, but It would be nice if I could just pass that file to gulp-cached so it will always allow it through.

Not caching changed files

The problem:
gulp.src is excluding .html files with an underline at the beginning, therefore changes made in the underlined files are not getting cached.

My code:

var cache = require('gulp-cached');
gulp.task('html', function() {
  return gulp.src('app/templates/**/!(_)*.html')
    .pipe(cache('process-html'))
    .pipe(gulp.dest('.tmp'));
});

How can I tell gulp-cached to cache the underlined files too?

Thanks in advance.

Checking for changes accross teams

I want to use gulp-imagemin which is a very slow and processor intensive task and I am working in a team environment using Git. How can I make sure that an image is only ever processed once in my entire team? Where does gulp-changed-in-place save it's change log? Can it be checked-in to Git?

Need an option to init the cache

Currently, when I run my gulp watch task, everything is linted on the first save.
The second time around, only the file I changed gets linted.

My question: Is there a way to enable some sort of init value? I want to cache all my javascript files the first time around if the cache is empty, but NOT lint at all. after that, start my piping and only the changed file will be linted

Code inject of Browser-sync not work

Hi, I am trying to configure gulp-cached in my streams, but code injection of Browser Sync not works.

This is my code:

gulp.task(config.css.task, function () {
	return gulp.src(config.css.source)
	.pipe(cache('css'))
	.pipe(sass(config.css.plugin.sass))
	.pipe(postcss(config.css.plugin.postcss))
	.pipe(gulp.dest(config.css.dist))
	.pipe(browserSync.stream());
});

Is not compatible with this feature? or do I have a bad configuration?

gulp-cached and gulp-typescript compatibility

Not sure if this is the right place to log this or not. Sorry if it is not.

Currently gulp-cached and gulp-typescript cannot work together if you are using global variables or functions. While obviously that is not ideal in a JS project, it is the reality of many legacy applications.

I am currently converting a JS proj to TS. It has ~35 JS files which are included into their respective html files as needed. I have converted each js file into .ts and have it compiling them into .js using gulp-typescript. However the watcher takes ~11s to run due to the size and amount of files. gulp-cached is the ideal fix for this. However.

When gulp-cached filters out the unchanged files from the file list and passes the changed files onto gulp-typescript, gulp-typescript then errors because the global functions/variables don't exist within the set of files currently being compiled.

There needs to be a way that gulp-typescript can access the cache of files to ensure the currently being compiled files dont error when combined with the cached files (excluding the file being compiled).

Hope this makes sense.

nothing in cache

Hello,

I'm testing gulp-cached for my imagemin task :

gulp.task('img', function () {
  return gulp.src(source + imgFiles)
    .pipe(cache('images'))
    .pipe(plugins.imagemin({
      svgoPlugins: [{
        removeViewBox: false
      }, {
        cleanupIDs: false
      }]
    }))
    .pipe(gulp.dest(destination + 'assets/'));
});

I'm testing with 2 png files.

Each time I run the task, every files is optimised :

gulp-imagemin: Minified 2 images (saved 27.44 kB - 44.5%)

Is there a problem with my task ?

Error when using gulp-load-tasks

When using gulp-load-tasks, I get this error:

Object #<Object> has no method 'cached'

With the example from the homepage, the task looks like this:

gulp.task('lint', function(){
  return gulp.src(dir.src + '/scripts/**')
    .pipe(task.cached('linting'))
    .pipe(task.jshint())
    .pipe(task.jshint.reporter())
});

Cheers!

Files don't seem to be cached

Caching doesn't seem to be working in my setup, perhaps I'm just missing something. Here is what I have:

gulp.task('svgmin', function() {
    return gulp.src('src/images/**/*.svg')
        .pipe(cached('svgmin'))
        .pipe(svgmin())
        .pipe(gulp.dest('dist/images'))
});

Each time the task runs, it look to minify the SVGs despite already being minified. I tried the same with linting (as the example from the readme), and it lints each file, even without a change.

Versions of everything are all up-to-date.

Cheers!

segfault isolated

Hello, i recently changed from gulp-cached to gulp-changed, and since then, i haven't seen any seg fault errors. I use it for production to copy files from dev to production, images, and scss tasks. its a couple hundred files. I just wanted to let you know.

Does cache clear upon termination of gulp task?

I have an images task that pipes/minifies etc to an images folder along with automated image optimization. I was experiencing a performance problem where if I only changed/deleted/added 1 image, the entire library would upload. I switched from gulp-changed to gulp-cached which is looking promising, but there is one catch...it seems my cache clears every time I terminate and restart my gulp watch task.

FWIW my code/task looks like this:

var imagesTask = function() {
  var flyoutResizeFilter = gulpFilter('**/*/accessories-*png', {restore: true});
  return gulp.src(paths.src)
     // below used to be .pipe(changed(paths.dest))
     .pipe(cached(paths.dest)) // Ignore unchanged images
     .pipe(imagemin({
       plugins: [
         imagemin.jpegtran(), imagemin.gifsicle(), imagemin.optipng({optimizationLevel:5}), imagemin.svgo()
       ],
       verbose: true,
     }))
    .pipe(flyoutResizeFilter)
    .pipe(imageResize({
      height: 135
    }))
    .pipe(flyoutResizeFilter.restore)
    .pipe(rename({
      prefix: 'images-'
    }))
    .pipe(gulp.dest(paths.dest))
}

If I add an image, the entire library uploads - once that is done if another image is changed or added it behaves as expected. So this is more of a 2 part question:

  1. Does the cache clear every time a gulp process is terminated?
  2. If so is there a way to preserve it after termination?

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.