grunt-contrib-watch: High CPU usage

Hi, Grunt Watch consumes about 65-70% CPU Resources.

my devDependencies are as follows.

  "devDependencies": {
    "grunt": "^0.4.5",
    "grunt-contrib-uglify": "^0.9.1",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-sass": "^0.18.1",
    "load-grunt-tasks": "^3.1.0",
    "node-bourbon": "^4.2.1-beta1"
  }

my gruntfile:

var path = "C:/gdrive/apps/3oak/wp-content/themes/3flooring/";
module.exports = function(grunt) {
    require('load-grunt-tasks')(grunt); // npm install --save-dev load-grunt-tasks
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        // Watch ===========
        watch: {
            // Sass -----
            sass: {
                files: [
                    path + 'components/sass/*.{scss,sass}',
                    path + 'components/sass/styles/*.{scss,sass}',
                    path + 'components/sass/styles/custom/*.{scss,sass}',
                    path + 'components/sass/styles/custom/**/*.{scss,sass}'
                    ],
                tasks: ['sass']
            },
            // Reload -
            livereload: {
                files: [
                    path + '*.php',
                    path + '**/*.php',
                    path + '**/**/*.php',
                    path + 'js/*.{js,json}',
                    path + 'js/**/*.{js,json}',
                    path + 'components/sass/*.{scss,sass}',
                    path + 'components/sass/**/*.{scss,sass}',
                    path + 'components/sass/**/**/*.{scss,sass}'],
                options: {
                    livereload: true
                }
            },
            // Scripts -
            scripts: {
                files: [
                    path + 'components/js/*.{js,json}'
                    ],
                tasks: ['uglify']
            }, // scripts
        },
        sass: {
            dist: {
                options: {
                    sourceMap: true,
                    includePaths: [
                        require('node-bourbon').includePaths,
                        path + 'components/sass/*.{scss,sass}',
                        path + 'components/sass/**/*.{scss,sass}'
                    ],
                    outputStyle: 'nested',
                    lineNumbers: true,
                },
                files: [
                    {src: path + 'components/sass/style.scss', dest: path + 'style.css'}
                ],
            }
        },
        // Uglify 
        uglify: {
            my_target: {
                options: {
                    sourceMap: true,
                    sourceMapName: path + 'scripts.min.map'
                },
                files: [
                    {src: path + 'components/js/*.js', dest: path + 'js/scripts.min.js'}
                ],
            }
        }
    });
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.registerTask('default', 'watch');
};

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Comments: 24 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Have you tried this

options: { interval: 1000 },

Switching to https://github.com/JimRobs/grunt-chokidar (forked off of grunt-contrib-watch but uses chokidar instead of gaze for file system watching) drops my CPU usage from ~25% to somewhere near 0% with the same functionality.

@shama @tkellen Would you consider a pull request to switch the underlying lib from gaze to chokidar?

I’m surprised this issue hasn’t gotten more attention. Tons of web developers are at coffee shops with no power and here grunt-watch-contrib is eating up battery.

FWIW chokidar is pretty horrible on Windows and as proof VSCode stopped using it years ago on windows and wrote their own. The do use chokidar on Linux and Mac or did last time I checked. For Windows they wrote some small C# program and read its streamed output

https://github.com/microsoft/vscode/tree/master/src/vs/platform/files/node/watcher/win32

We ended up switching away from grunt-contrib-watch-based file watching to use pm2 watchers (which use chokidar under the hood) since we had to turn up the AC in the office to compensate for all the warm computers in the room caused by this issue 😬

@vasilii-b grunt.option() is an interface for command line options. So grunt.option('stack', true) in your Gruntfile is effectively the same as grunt --stack.

interval is a grunt-contrib-watch task option that is defined in the options of the task itself: http://gruntjs.com/configuring-tasks#options

Unfortunately your above Gruntfile is hiding the config so I’m not sure where that custom Gruntfile implementation expects watch task options. Typically with Gruntfiles, we recommend being explicit with the config:

grunt.initConfig({
  watch: {
    options: {
      // Options for the watch task go here
      interval: 5000
    },
    stuff: {
      files: ['src/*.js'],
      tasks: ['concat']
    }
  }
});