gulp教程之gulp-uglify

原文链接: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.jstest2.js**匹配src/js0个或多个子文件夹)
    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:

      1. The comment is in the first line of a file
      2. A regular expression matches the string of the comment. For example: MIT@license, or Copyright.
      3. 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 or false.

    • some (deprecated)

      Preserve comments that start with a bang (!) or include a Closure Compiler directive (@preserve@license,@cc_on). Deprecated in favor of the license option, documented above.

You can also pass the uglify function any of the options listed here to modify UglifyJS's behavior.




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值