M
M
Mike2014-12-11 10:22:54
Grunt.js
Mike, 2014-12-11 10:22:54

Why does grunt-contrib-watch go into an infinite loop?

Hello colleagues!
I recently decided to master the Node.js + Grunt bundle. And everything works out for me. I am glad that now I can use SASS, that routine processes are automated, but my existence is overshadowed by the "grunt-contrib-watch" package, which I cannot get to work as I want. Help me please!
Here is the call to watch in Gruntfile.js:

watch: {
    options: {
    livereload: true,
  },
  scripts: {
        files: ['js/*.js'],
        tasks: ['concat', 'uglify'],
        options: {
            spawn: false
        }
    },
  css: {
    files: ['scss/*.scss','css/*.css',],
    tasks: ['sass','cssmin'],
        options: {
            spawn: false
        }
  }
}

Here are the modules loading:
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-watch');

Here are the tasks:
grunt.registerTask('default', ['concat', 'uglify', 'sass', 'cssmin', 'watch']);

It does not matter if LiveReload is active (for OS X). When you change, for example, a .sass file in the console, madness begins, which continues an infinite number of times.
MacBook-Pro-Mihail:dev mihailhlebnikov$ grunt
Running "concat:dist" (concat) task
File js/build/production.js created.

Running "uglify:build" (uglify) task
>> 1 file created.

Running "sass:dist" (sass) task

Running "cssmin:combine" (cssmin) task
File css/production.min.css created: 40.35 kB → 30.84 kB

Running "watch" task
Waiting...OK
>> File "scss/main.scss" changed.
Running "sass:dist" (sass) task

Running "cssmin:combine" (cssmin) task
File css/production.min.css created: 40.35 kB → 30.84 kB

Done, without errors.

Completed in 2.074s at Thu Dec 11 2014 10:22:23 GMT+0300 (MSK) - Waiting...OK
>> File "css/normalize.css" changed.
>> File "css/production.css" changed.
>> File "css/production.min.css" changed.
Running "sass:dist" (sass) task

Running "cssmin:combine" (cssmin) task
File css/production.min.css created: 40.35 kB → 30.84 kB

Done, without errors.

Completed in 1.875s at Thu Dec 11 2014 10:22:25 GMT+0300 (MSK) - Waiting...OK
>> File "css/normalize.css" changed.
>> File "css/production.css" changed.
>> File "css/production.min.css" changed.
Running "sass:dist" (sass) task

Running "cssmin:combine" (cssmin) task
File css/production.min.css created: 40.35 kB → 30.84 kB

Done, without errors.

Completed in 1.861s at Thu Dec 11 2014 10:22:28 GMT+0300 (MSK) - Waiting...OK
>> File "css/normalize.css" changed.
>> File "css/production.css" changed.
>> File "css/production.min.css" changed.
Running "sass:dist" (sass) task

The entire Gruntfile.js file:
//Стандартный экспорт модуля в nodejs
module.exports = function(grunt) {
  // Инициализация конфига GruntJS
  grunt.initConfig({

    //Склеивание JS файлов
    concat: {
        dist: {
            src: [
            	'js/vendor/jquery-2.1.1.min.js',
            	'js/vendor/modernizr-2.6.2.min.js',
            	'js/jquery.mousewheel.min.js',
                'js/jquery.bxslider.min.js',
                'js/plugins/jquery.easing.1.3.js',
                'js/plugins/jquery.fitvids.js',
                'js/jquery.fancybox.pack.js',
                'js/main.js',
                'js/plugins.js'
            ],
            dest: 'js/build/production.js'
        }
    },

    // Сжатие общего JS файла
    uglify: {
        build: {
            src: 'js/build/production.js',
            dest: 'js/build/production.min.js'
        }
    },

    // SASS
    sass: {
      dist: {
        files: {
          'css/production.css' : 'scss/main.scss',
          'css/normalize.css' : 'scss/normalize.scss'
        }
      }
    },

    // CSS Min
    cssmin: {
          combine: {
            files: {
              'css/production.min.css': [
                'css/production.css',
                'css/normalize.css',
                'css/jquery.bxslider.css',
                'css/jquery.fancybox.css'
                ]
            }
          }
        },

    watch: {
        options: {
        livereload: true,
      },
      scripts: {
            files: ['js/*.js'],
            tasks: ['concat', 'uglify'],
            options: {
                spawn: false
            }
        },
      css: {
        files: ['scss/*.scss','css/*.css',],
        tasks: ['sass','cssmin'],
            options: {
                spawn: false
            }
      }
    }

  });

  // Загрузка модулей, которые предварительно установлены
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-sass');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-watch');

  // Эти задания будут выполнятся сразу же когда вы в консоли напечатание grunt, и нажмете Enter
  grunt.registerTask('default', ['concat', 'uglify', 'sass', 'cssmin', 'watch']);
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aen, 2014-12-11
@ddale

There is a suspicion that the problem is in the line:
Watcher checks that some *.css file has been changed, which, as I suspect, is just obtained during the scss build and restarts the build.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question