grunt之concat、cssmin、uglify
周末有点懒,跑去看了《智取威虎山》,手撕鬼子的神剧情节被徐老怪一条回忆线就解释过去了,牛到极致尽是这种四两拨千斤的处理方式,手撕加分,四星推荐。
-----------------------------闲话分割线-----------------------------
concat、cssmin、uglify对应的分别是合并、css压缩、js压缩混淆,至于为什么把这三个放在一起,在后面的usemin模块会给出解释。
concat(V0.5.0)的options(github地址)
separator: 就是源文件之间的分隔符,默认是grunt.util.linefeed,也就是换行符。如果源文件是js,而且之后需要进行uglify,将此option改为";"
- banner: 目标文件的头部注释,默认是""
- footer: 目标文件的底部注释,默认是""
- stripBanners: 合并时是否删除源文件头部的注释,默认false
- process: 对目标文件进行内容替换,接受三种类型:Boolean、Funtion、Object。有两种替换模式,一种是设为function进行全部替换,一种是设为true或object进行模板(<% =var %>)替换。
- 新建两个文件,分别为helloworld.js
(function(){
console.log('hello world');
})();hellogrunt.js
(function(){
console.log('hello grunt');
})(); - 在Gruntfile.js中进行配置
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']);
}; - 以上是一个不设置process的concat任务,输出的目标文件
(function(){
console.log('hello grunt');
})();
(function(){
console.log('hello world');
})(); - 将process设置为Function,修改Gruntfile.js中的concat:test如下
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');
})(); - 将process设置为true,这样源文件中就可以使用模板了,Gruntfile.js不贴了,源文件如下
(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对象,往下看
- 将process设为Object,Global对象放在data属性中,Gruntfile如下
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
- sourceMap、sourceMapName、sourceMapStyle: 暂未研究,待以后补充
cssmin(V0.11.0)的options(github地址)
- report: 接受'min' and 'gzip',默认前者,生成的文件都是一样的(不知道是否和我环境是window有关),选择'gzip'时会汇报gzip文件的大小
//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)
uglify(V0.7.0)的options(github地址)
- mangle: 混淆,接受两种类型:Boolean、Object,默认值是{}
源文件helloworld.js(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)}(); - compress: 压缩,去除不必要的代码,接受两种类型:Boolean、Object,默认值是{}。当类型是Object时属性略多,贴出官方地址,以下仅演示global_defs属性的应用
开发时期下需要打log,而上线后需要将log去除,那么就可以用到compress的global_defs属性
源文件(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)}(); - beautify: 一般可理解为保留换行,接受两种类型:Boolean、Object,默认值是false。参数比较多,官方地址。
- expression: 将一段JSON字符串解析成一个标准的JSON,与mangle、compress冲突,默认为false
- enclose: 将js使用匿名函数wrap,与mangle冲突,接受Object,默认为undefined
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); - wrap: 将js使用匿名函数wrap,并对外暴露一个对象作为接口,与mangle冲突,接受String,默认为undefined
Gruntfile.jsmangle: 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}()); - maxLineLen: 每行限制长度,为0取消限制,接受Number,默认为32000
- ASCIIOnly: 将js中的非ASCII字符用unicode表示,默认为false
- exportAll: 将js中的所有变量自动添加到exports
源文件//注意不在匿名函数里面了
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}()); - preserveComments: 保留注释相关,接受false、'all'、'some'、Function
/*!
* 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则保留该注释
- banner、footer: 略
- sourceMap、sourceMapName、sourceMapIn、sourceMapIncludeSources: 关于压缩后的.map文件的选项,关于.map文件,详情可见阮一峰老师的BLOGsourceMap: 设置为true则生成.map文件,默认为false
sourceMapName: 重新定义.map文件的名字,压缩后的js中对.map的引用会同时更新
sourceMapIn: 涉及Coffee,不懂,不妄言
sourceMapIncludeSouces: 是否在.map文件中添加源文件到sourcesContent属性
-----------------------------结尾分割线-----------------------------
心力憔悴,这样写有点累,下篇介绍clean和copy,怎么写看心情。
grunt之concat、cssmin、uglify的更多相关文章
- grunt使用小记之uglify:最全的uglify使用DEMO
grunt-contrib-uglify uglify是一个文件压缩插件,项目地址:https://github.com/gruntjs/grunt-contrib-uglify 本文将以一个DEMO ...
- 闲话和grunt
一年半没更新是因为自己转岗了,android framework+system转前端,可以想象过程之苦逼,苦成了一首诗:很烦很烦/非常烦/非常非常十分烦/特别烦特烦/极其烦/贼烦/简直烦死了/啊——. ...
- 配置grunt进行css、js的检查、合并和压缩
现在会进行代码的合并和压缩已成为前端人员的必备知识,那么现在来介绍一个grunt的工具.grunt是个风靡世界的工具,它的首页是 http://www.gruntjs.net 这是个中文网站,有文档 ...
- grunt压缩合并代码
module.exports = function(grunt) { // 配置 grunt.initConfig({ pkg : grunt.file.readJSON('package.json' ...
- 开箱即用 - Grunt合并和压缩 js,css 文件
js,css 文件合并与压缩 Grunt 是前端自动化构建工具,类似webpack. 它究竟有多强悍,请看它的 介绍. 这里只演示如何用它的皮毛功能:文件合并与压缩. 首先说下js,css 合并与压缩 ...
- grunt之filerev、usemin
窃以为这两个插件是比较有用的,filerev是给js.css进行编码重命名,usemin修改html中被重命名的js.css文件的引用.另外说明下之前将concat.cssmin.uglify放在一篇 ...
- Grunt入门学习之(3) -- Gruntfile具体示例
经过前面的学习,将测试的Gruntfile整合在一起! /** * Created by Administrator on 2017/6/22. */ module.exports = functio ...
- Grunt Part 2
Objectives and Outcomes In this exercise, you will continue to learn to use Grunt, the task runner. ...
- 前端自动化构建工具Grunt
一.了解Gurnt(http://www.open-open.com/lib/view/open1433898272036.html) Grunt 是一个基于任务的JavaScript工程命令行构建工 ...
随机推荐
- C#之异步
C#之异步 异步是相对于同步而言.跟多线程不能同一而论.异步简单而言好比一个人两双手可以同时做两件以上不同的事情.多线程好比多个人做不同或相同的事情. 异步跟多线程有什么关系? 异步可以分为CPU异步 ...
- 使用MvcPager实现Ajax分页
接触ASP.NET MVC的时间不长,这段时间做东西的时候要用到分页,但是普通的分页用户体验不是很好,所以想实现无刷新的分页. 在网上找了好多例子,但是感觉都封装的不好,不小心发现了Webdiyer. ...
- python3--网络爬虫--爬取图片
网上大多爬虫仍旧是python2的urllib2写的,不过,坚持用python3(3.5以上版本可以使用异步I/O) 相信有不少人爬虫第一次爬的是Mm图,网上很多爬虫的视频教程也是爬mm图,看了某人的 ...
- Mysql查询优化器浅析
--Mysql查询优化器浅析 -----------------------------2014/06/11 1 定义 Mysql查询优化器的工作是为查询语句选择合适的执行路径.查询优化器的代码 ...
- python-----运算符及while循环
一.运算符 计算机可以进行的运算有很多种,不只是加减乘除,它和我们人脑一样,也可以做很多运算. 种类:算术运算,比较运算,逻辑运算,赋值运算,成员运算,身份运算,位运算,今天我们先了解前四个. 算术运 ...
- 关于MySQL buffer pool的预读机制
预读机制 两种预读算法 1.线性预读 2.随机预读 对预读的监控 一.预读机制 InnoDB在I/O的优化上有个比较重要的特性为预读,预读请求是一个i/o请求,它会异步地在缓冲池中预先回迁多个页面,预 ...
- 寻人启事(O(∩_∩)O哈哈~)
注:本文纯属扯淡,如有雷同,纯属巧合! 寻人启事格式 1:标题 标题通常在第一行中央写上<寻人启事>.<寻找XXX>或<寻XXX))等字样. 2:正文 正文一般须包括 ...
- OpenCV学习笔记:resize函数改变图像的大小
OpenCV提供了resize函数来改变图像的大小,函数原型如下: , , int interpolation=INTER_LINEAR ); 参数解释: src:输入,原图像,即待改变大小的图像: ...
- EXT.NET高效开发(二)——封装函数
在上一篇<EXT.NET高效开发(一)--概述>中,大致的介绍了一下EXT.NET.那么本篇就要继续完成未完成的事业了.说到高效开发,那就是八仙过海各显神通.比如使用代码生成器,这点大家可 ...
- EF5.0默认不支持DB First了?
面试官说貌似都是Code First了,需确认下