1、contrib-htmlmin 插件的使用html
一、安装 “grunt-contrib-htmlmin ”插件命令(在终端进入到项目根目录执行)npm
npm install grunt-contrib-htmlmin --save-devjson
二、在项目根目录下提供 htmlmin 插件任务配置须要的 src 目录和须要被压缩的源文件(html 源文件放置到 src 目录下)ide
mkdir src函数
三、在 Gruntfile.js 文件中对 htmlmin 任务进行配置grunt
// 包装函数
module.exports = function (grunt) {
// 任务配置,全部插件的配置信息
grunt.initConfig({
// 获取 package.json 的信息
pkg: grunt.file.readJSON('package.json'),
// htmlmin 插件的配置信息
htmlmin: {
options: {
removeComments: true, // 去除注释信息
collapseWhitespace: true, // 去除空白字符
removeEmptyAttributes: true, // 去除标签的空属性
removeCommentsFromCDATA: true, // 去除 CDATA 的注释信息
removeRedundantAttributes: true // 去除标签的冗余属性
},
// 具体任务配置
build: {
files: [{
expand: true,
cwd: 'src',
src: '**/*.html',
dest: 'dest'
}]
}
}
});
// 加载指定插件任务
grunt.loadNpmTasks('grunt-contrib-htmlmin');
// 默认执行的任务
grunt.registerTask('default', ['htmlmin']);
};
PS:htmlmin 插件的配置有两项:ui
“options”中经过使用各类属性来指定 htmlmin 压缩时的操做。spa
“build”中指定哪些 html 文件须要进行压缩。插件
四、最后在终端运行 grunt 命令
orm
PS:若是提示 "Done, without errors." 证实就没什么问题了,如今去项目根目录下看是否已经生成了存放压缩文件的目录和被压缩后的目标文件。