I have seen many issues which are related to this, but I haven't found the desired solution.
Problem : While I am working with sass files or js files in my project, I want to compile/jshint those files when they were changed. So I am using grunt-contrib-watch to do this. It is working fine, but after some files changed, It is tracking the file changes, but It stops compiling( stops running the task, which has to run when the file changes ).
Code :
module.exports = function (grunt) {
grunt.initConfig({
config: {
PROJECT_DIR: "Project"
},
sass: {
current: {
options: {
style: 'expanded',
lineNumber: true,
sourcemap: 'none',
update: true
}
}
},
jshint: {
all: []
},
watch: {
sass: {
files: ['<%= config.PROJECT_DIR %>**/sass/*.scss'],
tasks: ['sass:current'],
options: {
spawn: false,
debounceDelay: 2000
}
},
js: {
files: ['<%= config.PROJECT_DIR %>**/js/*.js'],
tasks: ['jshint'],
options: {
spawn: false,
debounceDelay: 2000
}
}
}
});
grunt.event.many('watch', 10, function (action, filepath, target) {
var paths = new cleanPaths(filepath);
var filepath = paths.filepath;
var dirpath = paths.dirpath;
switch (target) {
case 'sass':
if (grunt.file.isMatch(grunt.config('watch.sass.files'), filepath)) {
grunt.config('sass.current.src', filepath);
grunt.config('sass.current.dest', filepath.replace('sass/', 'css/').replace('.scss', '.css'));
// pushFile(filepath, filepath.replace('sass/', 'css/').replace('.scss', '.css'));
}
break;
case 'js':
if (grunt.file.isMatch(grunt.config('watch.js.files'), filepath)) {
grunt.config('jshint.all', [filepath]);
// pushFile(filepath)
}
break;
default:
break;
}
});
grunt.registerTask('default', ['watch']);
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-jshint');
};
I am dynamically updating the sass configs( src and dest values).
Package.json
{
"name": "",
"version": "1.0.0",
"main": "Gruntfile.js",
"scripts": {},
"author": "",
"license": "",
"description": "",
"devDependencies": {
"grunt-contrib-jshint": "^1.0.0",
"grunt-contrib-sass": "^1.0.0",
"grunt-contrib-watch": "^1.0.0"
}
}
Console image link : https://cloud.githubusercontent.com/assets/8776227/16587119/b7bfb332-42e6-11e6-8281-789ef60d22ec.png
Stops compiling even when, there is no error in the sass code.
I am using latest version of grunt-contrib-watch plugin.