995a7f16d6335d5a3f5ac1453c4d58fdb1adea7652e2fc5f7e94672c15a55ee7931203bae7598eca8ca5f453ef607bc789bf94a2f16fffb2fb2709ee013a63 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. # SVG sprite loader
  2. [![NPM version][version-img]][versions-img] [![Build status][ci-img]][ci-url] [![Documentation score][docs-coverage-img]][docs-coverage-url] [![Dependencies status][deps-img]][deps-url] [![Dev dependencies status][dev-deps-img]][dev-deps-url] [![NPM downloads][downloads-img]][npm-url]
  3. Webpack loader for creating SVG sprites.
  4. > :tada: 2.0 is out, please read the [migration guide & overview](2.0.md).
  5. > :warning: For old v0.x versions see [the README in the v0 branch](https://github.com/JetBrains/svg-sprite-loader/blob/v0/README.md).
  6. ## Table of contents
  7. - [Why it's cool](#why-its-cool)
  8. - [Installation](#installation)
  9. - [Configuration](#configuration)
  10. - [`symbolId`](#symbol-id)
  11. - [`symbolRegExp`](#symbol-regexp)
  12. - [`esModule`](#es-module)
  13. - [Runtime configuration](#runtime-configuration)
  14. - [`spriteModule`](#sprite-module)
  15. - [`symbolModule`](#symbol-module)
  16. - [`runtimeGenerator`](#runtime-generator)
  17. - [`runtimeCompat`](#runtime-compat) (deprecated)
  18. - [`runtimeOptions`](#runtime-options)
  19. - [Extract configuration](#extract-configuration)
  20. - [`extract`](#extract)
  21. - [`spriteFilename`](#sprite-filename)
  22. - [`publicPath`](#public-path)
  23. - [`outputPath`](#output-path)
  24. - [`plainSprite`](#plain-sprite)
  25. - [`spriteAttrs`](#sprite-attrs)
  26. - [Examples](#examples)
  27. - [Contributing guidelines](#contributing-guidelines)
  28. - [License](#license)
  29. - [Credits](#credits)
  30. ## Why it's cool
  31. - **Minimum initial configuration**. Most of the options are configured automatically.
  32. - **Runtime for browser**. Sprites are rendered and injected in pages automatically, you just refer to images via `<svg><use xlink:href="#id"></use></svg>`.
  33. - **Isomorphic runtime for node/browser**. Can render sprites on server or in browser manually.
  34. - **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.
  35. - **External sprite file** is generated for images imported from css/scss/sass/less/styl/html ([SVG stacking technique](https://css-tricks.com/svg-fragment-identifiers-work/#article-header-id-4)).
  36. ## Installation
  37. ```bash
  38. npm install svg-sprite-loader -D
  39. # via yarn
  40. yarn add svg-sprite-loader -D
  41. ```
  42. ## Configuration
  43. ```js
  44. // webpack 1
  45. {
  46. test: /\.svg$/,
  47. loader: 'svg-sprite-loader',
  48. query: { ... }
  49. }
  50. // webpack 1 multiple loaders
  51. {
  52. test: /\.svg$/,
  53. loaders: [
  54. `svg-sprite-loader?${JSON.stringify({ ... })}`,
  55. 'svg-transform-loader',
  56. 'svgo-loader'
  57. ]
  58. }
  59. // webpack >= 2
  60. {
  61. test: /\.svg$/,
  62. loader: 'svg-sprite-loader',
  63. options: { ... }
  64. }
  65. // webpack >= 2 multiple loaders
  66. {
  67. test: /\.svg$/,
  68. use: [
  69. { loader: 'svg-sprite-loader', options: { ... } },
  70. 'svg-transform-loader',
  71. 'svgo-loader'
  72. ]
  73. }
  74. ```
  75. <a id="symbol-id"></a>
  76. ### `symbolId` (`string | function(path, query)`, default `[name]`)
  77. How `<symbol>` `id` attribute should be named. All patterns from [loader-utils#interpolateName](https://github.com/webpack/loader-utils#interpolatename)
  78. are supported. Also can be a function which accepts 2 args - file path and query string and return symbol id:
  79. ```js
  80. {
  81. symbolId: filePath => path.basename(filePath)
  82. }
  83. ```
  84. <a id="symbol-regexp"></a>
  85. ### `symbolRegExp` (default `''`)
  86. Passed to the symbolId interpolator to support the [N] pattern in the loader-utils name interpolator
  87. <a id="es-module"></a>
  88. ### `esModule` (default `true`, autoconfigured)
  89. Generated export format:
  90. - when `true` loader will produce `export default ...`.
  91. - when `false` the result is `module.exports = ...`.
  92. By default depends on used webpack version: `true` for webpack >= 2, `false` otherwise.
  93. ## Runtime configuration
  94. When you require an image, loader transforms it to SVG `<symbol>`, adds it to the special sprite storage and returns class instance
  95. that represents symbol. It contains `id`, `viewBox` and `content` (`id`, `viewBox` and `url` in extract mode)
  96. fields and can later be used for referencing the sprite image, e.g:
  97. ```js
  98. import twitterLogo from './logos/twitter.svg';
  99. // twitterLogo === SpriteSymbol<id: string, viewBox: string, content: string>
  100. // Extract mode: SpriteSymbol<id: string, viewBox: string, url: string, toString: Function>
  101. const rendered = `
  102. <svg viewBox="${twitterLogo.viewBox}">
  103. <use xlink:href="#${twitterLogo.id}" />
  104. </svg>`;
  105. ```
  106. When browser event `DOMContentLoaded` is fired, sprite will be automatically rendered and injected in the `document.body`.
  107. If custom behaviour is needed (e.g. a different mounting target) default sprite module could be overridden via `spriteModule` option. Check example below.
  108. <a id="sprite-module"></a>
  109. ### `spriteModule` (autoconfigured)
  110. Path to sprite module that will be compiled and executed at runtime.
  111. By default it depends on [`target`](https://webpack.js.org/configuration/target) webpack config option:
  112. - `svg-sprite-loader/runtime/browser-sprite.build` for 'web' target.
  113. - `svg-sprite-loader/runtime/sprite.build` for other targets.
  114. If you need custom behavior, use this option to specify a path of your sprite implementation module.
  115. 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`.
  116. Example of sprite with custom mounting target (copypasted from [browser-sprite](https://github.com/JetBrains/svg-sprite-loader/blob/master/runtime/browser-sprite.js)):
  117. ```js
  118. import BrowserSprite from 'svg-baker-runtime/src/browser-sprite';
  119. import domready from 'domready';
  120. const sprite = new BrowserSprite();
  121. domready(() => sprite.mount('#my-custom-mounting-target'));
  122. export default sprite; // don't forget to export!
  123. ```
  124. It's highly recommended to extend default sprite classes:
  125. - [for browser-specific env](https://github.com/JetBrains/svg-baker/blob/master/packages/svg-baker-runtime/src/browser-sprite.js)
  126. - [for isomorphic env](https://github.com/JetBrains/svg-baker/blob/master/packages/svg-baker-runtime/src/sprite.js)
  127. <a id="symbol-module"></a>
  128. ### `symbolModule` (autoconfigured)
  129. Same as `spriteModule`, but for sprite symbol. By default also depends on `target` webpack config option:
  130. - `svg-baker-runtime/browser-symbol` for 'web' target.
  131. - `svg-baker-runtime/symbol` for other targets.
  132. <a id="runtime-generator"></a>
  133. ### `runtimeGenerator` ([default generator](https://github.com/JetBrains/svg-sprite-loader/blob/master/lib/runtime-generator.js))
  134. Path to node.js script that generates client runtime.
  135. Use this option if you need to produce your own runtime, e.g. React component configured with imported symbol. [Example](https://github.com/JetBrains/svg-sprite-loader/tree/master/examples/custom-runtime-generator).
  136. <a id="runtime-compat"></a>
  137. ### `runtimeCompat` (default `false`, deprecated)
  138. Should runtime be compatible with earlier v0.x loader versions. This option will be removed in the next major version release.
  139. <a id="runtime-options"></a>
  140. ### `runtimeOptions`
  141. Arbitrary data passed to runtime generator. Reserved for future use when other runtime generators will be created.
  142. ## Extract configuration
  143. In the extract mode loader should be configured with plugin, otherwise an error is thrown. Example:
  144. ```js
  145. // webpack.config.js
  146. const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');
  147. ...
  148. {
  149. plugins: [
  150. new SpriteLoaderPlugin()
  151. ]
  152. }
  153. ```
  154. <a id="extract"></a>
  155. ### `extract` (default `false`, autoconfigured)
  156. Switches loader to the extract mode.
  157. Enabled automatically for images imported from css/scss/sass/less/styl/html files.
  158. <a id="sprite-filename"></a>
  159. ### `spriteFilename` (type `string|Function<string>`,default `sprite.svg`)
  160. Filename of extracted sprite. Multiple sprites can be generated by specifying different loader rules restricted with `include` option or
  161. by providing custom function which recieves SVG file absolute path, e.g.:
  162. ```js
  163. {
  164. test: /\.svg$/,
  165. loader: 'svg-sprite-loader',
  166. options: {
  167. extract: true,
  168. spriteFilename: svgPath => `sprite${svgPath.substr(-4)}`
  169. }
  170. }
  171. ```
  172. `[hash]` in sprite filename will be replaced by it's content hash.
  173. It is also possible to generate sprite for each chunk by using `[chunkname]` pattern in spriteFilename option. This is experimental feature, use it with caution!
  174. <a id="public-path"></a>
  175. ### `publicPath` (type: `string`, default: `__webpack_public_path__`)
  176. Custom public path for sprite file.
  177. ```js
  178. {
  179. test: /\.svg$/,
  180. loader: 'svg-sprite-loader',
  181. options: {
  182. extract: true,
  183. publicPath: '/'
  184. }
  185. }
  186. ```
  187. <a id="output-path"></a>
  188. ### `outputPath` (type: `string`, default: null`)
  189. Custom output path for sprite file.
  190. By default it will use `publicPath`.
  191. This param is useful if you want to store sprite is a directory with a custom http access.
  192. ```js
  193. {
  194. test: /\.svg$/,
  195. loader: 'svg-sprite-loader',
  196. options: {
  197. extract: true,
  198. outputPath: 'custom-dir/sprites/'
  199. publicPath: 'sprites/'
  200. }
  201. }
  202. ```
  203. <a id="plain-sprite"></a>
  204. ### Plain sprite
  205. You can render plain sprite in extract mode without styles and usages. Pass `plainSprite: true` option to plugin constructor:
  206. ```js
  207. {
  208. plugins: [
  209. new SpriteLoaderPlugin({ plainSprite: true })
  210. ]
  211. }
  212. ```
  213. <a id="sprite-attrs"></a>
  214. ### Sprite attributes
  215. Sprite `<svg>` tag attributes can be specified via `spriteAttrs` plugin option:
  216. ```js
  217. {
  218. plugins: [
  219. new SpriteLoaderPlugin({
  220. plainSprite: true,
  221. spriteAttrs: {
  222. id: 'my-custom-sprite-id'
  223. }
  224. })
  225. ]
  226. }
  227. ```
  228. ## Examples
  229. See [examples](examples) folder.
  230. ## Contributing guidelines
  231. See [CONTRIBUTING.md](CONTRIBUTING.md).
  232. ## License
  233. See [LICENSE](LICENSE)
  234. ## Credits
  235. Huge thanks for [all this people](https://github.com/JetBrains/svg-sprite-loader/graphs/contributors).
  236. [npm-url]: https://www.npmjs.com/package/svg-sprite-loader
  237. [version-img]: https://img.shields.io/npm/v/svg-sprite-loader.svg?style=flat-square
  238. [versions-img]: https://libraries.io/npm/svg-sprite-loader/versions
  239. [downloads-img]: https://img.shields.io/npm/dm/svg-sprite-loader.svg?style=flat-square
  240. [deps-url]: https://david-dm.org/JetBrains/svg-sprite-loader
  241. [deps-img]: https://img.shields.io/david/JetBrains/svg-sprite-loader.svg?style=flat-square
  242. [dev-deps-url]: https://david-dm.org/JetBrains/svg-sprite-loader?type=dev
  243. [dev-deps-img]: https://img.shields.io/david/dev/JetBrains/svg-sprite-loader.svg?style=flat-square
  244. [ci-url]: https://travis-ci.org/JetBrains/svg-sprite-loader
  245. [ci-img]: https://img.shields.io/travis/JetBrains/svg-sprite-loader.svg?style=flat-square
  246. [docs-coverage-url]: https://inch-ci.org/github/JetBrains/svg-sprite-loader
  247. [docs-coverage-img]: https://inch-ci.org/github/JetBrains/svg-sprite-loader.svg?branch=master&style=flat-square