周末有点懒,跑去看了《智取威虎山》,手撕鬼子的神剧情节被徐老怪一条回忆线就解释过去了,牛到极致尽是这种四两拨千斤的处理方式,手撕加分,四星推荐。

-----------------------------闲话分割线-----------------------------

  concat、cssmin、uglify对应的分别是合并、css压缩、js压缩混淆,至于为什么把这三个放在一起,在后面的usemin模块会给出解释。

  concat(V0.5.0)的options(github地址)

  1. separator: 就是源文件之间的分隔符,默认是grunt.util.linefeed,也就是换行符。如果源文件是js,而且之后需要进行uglify,将此option改为";"

  2. banner: 目标文件的头部注释,默认是""
  3. footer: 目标文件的底部注释,默认是""
  4. stripBanners: 合并时是否删除源文件头部的注释,默认false
  5. process: 对目标文件进行内容替换,接受三种类型:Boolean、Funtion、Object。有两种替换模式,一种是设为function进行全部替换,一种是设为true或object进行模板(<% =var %>)替换。
    1. 新建两个文件,分别为helloworld.js

      (function(){
      console.log('hello world');
      })();

      hellogrunt.js

      (function(){
      console.log('hello grunt');
      })();
    2. 在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']);
      };
    3. 以上是一个不设置process的concat任务,输出的目标文件
      (function(){
      console.log('hello grunt');
      })();
      (function(){
      console.log('hello world');
      })();
    4. 将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');
      })();
    5. 将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对象,往下看

    6. 将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

  6. sourceMap、sourceMapName、sourceMapStyle: 暂未研究,待以后补充

  cssmin(V0.11.0)的options(github地址

  1. 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地址

  1. 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)}();
  2. 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)}();
  3. beautify: 一般可理解为保留换行,接受两种类型:Boolean、Object,默认值是false。参数比较多,官方地址
  4. expression: 将一段JSON字符串解析成一个标准的JSON,与mangle、compress冲突,默认为false
  5. 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);
  6. wrap: 将js使用匿名函数wrap,并对外暴露一个对象作为接口,与mangle冲突,接受String,默认为undefined
    Gruntfile.js
    mangle: 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}());
  7. maxLineLen: 每行限制长度,为0取消限制,接受Number,默认为32000
  8. ASCIIOnly: 将js中的非ASCII字符用unicode表示,默认为false
  9. 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}());
    
  10. 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则保留该注释

  11. banner、footer: 略
  12. 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的更多相关文章

  1. grunt使用小记之uglify:最全的uglify使用DEMO

    grunt-contrib-uglify uglify是一个文件压缩插件,项目地址:https://github.com/gruntjs/grunt-contrib-uglify 本文将以一个DEMO ...

  2. 闲话和grunt

    一年半没更新是因为自己转岗了,android framework+system转前端,可以想象过程之苦逼,苦成了一首诗:很烦很烦/非常烦/非常非常十分烦/特别烦特烦/极其烦/贼烦/简直烦死了/啊——. ...

  3. 配置grunt进行css、js的检查、合并和压缩

    现在会进行代码的合并和压缩已成为前端人员的必备知识,那么现在来介绍一个grunt的工具.grunt是个风靡世界的工具,它的首页是  http://www.gruntjs.net 这是个中文网站,有文档 ...

  4. grunt压缩合并代码

    module.exports = function(grunt) { // 配置 grunt.initConfig({ pkg : grunt.file.readJSON('package.json' ...

  5. 开箱即用 - Grunt合并和压缩 js,css 文件

    js,css 文件合并与压缩 Grunt 是前端自动化构建工具,类似webpack. 它究竟有多强悍,请看它的 介绍. 这里只演示如何用它的皮毛功能:文件合并与压缩. 首先说下js,css 合并与压缩 ...

  6. grunt之filerev、usemin

    窃以为这两个插件是比较有用的,filerev是给js.css进行编码重命名,usemin修改html中被重命名的js.css文件的引用.另外说明下之前将concat.cssmin.uglify放在一篇 ...

  7. Grunt入门学习之(3) -- Gruntfile具体示例

    经过前面的学习,将测试的Gruntfile整合在一起! /** * Created by Administrator on 2017/6/22. */ module.exports = functio ...

  8. Grunt Part 2

    Objectives and Outcomes In this exercise, you will continue to learn to use Grunt, the task runner. ...

  9. 前端自动化构建工具Grunt

    一.了解Gurnt(http://www.open-open.com/lib/view/open1433898272036.html) Grunt 是一个基于任务的JavaScript工程命令行构建工 ...

随机推荐

  1. 2.跑nodejs文件

    1.安装nodejs的环境,这里不介绍了. app.js 'use strict'; //process.argv 接受变量 var command = process.argv[2]; var a ...

  2. Uva 10142 Australia Voting

    水题 模拟 大意就是模拟一个选举的系统 认真读题,注意细节,耐心调试 #include<cmath> #include<math.h> #include<ctype.h& ...

  3. NYOJ--1058--dfs--部分和问题

    /* Name: NYOJ--1058--部分和问题 Author: shen_渊 Date: 15/04/17 19:27 Description: 简单的DFS,以为马上过的,递归的i+1写错了, ...

  4. NYOJ--284--广搜+优先队列--坦克大战

    /* Name: NYOJ--284--坦克大战 Author: shen_渊 Date: 14/04/17 19:08 Description: 广度优先搜索+优先队列 注意清空地图 对输入地图进行 ...

  5. python时间序列分析

              题记:毕业一年多天天coding,好久没写paper了.在这动荡的日子里,也希望写点东西让自己静一静.恰好前段时间用python做了一点时间序列方面的东西,有一丁点心得体会想和大家 ...

  6. Java并发工具类 - CountDownLatch

    Java并发工具类 - CountDownLatch 1.简介 CountDownLatch是Java1.5之后引入的Java并发工具类,放在java.util.concurrent包下面 http: ...

  7. mysql日期函数 当前日期 curdate() , 当前年 year(curdate()), 取date的年份 year(date) ,取date的月份 month(date)

    获取系统当前日期时间: sysdate() 获取系统当前日期:curdate() 获取系统当前时间:curtime() 获取给定日期的年份: year(date) 获取给定日期的月份:month(da ...

  8. 腾讯织云:DevOps 流水线应用平台践行之路

    欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:梁定安,腾讯织云负责人,目前就职于腾讯社交网络运营部,任运维技术总监,开放运维联盟委员,腾讯云布道师,腾讯课堂运维讲师,EXIN D ...

  9. 克隆一个centos虚拟机的环境配置

    克隆环境预处理设置 iptables -F  #清除规则 /etc/init.d/iptables save vim /etc/selinux/config selinux=disabled   # ...

  10. ueditor单独调用上传附件和图片的功能

    javascript富文本编辑器使我们添加.编辑网站中的文章更加方便和容易.这些富文本编辑器提供了所见即所得(What You See Is What You Get - WYSIWYG)的功能,可以 ...