You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+67-44Lines changed: 67 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -124,59 +124,82 @@ If an alias was in place for the images directory, i.e.
124
124
```
125
125
Then the svg can be inlined with: `<img inline src="~img/icons.svg">`. This method would require the use of **loaders** on your templates as shown above in point 2.
126
126
127
-
#### Setting `runPreEmit` option
128
-
If you aren't using **loaders** to resolve file locations, and would prefer to reference image paths relative to the **root** of your project (where your `package.json`file resides) then set the plugins `runPreEmit` config option to `true`:
127
+
#### Duplicated attributes
128
+
All the attributes of a `<img/>` element excepting `src` and `inline` will be copied to the inlined `<svg/>` element. Attributes like `id` or `class` will be copied to the resulting root of the `<svg/>` element and if the original SVG file already had these attributes they will be duplicated (and not replaced) on the resulting `<svg/>` element, though the attributes coming from the `<img/>` will appear first and [any subsequent duplicated attribute from the original SVG will be ignored by the browser](https://stackoverflow.com/questions/26341507/can-an-html-element-have-the-same-attribute-twice).
129
129
130
-
```javascript
131
-
plugins: [
132
-
newHtmlWebpackPlugin(),
133
-
newHtmlWebpackInlineSVGPlugin({
134
-
runPreEmit:true,
135
-
})
136
-
]
137
-
```
138
-
139
-
The plugin will now run prior to **html-webpack-plugin** saving your templates to your output directory. It will also expect all `<img inline`**src** attributes to be relative to your `package.json` file.
140
-
141
-
Therefore with the above project structure, and `runPreEmit` set to `true`, inlining icons.svg would look like:
130
+
For example:
142
131
143
132
```html
144
-
<imginlinesrc="src/images/icons.svg">
145
-
```
146
-
147
-
## Config
133
+
<imginlinesrc="images/icons.svg"id="myImageIMG"class="square"> <!-- img element to be replaced -->
148
134
149
-
#### HtmlWebpackInlineSVGPlugin Options
150
-
151
-
```javascript
152
-
newHtmlWebpackInlineSVGPlugin({
153
-
// default: false
154
-
// described above
155
-
runPreEmit: {Boolean},
156
-
157
-
// default: false
158
-
// if true will inline all svgs within the parsed html
159
-
inlineAll: {Boolean},
160
-
})
135
+
<svgid="myImageSVG">...</svg> <!-- icons.svg file to be inlined -->
161
136
```
162
137
163
-
#### SVGO
138
+
will result in:
164
139
165
-
To configure SVGO (module used to optimise your SVGs), add an `svgoConfig` object to your `html-webpack-plugin` config:
For a full list of the SVGO config (default) params we are using check out: [svgo-config.js](svgo-config.js). The config you set is merged with our defaults, it does not replace it.
144
+
The broswer will use `id=""myImageIMG"` and not `id="myImageSVG"`. It's however a better approach if you avoid having any duplicated attribute at all and only putting the required ones on the `<img>` element.
145
+
146
+
## Config options
147
+
148
+
The plugin accepts three options:
149
+
150
+
-`runPreEmit`: defaults to `false`. If you aren't using **loaders** to resolve file locations, and would prefer to reference image paths relative to the **root** of your project (where your `package.json` file resides) then set the plugins `runPreEmit` config option to `true`:
151
+
152
+
```javascript
153
+
plugins: [
154
+
newHtmlWebpackPlugin(),
155
+
newHtmlWebpackInlineSVGPlugin({
156
+
runPreEmit:true,
157
+
})
158
+
]
159
+
```
160
+
161
+
The plugin will now run prior to **html-webpack-plugin** saving your templates to your output directory. It will also expect all `<img inline`**src** attributes to be relative to your `package.json` file.
162
+
163
+
Therefore with the above project structure, and `runPreEmit` set to `true`, inlining icons.svg would look like:
164
+
165
+
```html
166
+
<imginlinesrc="src/images/icons.svg">
167
+
```
168
+
169
+
-`inlineAll`: defaults to `false`. It will inline all SVG images on the template without the need of the `inline` attribute on every image:
170
+
```javascript
171
+
plugins: [
172
+
newHtmlWebpackPlugin(),
173
+
newHtmlWebpackInlineSVGPlugin({
174
+
inlineAll:true
175
+
})
176
+
]
177
+
```
178
+
179
+
If `inlineAll` option is enabled you can use the `inline-exclude` attribute to exclude a particular image from being inlined:
180
+
181
+
```html
182
+
<div>
183
+
<imgsrc="src/images/icon1.svg"> <!-- it will be inlined -->
184
+
<imginline-excludesrc="src/images/icon2.svg"> <!-- it won't be inlined -->
185
+
</div>
186
+
```
187
+
188
+
-`svgoConfig`: to configure SVGO (module used to optimise your SVGs), add an `svgoConfig` object to your `html-webpack-plugin` config:
189
+
190
+
```javascript
191
+
plugins: [
192
+
newHtmlWebpackPlugin({
193
+
svgoConfig: {
194
+
removeTitle:false,
195
+
removeViewBox:true,
196
+
},
197
+
}),
198
+
newHtmlWebpackInlineSVGPlugin()
199
+
]
200
+
```
201
+
202
+
For a full list of the SVGO config (default) params we are using check out: [svgo-config.js](svgo-config.js). The config you set is merged with our defaults, it does not replace it.
0 commit comments