[Grunt + AngularJS] Using ng-annotate for min-safe AngularJS

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   color   ar   sp   for   

When you minify your code with a tool like Uglify, the resulting minified file will rename variables. This is a problem for AngualrJS, which uses parameter names to provide injected dependencies. You could use the array notation manually, but no human should ever have to suffer this fate, or you could use ng-annotate with Grunt, and let your helper robots get the job done instead.

Without annotations:

angular.module("MyMod").controller("MyCtrl", function($scope, $timeout) {});

With annotations:

angular.module("MyMod").controller("MyCtrl", ["$scope", "$timeout", function($scope, $timeout) {}]);

The problem with Uglify:

angular.module("MyMod").controller("MyCtrl", function($scope, $timeout) {});to:anuglar.module("MyMode").controller("MyCtrl", function(a,b){});

It will rename the injection, but AnularJS Don‘t know what is a and b, so it will cause problem.

If we usse annotation first then ufligy the code:

After annotation:angular.module("MyMod").controller("MyCtrl", ["$scope", "$timeout", function($scope, $timeout) {}]);After Uglify:angular.module("MyMod").controller("MyCtrl", ["$scope","$timeout", function(a,b){}]);

Uglify will still rename the injectionm, but with annotation, angularjs know what a and b are, so won‘t cause problem.

 

 

Install:

 

npm install grunt-ng-annotate --save-dev

Read More: https://www.npmjs.org/package/grunt-ng-annotate

 

Code:

 

/** * Created by Answer1215 on 11/16/2014. */module.exports = function(grunt) {    grunt.initConfig({        stylus:{            compile:{                options: {                    compress: false                },                files: {                    "app/css/app.css": "styl/app.styl"                }            }        },        watch:{            stylus:{                files: [‘styl/**/*.styl‘],                tasks: [‘stylus:compile‘]            },            css:{                options: {livereload: true},                files: [‘app/css/**.css‘]            },            html:{                options: {livereload: true},                files: [‘**.html‘]            },            script: {                options: {livereload: true},                files: [‘app/js/**.js‘]            }        },        concat:{            options: {                separator: ‘;‘            },            js:{                src: [‘bower_components/angular/angular.min.js‘, ‘build/temp/app.js‘, ‘build/temp/**.js‘],                dest: "build/app.js"            }        },        uglify: {            js: {                src: ["build/app.js"],                dest: "build/app.min.js"            }        },        clean: {            build: ‘build‘,  //clean the build directory            temp: ‘build/temp‘        },        ngAnnotate:{            options: {                // Task-specific options go here.                singleQuotes: true            },            app:{                files: {                    // Target-specific file lists and/or options go here.                    ‘build/temp/app.js‘: [‘app/js/app.js‘],                    ‘build/temp/one.js‘: [‘app/js/one.js‘],                    ‘build/temp/two.js‘: [‘app/js/two.js‘]                }            }        }    });    grunt.registerTask(‘build‘, [‘clean:build‘, ‘ngAnnotate‘, ‘concat‘, ‘uglify‘,‘clean:temp‘]);    grunt.loadNpmTasks(‘grunt-contrib-watch‘);    grunt.loadNpmTasks(‘grunt-contrib-stylus‘);    grunt.loadNpmTasks(‘grunt-contrib-concat‘);    grunt.loadNpmTasks(‘grunt-contrib-uglify‘);    grunt.loadNpmTasks(‘grunt-contrib-clean‘);    grunt.loadNpmTasks(‘grunt-ng-annotate‘);}

 

[Grunt + AngularJS] Using ng-annotate for min-safe AngularJS

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.