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工程命令行构建工 ...
随机推荐
- 谈谈渲染,玩玩nginx——前后端分离,转发请求到Tomcat的尝试
一.谈谈"渲染" 相信好多人都挺听过"渲染"这个词,但不清楚它是什么意思?前端开发以为这是后端的活儿,后端开发以为是前端的事儿,推着推着就不了了之.其实渲染很简 ...
- JavaScript高程--<script>标签
<script>标签 在HTML5中script主要有以下几个属性:async,defer,charset,src,type, async(可选): 关键词:异步脚本,外部文件,立即下载: ...
- Webservice 中涉及的几个概念
开发webservice有一段时间了.但是对其中的协议及实现一直存在疑惑.这里搜集相关概念.用以更好的理解它. 一.什么是Webservice WebService是一种跨编程语言和跨操作系统平台的远 ...
- python练习题一
1.使用while循环输出1 2 3 4 5 6 8 9 10 答:i=0 while i<10: i += 1 if i!=7: print(i) 2. ...
- IPV4/IPV6网络地址相关知识
IPv4地址:连接在Internet的每台主机(或路由器)都被分配了一个32bit的全球唯一的标识符,就是IP地址. IPv4地址的组成:网络号 + 主机号 IPv4地址的分类: A类地址:网络地址( ...
- Git的commit your changes or stash them before you can merge
今天用git pull来更新代码,遇到了下面的问题: error: Your local changes to the following files would be overwritten by ...
- 一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——实现篇:(一)概览
myRTSPClient主要可以分成3个部分: 1. RTSPClient用户接口层: 2. RTP 音视频传输解析层: 3. RTP传输层. "RTSPClient用户接口层": ...
- tomcat 组件研究二--请求过程
上一篇博客大概总结了tomcat 的组件以及其组织方式,对于tomcat 的启动过程也进行进行了简单的总结,下面这篇博客,继续研究tomcat 处理请求的相关组件,其实就是主要研究Connectors ...
- 去掉Windows桌面软件的快捷图标的箭头
去掉Windows桌面软件的快捷图标的箭头 怎么去除桌面快捷方式图标箭头 cmd /k reg delete "HKEY_CLASSES_ROOT\lnkfile" /v IsSh ...
- 并发是个什么鬼之同步工具类CountDownLatch
扯淡 写这篇文章,我先酝酿一下,实不相瞒,脱离底层太久了,更确切的情况是,真没曾认真研究过.就目前来说,很多框架包括工具类已经把实现封装的很深,你只需轻轻的调用一下API,便不费半点力气.以至于大家会 ...