Grunt常用插件及示例说明

下述给出了常用Grunt插件,并列举了部分插件示例:

插件名称 说明 Github地址
grunt-contrib-clean 清空文件和文件夹 https://github.com/gruntjs/grunt-contrib-clean
grunt-contrib-copy 复制文件和文件夹 https://github.com/gruntjs/grunt-contrib-copy
grunt-contrib-concat 连接、合并文件 https://github.com/gruntjs/grunt-contrib-concat
grunt-svgstore (svg)从指定文件夹合并svg https://github.com/FWeinb/grunt-svgstore
grunt-csscomb (CSS)格式化 https://github.com/csscomb/grunt-csscomb
grunt-contrib-less (CSS)将Less编译成css https://github.com/gruntjs/grunt-contrib-less
grunt-contrib-cssmin (CSS文件)压缩 https://github.com/gruntjs/grunt-contrib-cssmin
grunt-contrib-uglify (JS文件)压缩 https://github.com/gruntjs/grunt-contrib-uglify
grunt-contrib-htmlmin (HTML文件)压缩 https://github.com/gruntjs/grunt-contrib-htmlmin
grunt-filerev 文件内容hash(MD5) https://github.com/yeoman/grunt-filerev
grunt-filerev-replace 替换通过grunt-filerev的文件引用 https://github.com/solidusjs/grunt-filerev-replace
grunt-text-replace (文本文件)使用字符串、正则、函数替换内容 https://github.com/yoniholmes/grunt-text-replace
grunt-html-build (HTML文件)增加js、css、模板引用,移除调试代码 https://github.com/spatools/grunt-html-build
grunt-autoprefixer 添加前缀依赖Can I Use数据库 https://github.com/nDmitry/grunt-autoprefixer
grunt-parallel 并行运行命令和任务 https://github.com/iammerrick/grunt-parallel
grunt-contrib-watch 文件发生改变运行任务 https://github.com/gruntjs/grunt-contrib-watch
load-grunt-tasks 使用通配符加载所有任务 https://github.com/sindresorhus/load-grunt-tasks
time-grunt 显示运行任务的执行时间 https://github.com/sindresorhus/time-grunt

Gruntfile.js 骨架

'use strict';
module.exports = function(grunt){
    // 配置信息
    var config = {
        path: __dirname,
        src: __dirname + '/test/src',
        dest: __dirname + '/test/dest'
    };

    grunt.initConfig({
        config: config,
        pkg: grunt.file.readJSON('package.json')
    });

    /* 加载所有插件 */
    require('load-grunt-tasks')(grunt);
    /* 统计各个任务执行时间 */
    require('time-grunt')(grunt);

};
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

示例:将1.html文件中的var requestAddress = "";替换为var requestAddress = "http://blog.csdn.NET/ligang2585116";

// grunt-text-replace 使用字符串、正则、函数替换文本内容
// grunt.initConfig({})中增加Task
replace: {
    requestAddress: {
        src: ['<%= config.src %>/1.html'],
        overwrite: true,
        replacements: [{
            from: 'var requestAddress = "";',
            to: 'var requestAddress = "http://blog.csdn.net/ligang2585116";'
        }]
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

示例:给1.js增加md5戳,并替换1.html1.js引用

// grunt-filerev 文件加MD5摘要
// grunt-filerev-replace 替换通过grunt-filerev的文件引用
// grunt.initConfig({})中增加Task
filerev: {
    options: {
        algorithm: 'md5',
        length: 8
    },
    js: {
        src: '<%= config.src %>/1.js'
    }
},
filerev_replace: {
    options: {
        assets_root: '<%= config.src %>/',
        views_root: '<%= config.src %>/'
    },
    js: {
        src: '<%= config.src %>/1.html'
    }
}
// 注册任务
grunt.registerInitTask("js-md5", "add md5 for js", ['filerev:js', 'filerev_replace:js']);
 

来源自:http://blog.csdn.net/ligang2585116/article/details/53790043

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值