gulp-htmlmin壓縮html

來源:互聯網
上載者:User

標籤:

通過一條命令用Npm安裝gulp-htmlmin:

npm install gulp-htmlmin --save-dev

  安裝完畢後,開啟gulpfile.js檔案,我們裡面編寫一個task用來專門壓縮html,並對html進行一系列的處理:

var gulp = require( ‘gulp‘ ); var htmlmin = require( ‘gulp-htmlmin‘ ); gulp.task( ‘html‘ , function (){ var options = { collapseWhitespace: true , collapseBooleanAttributes: true , removeComments: true , removeEmptyAttributes: true , removeScriptTypeAttributes: true , removeStyleLinkTypeAttributes: true , minifyJS: true , minifyCSS: true }; gulp.src( ‘app/**/*.html‘ ) .pipe(htmlmin(options)) .pipe(gulp.dest( ‘dest/‘ )); }); 我們看到task裡面有個設定選項,分別介紹一下他們的屬性的作用:

1.collapseWhitespace:從字面意思應該可以看出來,清除空格,壓縮html,這一條比較重要,作用比較大,引起的改變壓縮量也特別大;

2.collapseBooleanAttributes:省略布爾屬性的值,比如:<input checked="checked"/>,那麼設定這個屬性後,就會變成 <input checked/>;

3.removeComments:清除html中注釋的部分,我們應該減少html頁面中的注釋。

4.removeEmptyAttributes:清除所有的空屬性,

5.removeSciptTypeAttributes:清除所有script標籤中的type="text/javascript"屬性。

6.removeStyleLinkTypeAttributes:清楚所有Link標籤上的type屬性。

7.minifyJS:壓縮html中的javascript代碼。

8.minifyCSS:壓縮html中的css代碼。

  總之,壓縮Html的原則就是清除沒用的代碼,刪除本就預設值的屬性,將html壓縮的最小,這樣才能提高項目啟動並執行效能。

gulp-htmlmin壓縮html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.