我完全是 Grunt 和 Javascript/Coffeescript 的新手。
我们在一个包含数百个 .coffee 文件的大型项目中使用 Grunt。由于 Grunt 编译了所有的咖啡文件(尽管只有一个文件发生了变化),我最初的问题是如何让 Grunt 只编译一个已更改的文件。使用stackoverflow我能够回答这个问题,谢谢大家:)
但现在看来,实施的解决方案打破了 livereload。当我从“grunt server”开始并在浏览器中显示我的页面时,一切看起来都很好。然后我更改一个 .coffee 文件并保存它。该文件被编译(我检查过),但我的浏览器永远不会重新加载。只有当我手动重新加载浏览器时,才会显示新的修改代码。
所以问题是:为什么 livereload 不再起作用?
我不知道这是否重要,但 Gruntfile 是使用 yeoman 在旧版本中创建的(使用 grunt-regarde)。我使用 grunt-contrib-watch 和 buildin livereload 将 package.json 和 Gruntfile 更新为更新的规范。没有grunt.event.on
一切工作正常。
来源(部分):
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 与 version 一起使用v0.4.4
,connect-livereload 与 version 一起使用0.2.0