前端自動化工具 grunt 外掛程式 htmlmin 的簡單使用(五)

來源:互聯網
上載者:User

標籤:前端自動化工具 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 的簡單使用(五)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.