Giter Club home page Giter Club logo

webpack-svgstore-plugin's Introduction

Webpack-svgstore-plugin

Installation

npm i webpack-svgstore-plugin --save-dev

Usage

webpack.config.js

1) require plugin

//webpack.config.js

var SvgStore = require('webpack-svgstore-plugin');

2) in plugins section

//webpack.config.js

//load plugins
plugins: [

  //create svg sprite from /path/to/*.svg
  new SvgStore(path.join(_path + '/app/assets/svg'), {
  
    // svg prefix
    prefix: 'icon-',
    svg: {
      style: 'position:absolute; width:0; height:0',
      xmlns: 'http://www.w3.org/2000/svg'
    },
    
    //output section
    output: [
    
      // you can create 1 or multiply sprites
      {
        // filter by prefix
        filter: 'Logo-',
        // add md5 hash to file name
        sprite: 'svg/[hash].logo_sprite.svg'
      },
      {
        // except filter - all except 'Logo-'
        filter: 'except-Logo-',
        sprite: 'svg/[hash].sprite.svg'
      }
    ],
    
    // append svgXHR function to call sprite
    append: true, // deafult: false
    appendPath: path.join(_path, 'app', 'views', 'shared', '_sprite.html.slim'), // path to file
    loop: 2,
    min: true
  })
]

Examples

1 sprite for all svg's

  new SvgStore(path.join(_path + '/app/assets/svg'), {
    svg: {
      style: 'position:absolute; width:0; height:0',
      xmlns: 'http://www.w3.org/2000/svg'
    },
    output: [
      {
        filter: 'all', // to get all svg's
        sprite: 'svg/sprite.svg'
      }
    ]
  })

Add prefix

  new SvgStore(path.join(_path + '/app/assets/svg'), {
    prefix: 'icon-', // add prefix
    svg: {
      style: 'position:absolute; width:0; height:0',
      xmlns: 'http://www.w3.org/2000/svg'
    },
    output: [
      {
        filter: 'all', // to get all svg's
        sprite: 'svg/sprite.svg'
      }
    ]
  })

Add hash to output sprite

  new SvgStore(path.join(_path + '/app/assets/svg'), {
    prefix: 'icon-',
    svg: {
      style: 'position:absolute; width:0; height:0',
      xmlns: 'http://www.w3.org/2000/svg'
    },
    output: [
      {
        filter: 'all',
        sprite: 'svg/[hash].sprite.svg' // hash
      }
    ]
  })

Minify sprite

  new SvgStore(path.join(_path + '/app/assets/svg'), {
    prefix: 'icon-',
    svg: {
      style: 'position:absolute; width:0; height:0',
      xmlns: 'http://www.w3.org/2000/svg'
    },
    output: [
      {
        filter: 'all',
        sprite: 'svg/[hash].sprite.svg'
      }
    ],
    min: true // default: false
  })

Add loop quantity for minify (svgo)

  new SvgStore(path.join(_path + '/app/assets/svg'), {
    prefix: 'icon-',
    svg: {
      style: 'position:absolute; width:0; height:0',
      xmlns: 'http://www.w3.org/2000/svg'
    },
    output: [
      {
        filter: 'all',
        sprite: 'svg/[hash].sprite.svg'
      }
    ],
    min: true,
    loop: 2 // default: 1 - sometimes need more than one
  })

Get 2 different sprites by filter

  new SvgStore(path.join(_path + '/app/assets/svg'), {
    prefix: 'icon-',
    svg: {
      style: 'position:absolute; width:0; height:0',
      xmlns: 'http://www.w3.org/2000/svg'
    },
    output: [
      {
        // 1 sprite with all svgs which name contains 'Logo-' 
        filter: 'Logo-',
        sprite: 'svg/[hash].logo_sprite.svg'
      },
      {
        // 2 sprite with all other svg except names contains 'Logo-'
        filter: 'except-Logo-',
        sprite: 'svg/[hash].sprite.svg'
      }
    ],
    min: true,
    loop: 2
  })

Append gotten sprites by ajax with custom formatting dynamically to some file - Comming soon

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.