Skip to content

Instantly share code, notes, and snippets.

@Vadorequest
Created October 9, 2015 18:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vadorequest/b48bcfda2d0205ba3f95 to your computer and use it in GitHub Desktop.
Save Vadorequest/b48bcfda2d0205ba3f95 to your computer and use it in GitHub Desktop.
Grunt-watch config file.
/**
* Run predefined tasks whenever watched file patterns are added, changed or deleted.
*
* ---------------------------------------------------------------
*
* Watch for changes on
* - files in the `assets` folder
* - the `tasks/pipeline.js` file
* and re-run the appropriate tasks.
*
* For usage docs see:
* https://github.com/gruntjs/grunt-contrib-watch
*
*/
module.exports = function(grunt) {
var path = require('path');
var _ = require('lodash');
grunt.config.set('watch', {
styles: {
files: ['assets/linker/styles/**/*.less'],
// When assets are changed:
tasks: ['less:dev', 'sync:dev', 'sails-linker:devStyles'],
options: {
spawn: false,
}
}
});
/**
* Custom events.
*/
grunt.event.on('watch', function(action, filePath, watchedTargetName) {
switch(watchedTargetName){
/*
Compile only what is needed.
Based on a white list of sub folders within the "styles" directory.
White listed folders will not require to compile all LESS file, but only the changed ones.
Others will require to compile everything.
*/
case 'styles':
// Root path from where the files are located.
var rootPath = 'assets/linker/styles/';
// Path of the file
var filePathRelativeToRootPath = path.relative(rootPath, filePath);
// Grunt task name (name of the file as well)
var gruntTask = 'less';
// Sub task to use.
var subTaskName = 'dev';
// List of folders that don't need to recompile everything.
var whiteListFolders = [
'common',
'devices',
'layous',
'themes',
'views',
];
if(action === 'changed'){
var isDir = path.dirname(filePath) !== '';
var dirName = filePathRelativeToRootPath.split(path.sep)[0];
// If the file is a directory and is belongs to the white list then we will override the grunt config on the fly to compile only that file.
if(isDir && _.contains(whiteListFolders, dirName)){
// We load the less config located at tasks/config/less.js
var config = grunt.config(gruntTask);
// Checks for any misconfiguration.
if(!config){
log.error('There is no config for the grunt task named ' + gruntTask);
}
if(!config[subTaskName]){
log.error('There is no sub task named ' + subTaskName + " for the the grunt task named " + gruntTask);
}
// Update the files.src to be the path to the modified file (relative to srcDir).
// Instead of updating all files, it will only update the one that has been changed.
config[subTaskName].files[0].src = filePathRelativeToRootPath;
grunt.config("less", config);
console.info('watcher LESS - The file ' + filePath + ' is in the white list and will be updated alone.');
}else{
console.info('watcher LESS - The file ' + filePath + ' is not is the white list, all LESS files will be updated.');
}
}
break;
}
} );
grunt.loadNpmTasks('grunt-contrib-watch');
};
@Vadorequest
Copy link
Author

Tasks:

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