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工程命令行构建工 ...
随机推荐
- H5投放在朋友圈广告做压力测试
一.环境 MacOS Sierra 二.背景 朋友圈广告投放的H5需要做ab压测,这里不赘述. 具体官方文档如下:http://ad.weixin.qq.com/learn/n10 三.正文 (1)别 ...
- SecureCRT 常用命令大全
常用命令:一.ls 只列出文件名 (相当于dir,dir也可以使用) -A:列出所有文件,包含隐藏文件. -l:列表形式,包含文件的绝大部分属性. -R:递归显示. --help:此命令的帮助. 二. ...
- 敏捷开发之产品日日新,一步通之---自动化代码构建->自动化打包->自动化安装部署
本文将介绍如何自动化实现代码构建,自动化代码打包成exe安装包,自动化安装到测试环境.通过计划任务的方式,每天自动化发布最新的产品供老板展示,供测试人员使用,真正实现敏捷的快速迭代. 自动代码构建 自 ...
- mysql 报错 session halted的解决办法,实际工作中的结论。
写后台程序,发现执行到sql语句时就报错session halted,如下图: 也上网搜过蛮多方法,都不能解决我的问题.后来自己发现了症结所在,其实很简单:执行insert的语句没有包含not nul ...
- 【NO.5】jmeter-结果文件
Jmeter的结果文件可以保存很多内容,你需要看哪个就勾选哪个,很简单是吧. 结果文件可以保存为2种形式:XML或者CSV.我印象里在书上提到过,如果保存为XML形式的结果文件,后续可以转化为表格便于 ...
- 【转】C++智能指针简单剖析
原文链接:http://www.cnblogs.com/lanxuezaipiao/p/4132096.html 导读 最近在补看 <C++ Primer Plus>第六版,这的确是本好书 ...
- Python下的OpenCV学习 02 —— 图像的读取与保存
OpenCV提供了众多对图片操作的函数,其中最基本的就是图片的读取与输出了. 一.读取图片 利用OpenCV读取一张图片是非常容易的,只需要用到 imread() 函数,打开shell或者cmd,进入 ...
- 20个开发人员非常有用的Java功能代码
本文将为大家介绍20个对开发人员非常有用的Java功能代码.这20段代码,可以成为大家在今后的开发过程中,Java编程手册的重要部分. 1. 把Strings转换成int和把int转换成String ...
- ubuntu安装mysql可视化工具MySQL-workbench及简单操作
一.使用命令行在ubuntu下安装mysql可视化工具MySQL-workbench Step1:安装MySQL-workbench 方案一:如果你已经装好mysql的相关服务,那么直接使用如下命令即 ...
- 80C51 数码管动态显示0~7
所使用的开发板 普中科技HC6800-ES V2.0 PC:win7 64位 编译软件: keil uversion2 烧写工具: 普中科技开发的PZ-ISP V1.82 烧写方式:热烧写 #incl ...