Grunt Uglify一些脚本

时间:2014-05-22 23:07:51

标签: javascript angularjs gruntjs grunt-contrib-uglify

我想从我的angularjs项目中获得一些特定的js。 我不想要uglify al js path。

我试过这个

 src : 'www/js/app.js', 'www/js/controllers.js', 'www/js/directives.js', 'www/js/factories.js', 'www/js/filters.js', 'www/js/services.js',

和这个

src : 'www/js/app.js, www/js/controllers.js, www/js/directives.js, www/js/factories.js, www/js/filters.js, www/js/services.js',

而不是运气。

module.exports = function(grunt){
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks("grunt-contrib-uglify");

    grunt.initConfig({
        clean : [ 'www/dist/*' ],
        uglify : {
            options : {
                report : 'min',
                mangle : true
            },
            my_target : {
                files : [ {
                    src : 'www/js/app.js, www/js/controllers.js, www/js/directives.js, www/js/factories.js, www/js/filters.js, www/js/services.js',
                    dest : 'www/dist/app.realease.min.js'
                } ]
            }
        }
    })

    grunt.registerTask('default', ['clean', 'uglify']);
}

如果有人可以帮助我,我会过分帮助你。

感谢。

1 个答案:

答案 0 :(得分:1)

您的问题是您提供了一个简单的字符串列表文件,您应该为每个要定位的文件提供一个字符串数组。

src : 'www/js/app.js', 'www/js/controllers.js', 'www/js/directives.js', 'www/js/factories.js', 'www/js/filters.js', 'www/js/services.js'

应该成为

src : ['www/js/app.js', 'www/js/controllers.js', 'www/js/directives.js', 'www/js/factories.js', 'www/js/filters.js', 'www/js/services.js']

但对我来说,你应该将你的文件汇总到一个,然后像这样对它进行uglify:

module.exports = function(grunt) {

    grunt.initConfig({

        clean : [ 'www/dist/*' ],
        concat: {
            options: {
                separator: ';'
            },
            dist: {
                src: ['www/js/app.js', 'www/js/controllers.js', 'www/js/directives.js', 'www/js/factories.js', 'www/js/filters.js', 'www/js/services.js'],
                dest: 'www/dist/app.release.js'
            }
        },
        uglify: {
            options: {
                mangle: false
            },
            dist: {
                files: {
                    'dist/app.release.min.js': ['<%= concat.dist.dest %>']
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-concat');

    grunt.registerTask('default', ['clean', 'concat', 'uglify']);

};