Giter Club home page Giter Club logo

svg-sprite-loader's Introduction

SVG sprite loader TravisCI Code Climate Inch CI

Webpack loader for creating SVG sprites.

๐ŸŽ‰ 2.0 is out, please read the migration guide & overview.

โš ๏ธ For old v0.x versions see the README in the v0 branch.

Table of contents

Why it's cool

  • Minimum initial configuration. Most of the options are configured automatically.
  • Runtime for browser. Sprites are rendered and injected in pages automatically, you just refer to images via <svg><use xlink:href="#id"></use></svg>.
  • Isomorphic runtime for node/browser. Can render sprites on server or in browser manually.
  • Customizable. Write/extend runtime module to implement custom sprite behaviour. Write/extend runtime generator to produce your own runtime, e.g. React component configured with imported symbol.
  • External sprite file is generated for images imported from css/scss/sass/less/styl/html (SVG stacking technique).

Installation

npm install svg-sprite-loader -D
# via yarn
yarn add svg-sprite-loader -D

Configuration

Example config
// webpack 1
{
  test: /\.svg$/,
  loader: 'svg-sprite-loader',
  query: { ... }
}

// webpack 1 multiple loaders
{
  test: /\.svg$/,
  loaders: [
    `svg-sprite-loader?${JSON.stringify({ ... })}`,
    'svg-fill-loader',
    'svgo-loader'
  ]
}

// webpack 2
{
  test: /\.svg$/,
  loader: 'svg-sprite-loader',
  options: { ... }
}

// webpack 2 multiple loaders
{
  test: /\.svg$/,
  use: [
    { loader: 'svg-sprite-loader', options: { ... } },
    'svg-fill-loader',
    'svgo-loader'
  ]
}

symbolId (default [name])

How <symbol> id attribute should be named. Complete list of supported patterns: loader-utils#interpolatename docs.

esModule (default true, autoconfigured)

Generated export format:

  • when true loader will produce export default ....
  • when false the result is module.exports = ....

By default depends on used webpack version: true for webpack >= 2, false otherwise.

Runtime configuration

When you require an image, loader transforms it to SVG <symbol>, adds it to the special sprite storage and returns class instance that represents symbol. It contains id, viewBox and content fields and can later be used for referencing the sprite image, e.g:

import twitterLogo from './logos/twitter.svg';
// twitterLogo === SpriteSymbol<id: string, viewBox: string, content: string>

const rendered = `
<svg viewBox="${twitterLogo.viewBox}">
  <use xlink:href="#${twitterLogo.id}" />
</svg>`;

When browser event DOMContentLoaded is fired, sprite will be automatically rendered and injected in the document.body. If custom behaviour is needed (e.g. a different mounting target) default sprite module could be overridden via spriteModule option. Check example below.

spriteModule (autoconfigured)

Path to sprite module that will be compiled and executed at runtime. By default it depends on target webpack config option:

  • svg-sprite-loader/runtime/browser-sprite.build for 'web' target.
  • svg-sprite-loader/runtime/sprite.build for other targets.

If you need custom behavior, use this option to specify a path of your sprite implementation module. Path will be resolved relative to the current webpack build folder, e.g. utils/sprite.js placed in current project dir should be written as ./utils/sprite.

Example of sprite with custom mounting target (copypasted from browser-sprite):

import BrowserSprite from 'svg-baker-runtime/src/browser-sprite';
import domready from 'domready';

const sprite = new BrowserSprite();
domready(() => sprite.mount('#my-custom-mounting-target'));

export default sprite; // don't forget to export!

It's highly recommended to extend default sprite classes:

symbolModule (autoconfigured)

Same as spriteModule, but for sprite symbol. By default also depends on target webpack config option:

  • svg-sprite-loader/runtime/browser-symbol.build for 'web' target.
  • svg-sprite-loader/runtime/symbol.build for other targets.

runtimeGenerator (default generator)

Path to node.js script that generates client runtime. Use this option if you need to produce your own runtime, e.g. React component configured with imported symbol. Example.

runtimeCompat (default false, deprecated)

Should runtime be compatible with earlier v0.x loader versions. This option will be removed in the next major version release.

runtimeOptions

Arbitrary data passed to runtime generator. Reserved for future use when other runtime generators will be created.

Extract configuration

In the extract mode loader should be configured with plugin, otherwise an error is thrown. Example:

// webpack.config.js
const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');

...

{
  plugins: [
    new SpriteLoaderPlugin()
  ]
}

extract (default false, autoconfigured)

Switches loader to the extract mode. Enabled automatically for images imported from css/scss/sass/less/styl/html files.

spriteFilename (default sprite.svg)

Filename of extracted sprite. Multiple sprites can be generated by specifying different loader rules restricted with include option. In case of any conflicts loader will produce a warning:

module: {
  rules: [
    // images from img/flags goes to flags-sprite.svg
    {
      test: /\.svg$/,
      loader: 'svg-sprite-loader',
      include: path.resolve('./img/flags'), 
      options: {
        extract: true,
        spriteFilename: 'flags-sprite.svg'
      }
    },
    
    // images from img/icons goes to icons-sprite.svg
    {
      test: /\.svg$/,
      loader: 'svg-sprite-loader',
      include: path.resolve('./img/icons'),
      options: {
        extract: true,
        spriteFilename: 'icons-sprite.svg'
      }
    }    
  ]
}

It is also possible to generate sprite for each chunk by using [chunkname] pattern in spriteFilename option. This is experimental feature, so use with caution!

Examples

See examples folder.

Contributing guidelines

TODO

License

MIT

Credits

TODO

svg-sprite-loader's People

Contributors

kisenka avatar princed avatar andrey-skl avatar freelance-tech-writer avatar joearasin avatar herringtondarkholme avatar albertogasparin avatar coleturner avatar davidspiess avatar rayshade avatar jonnyzzz avatar rayfranco avatar aodev avatar rocking42 avatar steplov avatar wanecek avatar wmertens avatar fr-ench avatar meowtec avatar

Stargazers

Dayu Sun avatar

Watchers

James Cloos avatar Vincent Ricard avatar

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.