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 |
|
15 | DON'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 |
|
21 | Svgo-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
|
26 | module.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
|
54 | module.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 |
|
75 | In `svgo-config.yml`:
|
76 |
|
77 | ```yml
|
78 | plugins:
|
79 | - removeTitle: true
|
80 | - convertPathData: false
|
81 | - convertColors:
|
82 | shorthex: false
|
83 | ```
|
84 |
|
85 | You can use `YML` or `JSON` files as external configs.
|
86 |
|
87 | ### Legacy Webpack v1 config
|
88 |
|
89 | There are two ways of loading svgo configuration.
|
90 | You can pass it as a JSON string after loader name, like this:
|
91 |
|
92 | ``` javascript
|
93 | // webpack.config.js
|
94 |
|
95 | var svgoConfig = JSON.stringify({
|
96 | plugins: [
|
97 | {removeTitle: true},
|
98 | {convertColors: {shorthex: false}},
|
99 | {convertPathData: false}
|
100 | ]
|
101 | });
|
102 |
|
103 | module.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 |
|
119 | Or you can save svgo config in your main webpack config object,
|
120 | and then specify name of the property in the loader query string.
|
121 |
|
122 | However, this option will not work in Webpack 2.<br>
|
123 | This is only shown here in the documentation for backwards compatibility.
|
124 |
|
125 | ``` javascript
|
126 | // webpack.config.js
|
127 |
|
128 | module.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 | ```
|