手把手教你如何使用webpack 生成css sprites

前言

咱們在開發網站的時候,一般會把經常使用的圖標合併成css sprite(雪碧圖),能夠有效的減小站點的http請求數量,從而提升網站性能。javascript

下面讓咱們一塊兒來學習一下如何使用webpack合併sprite圖。css

準備

  • webpack
  • webpack-spritesmith插件
  • file-loader
  • sass-loader(由於webpack-spritesmith除了生成雪碧圖以外,還會生成相應的mixin,使用起來很方便,因此要用sass)
  • 這裏我還使用了ExtractTextPlugin來提取css文件,這不是必須的你也能夠不用,用的話須要安裝extract-text-webpack-plugin這個插件

使用npm安裝好上面的東西java

下面請開始咱們的表演

step1:將咱們要合併的圖標準備好,放在src下的ico文件夾下

給你們分享一個好用的icon下載網站,裏面的圖標風格我很喜歡,並且是免費的哦,你們也能夠到裏面去下載本身喜歡的icon用於本次練習node

57,900 個免費的平面圖標webpack

這是我下載的png圖片web

facebook
facebook
twitter
twitter
google
google

step2 在你的webpack.config.js中按下面這樣編寫

const path = require('path');
const SpritesmithPlugin = require('webpack-spritesmith');

module.export = {
    // ...
    module: {
        rules: [
            {
                test: /png$/
                loader:[
                    'file-loader?name=i/[hash].[ext]'
                ]
            },
            {
                test: /\.(css|scss)$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: ['css-loader', 'postcss-loader', 'sass-loader']
                })
            }
        ]
    },

    resolve: {
        modules: [
            'node_modules',
            'assets/sprite' //css在哪裏能找到sprite圖
        ]
    },

    plugins: [
        new SpritesmithPlugin({
            src: {
                cwd: path.resolve(__dirname, 'src/ico'),  //準備合併成sprit的圖片存放文件夾
                glob: '*.png'  //哪類圖片
            },
            target: {
                image: path.resolve(__dirname, 'src/assets/sprites.png'),  // sprite圖片保存路徑
                css: path.resolve(__dirname, 'src/assets/_sprites.scss')  // 生成的sass保存在哪裏
            },
            apiOptions: {
                cssImageRef: "~sprite.png" //css根據該指引找到sprite圖
            }
        })
    ]
}複製代碼

step3 在你的scss文件中@import導入生成的文件

@import '../../../assets/sprite/_sprite.scss'; //路徑請自行更改

.facebook{ 
    @include sprite($facebook); 
}

.twitter{ 
    @include sprite($twitter); 
}

.google{ 
    @include sprite($google); 
}複製代碼

step4 運行webpack,看到咱們的sprite圖已經被用在咱們的站點上了

/assets/sprite/sprite.png就是咱們生成的sprite圖了npm

看看同時生成的sass文件api

// SCSS variables are information about icon's compiled state, stored under its original file name
//
// .icon-home {
//   width: $icon-home-width;
// }
//
// The large array-like variables contain all information about a single icon
// $icon-home: x y offset_x offset_y width height total_width total_height image_path;
//
// At the bottom of this section, we provide information about the spritesheet itself
// $spritesheet: width height image $spritesheet-sprites;
$facebook-name: 'facebook';
$facebook-x: 0px;
$facebook-y: 0px;
$facebook-offset-x: 0px;
$facebook-offset-y: 0px;
$facebook-width: 64px;
$facebook-height: 64px;
$facebook-total-width: 128px;
$facebook-total-height: 128px;
$facebook-image: '~sprite.png';
$facebook: (0px, 0px, 0px, 0px, 64px, 64px, 128px, 128px, '~sprite.png', 'facebook', );
$google-name: 'google';
$google-x: 64px;
$google-y: 0px;
$google-offset-x: -64px;
$google-offset-y: 0px;
$google-width: 64px;
$google-height: 64px;
$google-total-width: 128px;
$google-total-height: 128px;
$google-image: '~sprite.png';
$google: (64px, 0px, -64px, 0px, 64px, 64px, 128px, 128px, '~sprite.png', 'google', );
$twitter-name: 'twitter';
$twitter-x: 0px;
$twitter-y: 64px;
$twitter-offset-x: 0px;
$twitter-offset-y: -64px;
$twitter-width: 64px;
$twitter-height: 64px;
$twitter-total-width: 128px;
$twitter-total-height: 128px;
$twitter-image: '~sprite.png';
$twitter: (0px, 64px, 0px, -64px, 64px, 64px, 128px, 128px, '~sprite.png', 'twitter', );
$spritesheet-width: 128px;
$spritesheet-height: 128px;
$spritesheet-image: '~sprite.png';
$spritesheet-sprites: ($facebook, $google, $twitter, );
$spritesheet: (128px, 128px, '~sprite.png', $spritesheet-sprites, );

// The provided mixins are intended to be used with the array-like variables
//
// .icon-home {
//   @include sprite-width($icon-home);
// }
//
// .icon-email {
//   @include sprite($icon-email);
// }
//
// Example usage in HTML:
//
// `display: block` sprite:
// <div class="icon-home"></div>
//
// To change `display` (e.g. `display: inline-block;`), we suggest using a common CSS class:
//
// // CSS
// .icon {
//   display: inline-block;
// }
//
// // HTML
// <i class="icon icon-home"></i>
@mixin sprite-width($sprite) {
  width: nth($sprite, 5);
}

@mixin sprite-height($sprite) {
  height: nth($sprite, 6);
}

@mixin sprite-position($sprite) {
  $sprite-offset-x: nth($sprite, 3);
  $sprite-offset-y: nth($sprite, 4);
  background-position: $sprite-offset-x  $sprite-offset-y;
}

@mixin sprite-image($sprite) {
  $sprite-image: nth($sprite, 9);
  background-image: url(#{$sprite-image});
}

@mixin sprite($sprite) {
  @include sprite-image($sprite);
  @include sprite-position($sprite);
  @include sprite-width($sprite);
  @include sprite-height($sprite);
}

// The `sprites` mixin generates identical output to the CSS template
//   but can be overridden inside of SCSS
//
// @include sprites($spritesheet-sprites);
@mixin sprites($sprites) {
  @each $sprite in $sprites {
    $sprite-name: nth($sprite, 10);
    .#{$sprite-name} {
      @include sprite($sprite);
    }
  }
}複製代碼

最後看看應用在網站上的效果sass

總結

好了,以上就是webpack生成css sprite的辦法了,是否是覺着很簡單呢,若是本文對您有幫助,請記得點個贊哦。ide

本文由張小點於2017-10-3發表於掘金,轉載請註明出處。原文連接

參考

相關文章
相關標籤/搜索