javascript Grunt - 监视任务不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18225206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grunt - Watch Task is not working
提问by PI.
It seems like my watch task in my project grunt file is starting up then quitting straight away.
似乎我的项目 grunt 文件中的监视任务正在启动然后立即退出。
Please review the screenshot attached.
请查看随附的屏幕截图。
Please find the code for my Gruntfile.js;
请找到我的 Gruntfile.js 的代码;
module.exports = function (grunt) {
grunt.initConfig({
sass: {
options: {
cacheLocation: '.sass-cache'
},
prod: {
options: {
style: 'compressed'
},
files: [{
'assets/css/dist/main.min.css': 'assets/css/sass/main.scss'
}]
},
dev: {
options: {
style: 'nested'
},
files: [{
'assets/css/main.css': 'assets/css/sass/main.scss'
}]
}
},
imageoptim: {
files: [
'img/flat',
'img/flat/**'
],
options: {
imageAlpha: true,
jpegMini: true,
quitAfter: true
}
},
uglify: {
options: {
mangle: true,
compress: true
},
jsCompress: {
files: {
'js/dist/main.min.js': ['js/src/script1.js', 'js/src/script2.js']
}
}
},
concat: {
options: {
separator: ';',
},
dist: {
src: ['js/src/script1.js', 'js/src/script2.js'],
dest: 'js/dist/main.min.js',
}
},
watch: {
sassWatch: {
files: 'css/sass/**/*.scss',
tasks: ['sass:prod', 'sass:dev'],
},
JsWatch: {
files: ['js/modules/script1.js', 'js/modules/script2.js'],
tasks: ['uglify:jsCompress', 'concat:dist'],
}
},
notify: {
sassNotify: {
options: {
title: "Task Complete",
message: "Your Sass has been compiled and concatanatated!"
}
},
jsNotify: {
options: {
title: "Task Complete",
message: "Your JS has been minified and concatanatated!"
}
},
imageNotify: {
options: {
title: "Task Complete",
message: "All images have been compressed"
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-imageoptim');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-notify');
grunt.registerTask('sassNotify', 'watch:sassWatch');
grunt.registerTask('jsNotify', 'watch:jsWatch');
grunt.registerTask('imageNotify', 'imageoptim');
};
Does anyone have an idea why this is happening?
有谁知道为什么会这样?
Kind Regards,
亲切的问候,
B!
乙!
回答by James Harrington
My problem was that it was watching to much i had 100+ image files that it was keeping track of so i just specified what it should watch (js, html, scss, css).
我的问题是它正在观看太多我有 100 多个图像文件正在跟踪,所以我只是指定了它应该观看的内容(js、html、scss、css)。
回答by Kenjamin
I ran into the issue and was looking for an answer. In my case I had a typo in my files:
matching pattern which caused it to not find the directory to watch and abort, although an error message would have been nice. I notice that in your sass
task you prepend your files withassets/
but not in your watch task.
我遇到了这个问题并正在寻找答案。在我的情况下,我的files:
匹配模式中有一个错字,导致它找不到要监视的目录并中止,尽管错误消息会很好。我注意到,在您的sass
任务中,您在文件之前添加了文件,assets/
但没有在 watch 任务中添加。
I understand you've likely moved on but hopefully this helps someone in the future.
我知道你可能已经离开了,但希望这对未来的人有所帮助。
回答by Patrick Burtchaell
Try something like
尝试类似
....
watch: {
dev: {
files: 'assets/sass/**/*{.scss,.js}',
tasks: ['sass:dev','uglify:jsCompress'],
},
},
...
});
grunt.registerTask('dev', 'watch:dev');
The watch task in my gruntfilemay be helpful.
我的 gruntfile 中的watch 任务可能会有所帮助。
回答by badsyntax
I noticed this was not working as expected with an older version of grunt-contrib-watch
. In my case, I was using grunt-contrib-watch
V0.4.4.
我注意到这在旧版本的grunt-contrib-watch
. 就我而言,我使用的是grunt-contrib-watch
V0.4.4。
Try updating the grunt-contrib-watch
package to the latest version, by running:
尝试grunt-contrib-watch
通过运行以下命令将软件包更新到最新版本:
npm install grunt-contrib-watch@latest --save-dev
and then try execute your task again.
然后再次尝试执行您的任务。
回答by Krasimir
Here is how my Gruntfile.js looks like and it works ok:
这是我的 Gruntfile.js 的样子,它工作正常:
module.exports = function(grunt) {
grunt.initConfig({
sass: { // Task
dist: { // Target
options: { // Target options
style: 'expanded'
},
files: { // Dictionary of files
'css/styles.css': './sass/site.scss'
}
}
},
watch: {
sass: {
files: ['sass/**/*.scss'],
tasks: ['sass']
},
organic: {
files: ['../organic-css/**/*.scss'],
tasks: ['sass']
}
}
});
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['sass', 'watch']);
}
Maybe you need to register a "default" tasks.
也许您需要注册一个“默认”任务。