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)从指定文件夹合并svghttps://github.com/FWeinb/grunt-svgstore
grunt-csscomb(CSS)格式化https://github.com/csscomb/grunt-csscomb
grunt-contrib-less(CSS)将Less编译成csshttps://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.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.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']);
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋飛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值