前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Grunt常用插件及示例说明

Grunt常用插件及示例说明

作者头像
奋飛
发布2019-08-15 09:44:35
1.1K0
发布2019-08-15 09:44:35
举报
文章被收录于专栏:Super 前端Super 前端Super 前端

下述给出了常用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.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']);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年12月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档