[Grunt + AngularJS] Using ng-annotate for min-safe AngularJS
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'); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具