简体   繁体   English

Grunt - Watch Task无效

[英]Grunt - Watch Task is not working

It seems like my watch task in my project grunt file is starting up then quitting straight away. 似乎我的项目grunt文件中的监视任务正在启动,然后立即退出。

Please review the screenshot attached. 请查看附带的屏幕截图。

Please find the code for my Gruntfile.js; 请找到我的Gruntfile.js的代码;

module.exports = function (grunt) {

    grunt.initConfig({
        sass: {
            options: {
                cacheLocation: '.sass-cache'
            },
            prod: {
                options: {
                    style: 'compressed'
                },
                files: [{
                    'assets/css/dist/main.min.css': 'assets/css/sass/main.scss'
                }]
            },
            dev: {
                options: {
                    style: 'nested'
                },
                files: [{
                    'assets/css/main.css': 'assets/css/sass/main.scss'
                }]
            }
        },
        imageoptim: {
          files: [
            'img/flat',
            'img/flat/**'
          ],
          options: {
            imageAlpha: true,
            jpegMini: true,
            quitAfter: true
          }
        },
        uglify: {
            options: {
              mangle: true,
              compress: true
            },
            jsCompress: {
                files: {
                    'js/dist/main.min.js': ['js/src/script1.js', 'js/src/script2.js']
            }
          }
        },
        concat: {
            options: {
                separator: ';',
          },
          dist: {
                src: ['js/src/script1.js', 'js/src/script2.js'],
                dest: 'js/dist/main.min.js',
          }
        },
        watch: {
            sassWatch: {
                files: 'css/sass/**/*.scss',
                tasks: ['sass:prod', 'sass:dev'],
            },
            JsWatch: {
                files: ['js/modules/script1.js', 'js/modules/script2.js'],
                tasks: ['uglify:jsCompress', 'concat:dist'],
            }
        },
        notify: {
            sassNotify: {
                options: {
                    title: "Task Complete",
                    message: "Your Sass has been compiled and concatanatated!"
                }
            },
            jsNotify: {
                options: {
                    title: "Task Complete",
                    message: "Your JS has been minified and concatanatated!"
                }
            },
            imageNotify: {
                options: {
                    title: "Task Complete",
                    message: "All images have been compressed"
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-imageoptim');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-notify');

    grunt.registerTask('sassNotify', 'watch:sassWatch');
    grunt.registerTask('jsNotify', 'watch:jsWatch');
    grunt.registerTask('imageNotify', 'imageoptim');

};

Does anyone have an idea why this is happening? 有谁知道为什么会这样?

Kind Regards, 亲切的问候,

B! 乙!

在此输入图像描述

我的问题是,它正在观看我有100多个图像文件,它正在跟踪,所以我只是指定它应该看什么(js,html,scss,css)。

I noticed this was not working as expected with an older version of grunt-contrib-watch . 我注意到这与旧版本的grunt-contrib-watch无法正常工作。 In my case, I was using grunt-contrib-watch V0.4.4. 就我而言,我使用的是grunt-contrib-watch V0.4.4。

Try updating the grunt-contrib-watch package to the latest version, by running: 尝试通过运行以下命令将grunt-contrib-watch包更新到最新版本:

npm install grunt-contrib-watch@latest --save-dev 

and then try execute your task again. 然后再次尝试执行您的任务。

I ran into the issue and was looking for an answer. 我遇到了这个问题,正在寻找答案。 In my case I had a typo in my files: matching pattern which caused it to not find the directory to watch and abort, although an error message would have been nice. 在我的情况下,我的files:有一个拼写错误files:匹配模式导致它找不到要监视和中止的目录,尽管错误消息本来不错。 I notice that in your sass task you prepend your files with assets/ but not in your watch task. 我注意到在你的sass任务中,你在文件前面加上assets/但不在你的监视任务中。

I understand you've likely moved on but hopefully this helps someone in the future. 我知道你可能会继续前进,但希望这有助于未来的某些人。

Try something like 尝试类似的东西

   ....

    watch: {
        dev: {
            files: 'assets/sass/**/*{.scss,.js}',
            tasks: ['sass:dev','uglify:jsCompress'],
        },

    },

    ...

});

grunt.registerTask('dev', 'watch:dev');

The watch task in my gruntfile may be helpful. 我的gruntfile中监视任务可能会有所帮助。

Here is how my Gruntfile.js looks like and it works ok: 这是我的Gruntfile.js的样子,它运行正常:

module.exports = function(grunt) {

    grunt.initConfig({
        sass: {                              // Task
            dist: {                            // Target
                options: {                       // Target options
                    style: 'expanded'
                },
                files: {                         // Dictionary of files
                    'css/styles.css': './sass/site.scss'
                }
            }
        },
        watch: {
            sass: {
                files: ['sass/**/*.scss'],
                tasks: ['sass']
            },
            organic: {
                files: ['../organic-css/**/*.scss'],
                tasks: ['sass']
            }
        }
    });

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

    grunt.registerTask('default', ['sass', 'watch']);

}

Maybe you need to register a "default" tasks. 也许您需要注册“默认”任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM