Grunt - Watch任务无法正常工作

4

我的项目grunt文件中的观察任务似乎启动然后立即退出。

请查看附加的屏幕截图。

请找到我的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');

};

有人知道这个为什么会发生吗?

谢谢,

B!

enter image description here


JsWatch和jsWatch是两个不同的东西。你的gruntfile中是否也存在这种差异?虽然我认为这不会导致你的问题,但我认为更新watch是你首要解决的问题。 - Dawn
  1. 从grunt [api](http://gruntjs.com/api/grunt.task)中窃取
任务列表参数必须是任务数组。
  1. 你能验证文件更改是否会触发任务吗?即插入新行并保存它。
  2. 据我所知,在监视上指定不存在的任务不会引发错误。
- Adi Prasetyo
5个回答

6

我的问题是它监视了太多内容,我有100多个图像文件需要跟踪,所以我只指定了它应该监视的内容(js、html、scss、css)。


2
尝试类似以下内容:
   ....

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

    },

    ...

});

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

我 gruntfile 中的watch 任务可能会有所帮助。


你能否更新链接或者在回答中直接发布你的gruntfile,Patrick?我很想看整个文件 :) - chriszo111

2
我遇到了问题,正在寻找答案。在我的情况下,我在files:匹配模式中有一个拼写错误,导致它无法找到要监视的目录并中止,尽管错误消息会很好。我注意到在你的sass任务中,你在文件前面加上assets/,但在你的监视任务中没有。

我明白你可能已经过去了,但希望这能帮助未来的某个人。


1

我注意到早期版本的grunt-contrib-watch无法按预期工作。在我的情况下,我使用的是grunt-contrib-watch V0.4.4。

尝试更新grunt-contrib-watch包到最新版本,运行以下命令:

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

然后尝试重新执行您的任务。

-3

这是我的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']);

}

也许你需要注册一个“默认”的任务。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接