Skip to content

Instantly share code, notes, and snippets.

@luissquall
Last active December 16, 2015 08:48
Show Gist options
  • Save luissquall/5408257 to your computer and use it in GitHub Desktop.
Save luissquall/5408257 to your computer and use it in GitHub Desktop.
Apply tasks only to changed files.
/*global module:false*/
module.exports = function(grunt) {
// Load tasks
grunt.loadTasks('util/grunt');
// Load vendors tasks
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
// Project configuration
grunt.initConfig({
compass: {
dist: {
options: {
config: 'config.rb'
}
}
},
uglify: {
dist: {
expand: true,
cwd: 'app/webroot/js/src/',
src: ['**/*.js'],
dest: 'app/webroot/js/build/'
}
},
jshint: {
dist: {
src: ['app/webroot/js/src/**/*.js']
}
},
watch: {
options: {
nospawn: true
},
stylesheets: {
files: 'app/webroot/css/src/**/*.scss',
tasks: ['compass']
},
scripts: {
files: 'app/webroot/js/src/**/*.js',
tasks: ['jshint', 'uglify']
}
}
});
grunt.event.on('watch', function(action, filepath) {
if (grunt.file.isMatch(grunt.config('watch.stylesheets.files'), filepath)) {
grunt.config('compass.dist.options.specify', [filepath]);
}
if (grunt.file.isMatch(grunt.config('watch.scripts.files'), filepath)) {
var uglifySrc = filepath.replace(grunt.config('uglify.dist.cwd'), '');
grunt.config('jshint.dist.src', [filepath]);
grunt.config('uglify.dist.src', [uglifySrc]);
}
});
// Default task.
grunt.registerTask('default', ['compass', 'jshint', 'uglify']);
};
@crdx
Copy link

crdx commented May 25, 2013

You can get rid of the direct minimatch dependency and use grunt.file.isMatch btw:

if (grunt.file.isMatch(grunt.config('watch.scripts.files'), filepath)) {

I discovered this because I had multiple globs in my watch files, which minimatch doesn't support as an argument to that function, but grunt.file.isMatch does.

https://github.com/gruntjs/grunt/wiki/grunt.file#gruntfileismatch

@luissquall
Copy link
Author

Thank you @crdx. I'm updating the example to reflect your suggestion.

@viclm
Copy link

viclm commented Sep 4, 2013

it seems doesn't work

@jonakyd
Copy link

jonakyd commented Oct 28, 2013

doesn't work either

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment