问题
I have set up the grunt-contrib-watch task to copy a list of files to a "dist" directory every time I save one of the files of the "src" directory. Unfortunately it takes 7 to 9 seconds to accomplish this task.
I have heard about the "spawn" option for grunt-contrib-watch. Using load-grunt-tasks to load the config of each task from a separate JSON file, I changed my watch.json so that it looks like this :
{
"service": {
"files": [
"src/*.php"
],
"tasks": [
"copy:service"
],
"options": {
"spawn": "false",
"livereload": "true"
}
}
}
...but setting it to false doesn't seems to change anything : it still takes 7 to 9 seconds to run. I installed time-grunt to monitor the task timing, here is what I got when saving a file :
When saving a file, I got the following output :
Waiting...
>> File "src\myfile.php" changed.
Running "copy:service" (copy) task
Created 7 directories, copied 120 files
Done, without errors.
Execution Time (2015-06-04 11:38:23 UTC)
loading tasks 333ms ██████████████████ 40%
copy:service 490ms ██████████████████████████ 60%
Total 823ms
Completed in 7.105s at Thu Jun 04 2015 13:38:24 GMT+0200 (W. Europe Daylight Time)
So it looks like the task in itself took less than a second, meaning that Grunt itself would take 6 seconds to load ? That seems pretty high. I'm on Windows 7, I've heard that on Windows there could be some performance issues.
回答1:
Same problem here, after changed and execute task all modules was reload.
But i found a very good solution on github (https://github.com/steida/grunt-este-watch)
What's wrong with official grunt-contrib-watch?
It's slow and buggy, because it uses combination fs.fileWatch and fs.watch, for historical reason. From Node 0.9.2+, fs.watch is ok.
What to do?
install grunt-este-watch
npm install grunt-este-watch --save-dev
change contrib watch
grunt.loadNpmTasks('grunt-contrib-watch');
to este watch
grunt.loadNpmTasks('grunt-este-watch');
change task
watch: { javascript: { files: 'src/js/**/*', tasks: ['uglify'] } }
to
esteWatch: { options: { dirs: ['../src/**/*'] }, 'js': function(filepath) { return 'uglify' } }
来源:https://stackoverflow.com/questions/30643176/grunt-contrib-watch-slow-even-with-spawn-false