webpack3之處理圖片

目錄結(jié)構(gòu)

目錄結(jié)構(gòu)
  • 處理圖片(一般是css、less、sass中,還有app.js中require的圖片文件)

安裝模塊

npm install webpack style-loader precss postcss-loader css-loader autoprefixer file-loader url-loader --save
  • 其中 file-loader url-loader兩個(gè)模塊是圖片處理的必要模塊

相關(guān)文件

//====app.js
require('./styles/index.css');
require('./styles/base.less');
require('./images/user-avater.png');
//====index.css文件
body{
  padding: 0px;
  margin:0px;
  background-color: #f8f8f8;
}
//====base.less
@colorFF:#666;
body{
color: @colorFF;
}
.page{
  background-color:#f8f8f8;
  position: absolute;
  top: 0px;
  padding-top:50px;
  left: 0px;
  right: 0px;
  bottom: 60px;
  overflow: auto;

  &.no-nav{
    bottom: 0px;
  }
}
  nav{
    position: absolute;
    bottom: 0px;
    left: 0px;
    right: 0px;
    height: 60px;
    display: flex;
    border-top:1px solid #ededed;
    background-color: #fff;

    a:link,a:visited{
      text-decoration:none;
      flex: 1;
      text-align: center;
      box-sizing: border-box;
/*      border-right: 1px solid #ededed;*/
      color: #666;
      padding-top: 5px;
    }
    a:last-child{
      border-right: none;
    }
    a.v-link-active{
      color: #FF4354;
    }
    a i{
      display: block;
      margin: 0 auto;
      width: 25px;
      height: 25px;
    }
    a.home.v-link-active i{
      background: url('../images/nav-home-on.png') no-repeat center;
      background-size: contain;
    }
    a.home i{
      background: url('../images/nav-home-off.png') no-repeat center;
      background-size: contain;
    }
    a.topics.v-link-active i{
      background: url('../images/nav-circle-on.png') no-repeat center;
      background-size: contain;
    }
    a.topics i{
      background: url('../images/nav-circle-off.png') no-repeat center;
      background-size: contain;
    }
    a.message.v-link-active i{
      background: url('../images/nav-message-on.png') no-repeat center;
      background-size: contain;
    }
    a.message i{
      background: url('../images/nav-message-off.png') no-repeat center;
      background-size: contain;
    }
    a.user.v-link-active i{
      background: url('../images/nav-mine-on.png') no-repeat center;
      background-size: contain;
    }
    a.user i{
      background: url('../images/nav-mine-off.png') no-repeat center;
      background-size: contain;
    }
  }

配置文件(webpack.config.js)

var webpack=require('webpack');

//css文件提取器需要的配置項(xiàng)
var ExtractTextPlugin=require('extract-text-webpack-plugin');

//postcss-loader 需要的配置項(xiàng)
var precss       = require('precss');
var autoprefixer = require('autoprefixer');

module.exports = {
  entry: {
    app:'./src/app.js'
  },
  output: {
    path: './build',
    filename: 'js/[name].js'
  },
  module: {
    loaders: [
      // 編譯css并自動添加css前綴 并將css提取
      { test: /\.css$/, loader: ExtractTextPlugin.extract('style','css!postcss-loader')},
      { test: /\.less$/, loader: ExtractTextPlugin.extract('style','css!postcss-loader!less')},

//=========為了處理圖片我們加入以下配置======================
      {
        // 圖片加載器,雷同file-loader,更適合圖片,可以將較小的圖片轉(zhuǎn)成base64,減少http請求
        // 如下配置,將小于8192byte的圖片轉(zhuǎn)成base64碼
          test: /\.(png|jpg|gif)$/,
          loader: 'url-loader?limit=8192&name=../images/[name].[ext]?[hash]',
        }
//======================================================

    ]
  },
  postcss: function () {
    return [precss, autoprefixer];
  },
  plugins:[
    new ExtractTextPlugin('css/app.css'),
    ]
}

url-loader的參數(shù)

  • limit = 后面跟的是數(shù)字,加上這個(gè)參數(shù),圖片文件大小(單位為byte)小于該參數(shù)值的文件會被轉(zhuǎn)換為base64編碼的形式
    (如)
//處理之前
    a.home.v-link-active i{
      background: url('../images/nav-home-on.png') no-repeat center;
      background-size: contain;
    }
//處理之后
nav a.home.v-link-active i {
  background: url(data:image/png;base64,iVBORw0KGgoAA...BJRU5ErkJggg==) no-repeat center;
  background-size: contain;
}
  • name 是表示文件被處理之后再bulid目錄中的路徑和圖片生成規(guī)則
// 處理之前
.page{
  position: absolute;
  top: 0px;
  background-image: url('../images/user-avater.png');
}
//處理之后
.page {
  position: absolute;
  top: 0px;
  background-image: url(./images/user-avater..png?1e0325ccb9b6cb0b41103804d088890d);
}
  • [name]表示圖片文件的文件名

  • [ext]表示圖片文件的擴(kuò)展名

  • [hash]表示圖片文件的哈希值

  • 以上三個(gè)參數(shù)可以自由組合,改變圖片被處理有生成的文件名等信息

HTML引用

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>處理css</title>
  <script type="text/javascript" src="build/js/app.js"></script>
  <link rel="stylesheet" type="text/css" href="build/css/app.css"> 
<body>
  
</body>
</html>

擴(kuò)展閱讀

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 webpack介紹和使用 一、webpack介紹 1、由來 ...
    it筱竹閱讀 11,486評論 0 21
  • 記得2004年的時(shí)候,互聯(lián)網(wǎng)開發(fā)就是做網(wǎng)頁,那時(shí)也沒有前端和后端的區(qū)分,有時(shí)一個(gè)網(wǎng)站就是一些純靜態(tài)的html,通過...
    陽陽陽一堆陽閱讀 3,470評論 0 5
  • webpack 介紹 webpack 是什么 為什么引入新的打包工具 webpack 核心思想 webpack 安...
    yxsGert閱讀 6,682評論 2 71
  • 無意中看到zhangwnag大佬分享的webpack教程感覺受益匪淺,特此分享以備自己日后查看,也希望更多的人看到...
    小小字符閱讀 8,383評論 7 35
  • 1.自定義類繼承 SQLiteOpenHelper 我們應(yīng)該重點(diǎn)掌握execSQL()和rawQuery()方法。...
    Kevin_Curry閱讀 529評論 0 1

友情鏈接更多精彩內(nèi)容