原文链接:http://www.ydcss.com/archives/54
简介:
使用gulp-uglify压缩javascript文件,减小文件大小。
1、安装nodejs/全局安装gulp/项目安装gulp/创建package.json和gulpfile.js文件
1.1、gulp基本使用还未掌握?请参看: gulp详细入门教程
1.2、本示例目录结构如下:
2、本地安装gulp-uglify
2.1、github:https://github.com/terinjokes/gulp-uglify
2.2、安装:命令提示符执行 cnpm install gulp-uglify --save-dev
2.3、注意:没有安装cnpm请使用 npm install gulp-uglify --save-dev
。 什么是cnpm,如何安装?
2.4、说明:--save-dev
保存配置信息至 package.json 的 devDependencies 节点。为什么要保存至package.json?
3、配置gulpfile.js
3.1、基本使用
var gulp = require('gulp'),
uglify = require('gulp-uglify');
gulp.task('jsmin', function () {
gulp.src('src/js/index.js')
.pipe(uglify())
.pipe(gulp.dest('dist/js'));
});
3.2、压缩多个js文件
var gulp = require('gulp'), uglify = require('gulp-uglify'); gulp.task('jsmin', function () { gulp.src(['src/js/index.js','src/js/detail.js']) //多个文件以数组形式传入 .pipe(uglify()) .pipe(gulp.dest('dist/js')); });3.3、匹配符“!”,“*”,“**”,“{}”
var gulp = require('gulp'), uglify= require('gulp-uglify'); gulp.task('jsmin', function () { //压缩src/js目录下的所有js文件 //除了test1.js和test2.js(**匹配src/js的0个或多个子文件夹) gulp.src(['src/js/*.js', '!src/js/**/{test1,test2}.js']) .pipe(uglify()) .pipe(gulp.dest('dist/js')); });3.4、指定变量名不混淆改变
var gulp = require('gulp'), uglify= require('gulp-uglify'); gulp.task('jsmin', function () { gulp.src(['src/js/*.js', '!src/js/**/{test1,test2}.js']) .pipe(uglify({ //mangle: true,//类型:Boolean 默认:true 是否修改变量名 mangle: {except: ['require' ,'exports' ,'module' ,'$']}//排除混淆关键字 })) .pipe(gulp.dest('dist/js')); });3.5、gulp-uglify其他参数 具体参看
var gulp = require('gulp'), uglify= require('gulp-uglify'); gulp.task('jsmin', function () { gulp.src(['src/js/*.js', '!src/js/**/{test1,test2}.js']) .pipe(uglify({ mangle: true,//类型:Boolean 默认:true 是否修改变量名 compress: true,//类型:Boolean 默认:true 是否完全压缩 preserveComments: 'all' //保留所有注释 })) .pipe(gulp.dest('dist/js')); });
4、执行任务
4.1、命令提示符执行:gulp jsmin
5、结束语
5.1、本文有任何错误,或有任何疑问,欢迎留言说明。
PS:参数:
Options
-
mangle
Pass
false
to skip mangling names. -
output
Pass an object if you wish to specify additional output options. The defaults are optimized for best compression.
-
compress
Pass an object to specify custom compressor options. Pass
false
to skip compression completely. -
preserveComments
A convenience option for
options.output.comments
. Defaults to preserving no comments.-
all
Preserve all comments in code blocks
-
license
Attempts to preserve comments that likely contain licensing information, even if the comment does not have directives such as
@license
or/*!
.Implemented via the
uglify-save-license
module, this option preserves a comment if one of the following is true:- The comment is in the first line of a file
- A regular expression matches the string of the comment. For example:
MIT
,@license
, orCopyright
. - There is a comment at the previous line, and it matches 1, 2, or 3.
-
function
Specify your own comment preservation function. You will be passed the current node and the current comment and are expected to return either
true
orfalse
. -
some
(deprecated)Preserve comments that start with a bang (
!
) or include a Closure Compiler directive (@preserve
,@license
,@cc_on
). Deprecated in favor of thelicense
option, documented above.
-
You can also pass the uglify
function any of the options listed here to modify UglifyJS's behavior.