grunt watch多次执行任务

时间:2014-07-10 10:52:39

标签: javascript gruntjs grunt-contrib-watch

我有以下Grunfile.js文件:

module.exports = function(grunt) {

    // Project configuration.
    grunt.initConfig({

        pkg: grunt.file.readJSON('package.json'),

        sass: {
            dist: {
                options: {
                    style: 'compressed'
                },
                files: {
                    'css/build/style.css': 'css/style.scss'
                }
            }
        },

        watch: {
            stylesheets: {
                files: ['css/*.scss'],
                tasks: ['newer:sass']
            }
        }

    });

    // Load the plugin that compiles sass
    grunt.loadNpmTasks('grunt-contrib-sass');

    // watch for, and run grunt if files change
    grunt.loadNpmTasks('grunt-contrib-watch');

    // Plugin for Grunt tasks to run with newer files only.
    grunt.loadNpmTasks('grunt-newer');

    // Grunt Tasks:
    grunt.registerTask('default', ['newer:sass']);

};

运行grunt watch并保存我的scss文件后,控制台输出如下:

Running "watch" task
Waiting...

>> File "css\style.scss" changed.
Running "newer:sass" (newer) task

Running "newer:sass:dist" (newer) task

Running "sass:dist" (sass) task
File css/build/style.css created.

Running "newer-postrun:sass:dist:1:D:\xampp\htdocs\grunt-test\node_modules\grunt-newer\.cache" (newer-postrun) task

Done, without errors.

问题是,每次都会编译.scss文件。即使没有变化。

我不明白为什么grunt正在运行3个任务(更新:sass,更新:sass:dist,sass:dist),而不是只运行watch(newer:sass)下定义的任务。

希望有人有想法解决这个问题。 :)

1 个答案:

答案 0 :(得分:0)

我不肯定这是问题,但请尝试指定您不想运行所有sass,只需sass:dist。所以请尝试:grunt.registerTask('default', ['newer:sass:dist']);