grunt-contrib-watch slow even with spawn = false

核能气质少年 提交于 2019-12-13 01:28:12

问题


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?

  1. install grunt-este-watch

    npm install grunt-este-watch --save-dev
    
  2. change contrib watch

    grunt.loadNpmTasks('grunt-contrib-watch');
    

    to este watch

    grunt.loadNpmTasks('grunt-este-watch');
    
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!