博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Grunt + AngularJS] Using ng-annotate for min-safe AngularJS
阅读量:7117 次
发布时间:2019-06-28

本文共 3487 字,大约阅读时间需要 11 分钟。

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  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: 

 

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');}

 

转载地址:http://penel.baihongyu.com/

你可能感兴趣的文章
教你使用squid搭配dante做代理
查看>>
ecshop调用文章显示上一篇下一篇_无需整理
查看>>
cisco ***笔记
查看>>
执行php程序的时候,报错Allowed memory size of 134217728 bytes exhausted (tried to allocate 83 bytes)...
查看>>
春Phone计划 51cto沙龙上海站
查看>>
HDFS体系结构详解
查看>>
我的友情链接
查看>>
[转]我对CTO的理解
查看>>
RH413-Linux系统下umask测试
查看>>
MYSQLmy-innodb-heavy-4G.cnf配置文件注解
查看>>
HTML5 Audio/Video 标签,属性,方法,事件汇总
查看>>
Android 学习笔记【基础扫盲篇】
查看>>
shiro filter
查看>>
重新排列数字使其刚好比当前值大 Next Greater Element III
查看>>
tomcat虚拟子目录设置
查看>>
C++中sizeof详解
查看>>
elasticsearch集群部署
查看>>
我的友情链接
查看>>
Exchange 2010 OWA更改过期密码
查看>>
我的友情链接
查看>>