關於gulp,只壓縮頁面裡邊的css,並且在本檔案夾產生.min的檔案

來源:互聯網
上載者:User

gulpfile.js檔案:

var gulp = require('gulp'),
    htmlmin = require('gulp-htmlmin');
 
gulp.task('default', function() {
  // 將你的預設的任務代碼放在這
    const options = {
        removeComments: true,  //清除HTML注釋
        collapseWhitespace: true,  //壓縮HTML
        collapseBooleanAttributes: true,  //省略布爾屬性的值 <input checked="true"/> ==> <input checked />
        removeEmptyAttributes: true,  //刪除所有空格作屬性值 <input id="" /> ==> <input />
        removeScriptTypeAttributes: true,  //刪除<script>的type="text/javascript"
        removeStyleLinkTypeAttributes: true,  //刪除<style>和<link>的type="text/css"
        minifyJS: true,  //壓縮頁面JS
        minifyCSS: true  //壓縮頁面CSS
    };
    return gulp.src('*.html')
        .pipe(htmlmin(options))
        .pipe(gulp.dest('*.min.html'));

});


上邊的功能可以根據需要來調整,比如我用的時候只用了minifyCSS這個選項。

在檔案夾裡建立好gulpfile.css之後,只要cmd,然後下載好gulp的相關組件,然後運行gulp就可以啦

相關文章

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.