UNPKG

2.8 kBMarkdownView Raw
1# [svgo](https://github.com/svg/svgo) loader for [webpack](https://github.com/webpack/webpack)
2
3## Install
4
5```
6$ npm install svgo svgo-loader --save-dev
7```
8
9... or with Yarn
10
11```
12$ yarn add svgo svgo-loader -D
13```
14
15DON'T FORGET TO INSTALL / UPDATE THE `svgo` PACKAGE after you update `svg-loader` (see [#20](https://github.com/rpominov/svgo-loader/issues/20))
16
17## Usage
18
19[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
20
21Svgo-loader just passes config to the [svgo](https://github.com/svg/svgo) library.
22
23### Put the SVGO config into loader's `options`
24
25``` javascript
26module.exports = {
27 ...,
28 module: {
29 rules: [
30 {
31 test: /\.svg$/,
32 use: [
33 {loader: 'file-loader'},
34 {
35 loader: 'svgo-loader',
36 options: {
37 plugins: [
38 {removeTitle: true},
39 {convertColors: {shorthex: false}},
40 {convertPathData: false}
41 ]
42 }
43 }
44 ]
45 }
46 ]
47 }
48}
49```
50
51### Or use an external config like you would with SVGO CLI
52
53``` javascript
54module.exports = {
55 ...,
56 module: {
57 rules: [
58 {
59 test: /\.svg$/,
60 use: [
61 {loader: 'file-loader'},
62 {
63 loader: 'svgo-loader',
64 options: {
65 externalConfig: "svgo-config.yml"
66 }
67 }
68 ]
69 }
70 ]
71 }
72}
73```
74
75In `svgo-config.yml`:
76
77```yml
78plugins:
79 - removeTitle: true
80 - convertPathData: false
81 - convertColors:
82 shorthex: false
83```
84
85You can use `YML` or `JSON` files as external configs.
86
87### Legacy Webpack v1 config
88
89There are two ways of loading svgo configuration.
90You can pass it as a JSON string after loader name, like this:
91
92``` javascript
93// webpack.config.js
94
95var svgoConfig = JSON.stringify({
96 plugins: [
97 {removeTitle: true},
98 {convertColors: {shorthex: false}},
99 {convertPathData: false}
100 ]
101});
102
103module.exports = {
104 ...
105 module: {
106 loaders: [
107 {
108 test: /.*\.svg$/,
109 loaders: [
110 'file-loader',
111 'svgo-loader?' + svgoConfig
112 ]
113 }
114 ]
115 }
116}
117```
118
119Or you can save svgo config in your main webpack config object,
120and then specify name of the property in the loader query string.
121
122However, this option will not work in Webpack 2.<br>
123This is only shown here in the documentation for backwards compatibility.
124
125``` javascript
126// webpack.config.js
127
128module.exports = {
129 ...
130 module: {
131 loaders: [
132 {
133 test: /.*\.svg$/,
134 loaders: [
135 'file-loader',
136 'svgo-loader?useConfig=svgoConfig1'
137 ]
138 }
139 ]
140 },
141 svgoConfig1: {
142 plugins: [
143 {removeTitle: true},
144 {convertColors: {shorthex: false}},
145 {convertPathData: false}
146 ]
147 }
148}
149```