I am new to Grunt and to Javascript/Coffeescript altogether.
We are using Grunt in a rather large project with hundreds of .coffee - files. Since Grunt compiles all coffeefiles (although only one file has changed), my initial question was on how to get Grunt to compile only the one changed file. Using stackoverflow I was able to answer that question, thank you all :)
But now it seems that the implemented solution breaks the livereload. When I start with "grunt server" and display my page in the browser, everything looks fine. Then I change one .coffee file and save it. The file gets compiled (I checked), but my browser is never reloaded. Only when I manually reload the browser the new modified code gets displayed.
So the question is: Why does livereload no longer work?
I don't know if this matters, but the Gruntfile was created with yeoman in an older version (with grunt-regarde). I updated the package.json and the Gruntfile to newer specs using grunt-contrib-watch and the buildin livereload. Without the grunt.event.on
everything works fine.
Sources (partially):
grunt.initConfig({
watch: {
coffee: {
files: ['<%= yeoman.app %>/coffeescripts/**/*.coffee'],
tasks: ['coffee:app'],
options: {
nospawn: true
},
},
compass: {
files: ['<%= yeoman.app %>/styles/**/*.{scss,sass}'],
tasks: ['compass']
},
templates: {
files: ['<%= yeoman.app %>/templates/**/*.tpl'],
tasks: ['handlebars']
},
livereload: {
options: {
livereload: LIVERELOAD_PORT
},
files: [
'<%= yeoman.app %>/*.html',
'<%= yeoman.tmp %>/styles/**/*.css',
'<%= yeoman.tmp %>/scripts/**/*.js',
'<%= yeoman.tmp %>/spec/**/*.js',
'<%= yeoman.app %>/img/{,*/}*.{png,jpg,jpeg,webp}',
]
}
},
coffee: {
app: {
expand: true,
cwd: '<%= yeoman.app %>/coffeescripts',
src: '**/*.coffee',
dest: '<%= yeoman.tmp %>/scripts',
ext: '.js',
options: {
runtime: 'inline',
sourceMap: true
},
}
}
}
});
grunt.event.on('watch', function(action, filepath) {
filepath = filepath.replace(grunt.config('coffee.app.cwd')+'/', '' );
grunt.config('coffee.app.src', [filepath]);
});
grunt.registerTask('server', function (target) {
if (target === 'dist') {
return grunt.task.run(['build', 'open', 'connect:dist:keepalive']);
}
grunt.task.run([
'clean:server',
'coffee',
'compass:server',
'symlink:bower',
'connect:livereload',
'handlebars',
'notify:watch',
'watch'
]);
});
grunt-contrib-watch is used with version v0.4.4
,
connect-livereload with version 0.2.0