UNPKG

3.31 kBMarkdownView Raw
1# Webpack SVG sprite loader
2
3It's like [style-loader](https://github.com/webpack/style-loader) but for SVG:
4
5- Creates a single SVG sprite from a set of images.
6- Raster images support (PNG, JPG and GIF).
7- Custom sprite implementation support.
8
9## How it works
10
11When you require an image, loader transforms it to SVG symbol and add it to the array in special [sprite](lib/web/sprite.js) class.
12When browser event `DOMContentLoaded` fires sprite will be rendered and injected as first child of `document.body`.
13Require statement e.g. `require('svg-sprite!./image.svg')` returns a symbol id, so you can reference it later
14in `<svg><use xlink:href="#id"/></svg>`. Raster images will be inlined (base64) and wrapped with an `<image>` tag.
15Files like `image@2x.png` will be transformed with proper scale.
16
17### Custom sprite implementation
18
19By default sprite renders when `DOMContentLoaded` event fires and injects as first child in `document.body`.
20If you need custom behavior, use `spriteModule` config option to specify module path of your sprite implementation.
21You can extend a default [`lib/web/sprite.js`](lib/web/sprite.js), or create your own.
22In the latter case you only need to implement the `add` method that accepts the symbol data as a string.
23
24## Installation
25
26```bash
27npm install svg-sprite-loader --save-dev
28```
29
30## Example config
31```js
32module.exports = {
33 module: {
34 loaders: [{
35 test: /\.svg$/,
36 loader: 'svg-sprite?' + JSON.stringify({
37 name: '[name]_[hash]',
38 prefixize: true,
39 spriteModule: 'utils/my-custom-sprite'
40 })
41 }]
42 }
43};
44```
45
46## Configuration
47
48* `name` configures a custom symbol id naming. Default is `[name]`. Following name patterns are supported:
49 * `[ext]` the extension of the image.
50 * `[name]` the basename of the image.
51 * `[path]` the path of the image.
52 * `[hash]` the hash or the image content.
53 * `[pathhash]` the hash or the image path.
54* `prefixize` isolates an image content by prefixing its `id`, `xlink:href` and `url(#id)` elements. Default is `true`.
55* `spriteModule` defines [custom sprite implementation](#custom-sprite-implementation) module path.
56
57## Examples
58
59Single image
60```js
61var id = require('svg-sprite!./image.svg');
62// => 'image'
63```
64
65Set of images
66```js
67var files = require.context('svg-sprite!images/logos', false, /(twitter|facebook|youtube)\.svg$/);
68files.keys().forEach(files);
69```
70
71Custom sprite behavior
72```js
73// my-sprite.js
74var Sprite = require('node_modules/svg-sprite-loader/lib/web/sprite');
75module.exports = new Sprite();
76
77// my-app.jsx
78var sprite = require('my-sprite');
79
80class MyApplication extends React.Component {
81 componentWillMount() {
82 sprite.elem = sprite.render(document.body);
83 }
84
85 componentWillUnmount() {
86 sprite.elem.parentNode.removeChild(sprite.elem);
87 }
88}
89```
90
91Using with React
92```js
93// icon.jsx
94var GLYPHS = {
95 PONY: require('img/pony.svg'),
96 UNICORN: require('img/unicorn.svg')
97};
98
99class Icon extends React.Component {
100 render() {
101 var glyph = this.props.glyph;
102 return (
103 <svg className="icon" dangerouslySetInnerHTML={{__html: '<use xlink:href="#' + glyph + '"></use>'}}/>
104 )
105 }
106}
107
108module.exports = Icon;
109module.exports.GLYPHS = GLYPHS;
110
111// some-component.jsx
112var Icon = require('components/icon');
113<Icon glyph={Icon.GLYPHS.UNICORN}>
114```