在Grunt中运行缩小不能读取未定义的属性

时间:2016-07-13 07:23:31

标签: gruntjs minify

我正在尝试构建一个Grunt任务,该任务可以缩小我的JS文件并返回一个缩小的JS文件。

这是我的gruntfile.js文件:

    module.exports = function (grunt) {

        // Project configuration.
        grunt.initConfig({
            minified: {
                files: {
                    src: [
                    'js/*.js',
                    ],
                    dest: 'js/min/'
                },
                options: {
                    allinone: true
                }
            },
        });

        grunt.loadNpmTasks('grunt-minified');
    };

当我运行任务时,它确实有效,但它也会返回错误。

    > cmd.exe /c grunt -b "C:\Users\alucardu\documents\visual studio 2015\Projects\JS-demo\JS-demo" --gruntfile "C:\Users\alucardu\documents\visual studio 2015\Projects\JS-demo\JS-demo\Gruntfile.js" minified
    Running "minified:files" (minified) task
    Warning: Cannot read property 'yellow' of undefined Use --force to continue.
    Process terminated with code 3.
    Aborted due to warnings.

我在整个解决方案中为“黄色”做了搜索操作,但它没有返回任何结果。此外,当我清空我正在缩小的两个JS文件时,它仍然会返回错误。

有谁知道它为什么会返回此错误?

1 个答案:

答案 0 :(得分:0)

删除

options: {
  allinone: true
}

警告不再显示,但它也不会将文件连接在一起。所以我添加了另一个名为concat的任务。所以现在我的gruntfile看起来像这样:

    module.exports = function (grunt) {

        // Project configuration.
        grunt.initConfig({
            watch: {
                scripts: {
                    files: ['js/*.js'],
                    tasks: ['concat', 'minified', 'uglify'],
                },
            },
            concat: {
                dist: {
                    src: ['js/*.js'],
                    dest: 'js/min/concat.js'
                },
            },
            minified: {
                files: {
                    src: ['js/min/concat.js'],
                    dest: 'js/min/minified.js'
                },
            },
            uglify: {
                my_target: {
                    files: {
                        'js/min/uglify.js': ['js/min/minified.jsconcat.js']
                    }
                }
            },

        });

        grunt.loadNpmTasks('grunt-contrib-watch');
        grunt.loadNpmTasks('grunt-contrib-concat');
        grunt.loadNpmTasks('grunt-minified');
        grunt.loadNpmTasks('grunt-contrib-uglify');


    };

似乎工作正常。