标签:
周末有点懒,跑去看了《智取威虎山》,手撕鬼子的神剧情节被徐老怪一条回忆线就解释过去了,牛到极致尽是这种四两拨千斤的处理方式,手撕加分,四星推荐。
-----------------------------闲话分割线-----------------------------
concat、cssmin、uglify对应的分别是合并、css压缩、js压缩混淆,至于为什么把这三个放在一起,在后面的usemin模块会给出解释。
separator: 就是源文件之间的分隔符,默认是grunt.util.linefeed,也就是换行符。如果源文件是js,而且之后需要进行uglify,将此option改为";"
(function(){ console.log(‘hello world‘); })();
hellogrunt.js
(function(){ console.log(‘hello grunt‘); })();
module.exports = function (grunt) { require(‘load-grunt-tasks‘)(grunt); var path = { dest: ‘dest/‘, src: ‘src/‘ }; grunt.initConfig({ gpath: path, concat: { test: { files: [ { src: ‘<%= gpath.src %>*.js‘, dest: ‘<%= gpath.src %>hello.js‘ } ] } } }); grunt.registerTask(‘default‘, [‘concat‘]); };
(function(){ console.log(‘hello grunt‘); })(); (function(){ console.log(‘hello world‘); })();
test: { options: { process: function(src, filepath) { return src.replace(/hello/g, ‘hi‘); } }, files: [ { src: ‘<%= gpath.src %>*.js‘, dest: ‘<%= gpath.dest %>hello.js‘ } ] }
目标文件
(function(){ console.log(‘hi grunt‘); })(); (function(){ console.log(‘hi world‘); })();
(function(){ if(<%= concat.test.options.process %>) { console.log(‘hello world‘); } })();
目标文件
(function(){ console.log(‘hello grunt‘); })(); (function(){ if(true) { console.log(‘hello world‘); } })();
需要注意的是,当process设为true时模板的Global对象是grunt.initConfig的传入参数,如果想修改Global对象,往下看
module.exports = function (grunt) { require(‘load-grunt-tasks‘)(grunt); var path = { dest: ‘dest/‘, src: ‘src/‘ }, global = { bool: true }; grunt.initConfig({ gpath: path, concat: { test: { options: { process: { data: global } }, files: [ { src: ‘<%= gpath.src %>*.js‘, dest: ‘<%= gpath.src %>hello.js‘ } ] } } }); grunt.registerTask(‘default‘, [‘concat‘]); };
源文件
(function(){ if(<%= bool %>) { console.log(‘hello world‘); } })();
输出的目标文件如5
//min Running "cssmin:test" (cssmin) task File dest/base.css created: 2.95 kB → 2.34 kB File dest/main.css created: 1.05 kB → 954 B //gzip Running "cssmin:test" (cssmin) task File dest/base.css created: 2.95 kB → 2.34 kB → 803 B (gzip) File dest/main.css created: 1.05 kB → 954 B → 428 B (gzip)
(function() { var hello = ‘hello ‘, world = ‘world‘; console.log(hello + world); })();
当不设置mangle时,目标文件helloworld.min.js
!function(){var a="hello ",b="world";console.log(a+b)}();
设置mangle为false时,目标文件
!function(){var hello="hello ",world="world";console.log(hello+world)}();
设置mangle为Object时,将不进行混淆的变量放置在"except"属性中
mangle: { except: [‘hello‘] }
目标文件
!function(){var hello="hello ",a="world";console.log(hello+a)}();
(function(){ var hello = ‘hello ‘, world = ‘world‘; DEBUG&&console.log(hello + world); DEBUG&&alert(hello + world); })();
开发环境下的Gruntfile.js
compress: { global_defs: { DEBUG: true } }
目标文件
!function(){var a="hello ",b="world";!0&&console.log(a+b),!0&&alert(a+b)}();
发布环境下的Gruntfile.js
compress: { global_defs: { DEBUG: false } }
由于所有的执行代码全部被取消,生成的目标文件为空文件
PS: 以上是为了演示global_defs的用处,如果要简单的去除console语句,可以直接用drop_console(默认false)属性
源文件
(function(){ var hello = ‘hello ‘, world = ‘world‘; console.log(hello + world); alert(hello + world); })();
Gruntfile.js
compress: { drop_console: true }
目标文件
!function(){var a="hello ",b="world";alert(a+b)}();
Gruntfile.js
compress: false, mangle: false, enclose: { ‘window.name‘: ‘name‘, }
源文件
(function() { var hello = ‘hello ‘, world = ‘world‘; console.log(hello + world + name); //name为外部变量 })();
目标文件
!function(name){!function(){var hello="hello ",world="world";console.log(hello+world+name)}()}(window.name);
mangle: false, wrap: ‘test‘
源文件
(function() { var hello = ‘hello ‘, world = ‘world‘; exports.name = hello + world; //exports为对外暴露的对象 console.log(window.test.name); //test为上面设置的wrap,对应exports })();
目标文件
!function(exports,global){global.test=exports,function(){var hello="hello ",world="world";exports.name=hello+world,console.log(window.test.name)}()}({},function(){return this}());
//注意不在匿名函数里面了 var hello = ‘hello ‘, world = ‘world‘; exports.name = hello + world; console.log(window.test.name);
Gruntfile.js
mangle: false, wrap: ‘test‘, exportAll: true
目标文件
!function(exports,global){global.test=exports;var hello="hello ",world="world";exports.name=hello+world,console.log(window.test.name),exports.hello=hello,exports.world=world}({},function(){return this}());
/*! * comment ‘all‘ ‘some‘ 保留 */ // @preserve preserve ‘all‘ ‘some‘ 保留 // @license license ‘all‘ ‘some‘ 保留 // @cc_on cc_on ‘all‘ ‘some‘ 保留 // @tarol ‘all‘ 保留 (function() { console.log(‘hello world‘); })();
Funtion传入参数arguments[1].value为注释内容,返回true则保留该注释
-----------------------------结尾分割线-----------------------------
心力憔悴,这样写有点累,下篇介绍clean和copy,怎么写看心情。
标签:
原文地址:http://www.cnblogs.com/tarol/p/4192242.html