[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');
}
[Grunt + AngularJS] Using ng-annotate for min-safe AngularJS的更多相关文章
- [后端人员耍前端系列]AngularJs篇:30分钟快速掌握AngularJs
一.前言 对于前端系列,自然少不了AngularJs的介绍了.在前面文章中,我们介绍了如何使用KnockoutJs来打造一个单页面程序,后面一篇文章将介绍如何使用AngularJs的开发一个单页面应用 ...
- 【AngularJS】—— 3 我的第一个AngularJS小程序
通过前面两篇的学习,基本上对AngularJS的使用有了一定的了解. 本篇将会自己手动写一个小程序,巩固下理解. 首先要注意的是,引用AngularJS的资源文件angular.min.js文件. 由 ...
- Angularjs,WebAPI 搭建一个简易权限管理系统 —— Angularjs 前端主体结构(五)
目录 前言 Angularjs名词与概念 Angularjs 基本功能演示 系统业务与实现 WebAPI项目主体结构 Angularjs 前端主体结构 6 Angularjs 前端主体结构 6.1 A ...
- Angularjs,WebAPI 搭建一个简易权限管理系统 —— Angularjs名词与概念(一)
目录 前言 Angularjs名词与概念 Angularjs 基本功能演示 系统业务与实现 WebAPI项目主体结构 Angularjs 前端主体结构 2. 前言 Angularjs开发CRUD类型的 ...
- 【js类库AngularJs】web前端的mvc框架angularjs之hello world
AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为核 ...
- 走进AngularJs(八) ng的路由机制
在谈路由机制前有必要先提一下现在比较流行的单页面应用,就是所谓的single page APP.为了实现无刷新的视图切换,我们通常会用ajax请求从后台取数据,然后套上HTML模板渲染在页面上,然而a ...
- 走进AngularJs(二) ng模板中常用指令的使用方式
通过使用模板,我们可以把model和controller中的数据组装起来呈现给浏览器,还可以通过数据绑定,实时更新视图,让我们的页面变成动态的.ng的模板真是让我爱不释手.学习ng道路还很漫长,从模板 ...
- AngularJS学习笔记(一)走近AngularJS
什么是AngularJS AngularJS是一款优秀的前端JS框架,是Google多款产品之一,简称ng. ng有着诸多特性,最为核心的是:MVVM.模块化.自动化双向数据绑定.语义化标签.依赖注入 ...
- angularjs ocLazyLoad分步加载js文件,angularjs ocLazyLoad按需加载js
用angular有一段时间了,平日里只顾着写代码,没有注意到性能优化的问题,而今有时间,于是捋了捋,讲学习过程记录于此: 问题描述:由于采用angular做了网页的单页面应用,需要一次性在主布局中将所 ...
- [AngularJS] Write a simple Redux store in AngularJS app
The first things we need to do is create a reducer: /** * CONSTANT * @type {string} */ export const ...
随机推荐
- socketserver用法列子
socketserver socketserver内部使用IO多路复用以及“多线程”和“多进程”,从而实现并发处理多个客户端请求的scoket服务端.即,每个客户端请求连接到服务器时,socket服务 ...
- Redis学习篇(十一)之发布订阅
PUBLISH/SUBSCRIBE 发布订阅的原理 包含两个角色,一个是发布者, 一个是订阅者 订阅者可以订阅一个或者多个频道(channel) 发布者可以向指定的频道发布信息 通过SUBSCRIBE ...
- GC参数
串行收集器 串行收集器(Serial),是一个相对比较老的回收器,但是它的效率在回收器中相对较好,并且比较稳定.他在进行垃圾回收的过程中,使得应用暂时被挂起,然后启用单条线程去做垃圾回收,所以在进行垃 ...
- 【BZOJ 1566】 1566: [NOI2009]管道取珠 (DP)
1566: [NOI2009]管道取珠 Time Limit: 20 Sec Memory Limit: 650 MBSubmit: 1659 Solved: 971 Description In ...
- [JSOI2007]文本生成器 --- AC自动机 + DP
[JSOI2007]文本生成器 题目描述: JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文本生成器v6版. 该软件可以随机 ...
- [转]应用服务器ApacheSocketUnixthread
HttpClient容易忽视的细节——连接关闭 博客分类: java HttpClient client = new HttpClient(); HttpMethod method = new G ...
- js——引用类型和基本类型
js中的数据类型有以下几种: 基本类型:Number Boolean String undefined null Symbol 引用类型:Object(Array, Function, Date ...
- loading(正在加载特效)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- tomcat JVM内存 配置
原文:http://elf8848.iteye.com/blog/467460 常见的内存溢出有以下两种: java.lang.OutOfMemoryError: PermGen space java ...
- Birt中实现字段拆分为表的还有一种方法
来源: http://developer.actuate.com/community/forum/index.php? /topic/36204-split-data-row/. 将字段拆分为 ...