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

Source: Internet
Author: User

When you minify your code with a tool like uglify, the resulting minified file would rename variables. This was a problem for ANGUALRJS, which uses parameter names to provide injected dependencies. You could use the array notation manually, but no human shouldever has to suffer this fate, or do could use Ng -annotate with Grunt, and let your helper robots get the job done instead.

Without annotations:

Angular.module ("mymod"). Controller ("Myctrlfunction ($timeout) {});    

With annotations:

Angular.module ("mymod"). Controller ("Myctrl", ["$scope" $timeoutfunction ( $timeout) {}]);

The problem with uglify:

function ($scope, $timeout) {}); To:anuglar.module (thefunction(A, b) {});

It'll rename the injection, but anularjs don't know what's A and B, so it'll cause problem.

If we usse annotation first then ufligy the code:

After annotation:angular.module (function($scope, $timeout) {}]); After Uglify:angular.module (function(A, b) {}]);

Uglify would still rename the injectionm, but with annotation, angularjs know what A and B is, 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: {Comp Ress: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 directoryTemp: ' 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.