標籤:前端自動化工具 grunt 外掛程式 htmlmin 的簡單使用(五)
一、contrib-htmlmin 外掛程式的使用
1、安裝 “grunt-contrib-htmlmin ”外掛程式命令(在終端進入到項目根目錄執行)
npm install grunt-contrib-htmlmin --save-dev
2、在項目根目錄下提供 htmlmin 外掛程式任務配置需要的 src 目錄和需要被壓縮的源檔案(html 源檔案放置到 src 目錄下)
mkdir src
3、在 Gruntfile.js 檔案中對 htmlmin 任務進行配置
// 封裝函數
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 外掛程式的配置有兩項:
“options”中通過使用各種屬性來指定 htmlmin 壓縮時的操作。
“build”中指定哪些 html 檔案需要進行壓縮。
4、最後在終端運行 grunt 命令
PS:如果提示 "Done, without errors." 證明就沒什麼問題了,現在去項目根目錄下看是否已經產生了存放壓縮檔的目錄和被壓縮後的目標檔案。
本文出自 “珞辰的部落格” 部落格,轉載請與作者聯絡!
前端自動化工具 grunt 外掛程式 htmlmin 的簡單使用(五)