Grunt Watch 任务不触发 Uglify 任务

Grunt Watch Task Not Triggering Uglify Task

我是 运行 一个非常简单的 Grunt 工作流程,在 MAMP 中运行。我正在使用监视任务来触发 Sass 任务,该任务运行两个子任务(将展开和压缩的样式表放在不同的目录中)和 minifying/concatenating JavaScript 文件。 Sass 任务正常运行,但 Watch 任务不会看到任何 JavaScript 文件更改来触发 Uglify 任务。这是我的 Grunt.js file

module.exports = function(grunt){
    grunt.initConfig({
        pkg : grunt.file.readJSON('package.json'),

        //Sass task
        sass:{
            dev: {
                options: {
                    style: 'expanded',
                    sourcemap: 'none'
                },
                files: {
                    //Destination, Source
                    'human-readable/style-expanded.css': 'sass/style.scss',
                    'human-readable/bootstrap-min.css': 'sass/bootstrap.scss'
                }
            },
            prod:{
                options:{
                    style: 'compressed',
                    sourcemap: 'none'
                },
                files:{
                    //Destination, Source
                    'style.css': 'sass/style.scss',
                    'css/bootstrap.min.css': 'sass/bootstrap.scss'
                }
            }
        },

        //Uglify task
        uglify:{
            options:{
                mangle: false,
                screwIE8: true
            },
            my_target:{
                files:{
                    //Destination, Source
                    'js/main.min.js': ['human-readable/main.js','js/bootstrap.min.js']
                }
            }
        },

        //Watch task
        watch: {
            css: {
                files: '**/*.scss',
                tasks: ['sass']
            },
            scripts: {
                files: '**/*.js',
                tasks: ['uglify']
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.registerTask('default', ['watch']);

}

想法?

可能是你试图同时看两件事,这可能是问题所在,试试 grunt-concurrent 模块,它允许你 运行 g运行t同时/并行任务:

grunt.loadNpmTasks('grunt-concurrent');
grunt.initConfig({
    ...
    concurrent: {
        tasks: ['watch:css', 'watch:script']
    }
    ...

grunt.registerTask('default', ['concurrent']);
...