npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svg-classic-sprite-loader

v0.2.5

Published

Webpack loader for creating classic SVG sprites

Downloads

124

Readme

svg-classic-sprite-loader

Webpack loader for creating classic SVG sprites.

The main reason we make a different loader from svg-sprite-loader is that non-classic way (not using background-position) to create svg sprite does not work in Safari.

This article shows several ways to create svg sprites. You can take a look in different browers.

NPM Version Dependencies NPM Download

Installation

npm install --save-dev svg-classic-sprite-loader

Note: This loader does not support [email protected] currently.

Quick Start

Add loader in webpack.config.js like this:

module.exports = {
    ...
    module: {
        rules: [
            { test: /\.css$/, use: [
                'style-loader',
                'css-loader',
                'svg-classic-sprite-loader',
            ] },
            { test: /\.svg$/, use: {
                loader: 'file-loader',
                options: {
                    name: '[name].[ext]'
                }
            } },
        ],
    },
};

Use svgs in a CSS file:

.foo {
    background: url(./assets/check.svg);
}
.bar {
    background: url(./assets/accessory.svg);
}

The loader will merge svgs into a sprite file, and replace CSS codes:

.foo {
    background: url(sprite.svg) -20px -20px no-repeat;
}
.bar {
    background: url(sprite.svg) -92px -20px no-repeat;
}

For more examples, check here.

Features:sparkles:

  • Easy to use, just set up the associated svg path in CSS only.
  • Generating sprite according to need.
  • Output multiple sprite.

Config

loader options

defaultName

  • Type: string
  • Default: 'sprite'

Default file name of sprite output file.

padding

  • Type: number
  • Default: 'sprite'

The margin between svgs in sprite.

filter

  • Type: string
  • Default: 'all'

Options: 'all''query'RegExp

How to filter svg files for merging:

  • 'all': All imported svgs will be merged.
  • 'query': Only svg path with ?sprite query param will be merged.
  • RegExp: Only svg path matched by RegExp

queryParam

Customize key of query param in svg path. Only works when filter: 'query'

  • Type: string
  • Default: 'sprite'

Examples

Use query

/* webpack.config.js */
loader: 'svg-classic-sprite-loader',
options: {
    filter: 'query',
},
/* css */
.test {
    background: url(./assets/log-check.svg?sprite);
}
.test1 {
    background: url(./assets/check.svg?sprite=sprite);
}
.test2 {
    background: url(./assets/apm-check.svg);
}

log-check.svg and check.svg are merged into sprite.svg. Finally output files are sprite.svg and apm-check.svg.

Output multiple sprites

.foo {
    background: url(./assets/check.svg?sprite=sprite1);
}
.bar {
    background: url(./assets/accessory.svg?sprite=sprite2);
}
...

check.svg is merged into sprite1.svg, and accessory.svg is merged into sprite2. Finally output files are sprite1.svg and sprite2.svg.

Use RegExp

/* webpack.config.js */
loader: 'svg-classic-sprite-loader',
options: {
    filter: /log/,
},
/* css */
.test{
    background: url(./assets/log-check.svg?sprite=sprite1);
}
.test1{
    background: url(./assets/check.svg?sprite=sprite1);
}

Only log-check.svg is merged into sprite1.svg. Finally output files are sprite1.svg and check.svg.

Changelog

See Releases

Contribution Guide

See Contributing Guide

LICENSE

MIT