Javascript grunt watch多次执行任务
我有以下Grunfile.js文件:Javascript grunt watch多次执行任务,javascript,gruntjs,grunt-contrib-watch,Javascript,Gruntjs,Grunt Contrib Watch,我有以下Grunfile.js文件: module.exports = function(grunt) { // Project configuration. grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), sass: { dist: { options: { style:
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
sass: {
dist: {
options: {
style: 'compressed'
},
files: {
'css/build/style.css': 'css/style.scss'
}
}
},
watch: {
stylesheets: {
files: ['css/*.scss'],
tasks: ['newer:sass']
}
}
});
// Load the plugin that compiles sass
grunt.loadNpmTasks('grunt-contrib-sass');
// watch for, and run grunt if files change
grunt.loadNpmTasks('grunt-contrib-watch');
// Plugin for Grunt tasks to run with newer files only.
grunt.loadNpmTasks('grunt-newer');
// Grunt Tasks:
grunt.registerTask('default', ['newer:sass']);
};
运行grunt watch并保存我的scss文件后,控制台输出如下:
Running "watch" task
Waiting...
>> File "css\style.scss" changed.
Running "newer:sass" (newer) task
Running "newer:sass:dist" (newer) task
Running "sass:dist" (sass) task
File css/build/style.css created.
Running "newer-postrun:sass:dist:1:D:\xampp\htdocs\grunt-test\node_modules\grunt-newer\.cache" (newer-postrun) task
Done, without errors.
问题是,每次都会编译.scss文件。即使没有变化
我不明白为什么grunt要运行3个任务(更新的:sass,更新的:sass:dist,sass:dist),而不是只运行在watch下定义的任务(更新的:sass)
希望有人有办法解决这个问题。:) 我不确定这是问题所在,但请尝试指定您不希望运行所有的
sass
,只运行sass:dist
。所以试试:grunt.registerTask('default',['newer:sass:dist'])代码>