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
#### Convert .svg files into inline SVG tags within the output html of templates parsed by [html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin).
5
+
Converts .svg files into inlined `<svg>` tags within the output html of templates parsed by [html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin).
1.[Incorrect File Paths And URLs](#incorrect-file-paths-and-urls)
18
+
1.[Duplicated Attributes](#duplicated-attributes)
19
+
1.[Config](#config)
20
+
1.[Versions](#versions)
21
+
1.[Contribution](#contribution)
22
+
1.[License](#license)
24
23
25
-
The latest version of this package supports webpack 4. All versions marked v2.x.x will target webpack 4 and html-webpack-plugin v4.
26
24
27
-
For webpack 3 and html-webpack-plugin v3 support use v1.3.0 of this package.
28
-
29
-
### v2.x.x
30
-
Support webpack v4
31
-
Support html-webpack-plugin v4
32
-
33
-
### v1.3.0
34
-
Support webpack v3
35
-
Support html-webpack-plugin v3
36
-
37
-
# Overview
25
+
## Overview
38
26
39
27
By inlining SVGs you can combine them with techniques such as: [Icon System with SVG Sprites](https://css-tricks.com/svg-sprites-use-better-icon-fonts/).
40
28
41
29
As of version 1.0.0 **by default** this plugin processes SVG files after all template and image files have been written to their corresponding output directory. This allows it to work alongside loaders, after webpack resolves all file locations.
42
30
43
-
> Please note: to use **aliases** you will need to install loaders to resolve your svg paths and parse the templates html. More info is provided below: [Getting to your SVGs](#getting-to-your-svgs).
31
+
> Please note: to use **aliases** you will need to install loaders to resolve your svg paths and parse the templates html. More info is provided below: [Getting To Your SVGs](#getting-to-your-svgs).
44
32
45
33
**As of version 1.1.0** the plugin can also be run prior to the output of your templates. This allows you to reference image files from the root of your project which can help with getting to certain files, i.e. within your `node_modules` directory. More info is provided below: [Setting runPreEmit option](#setting-runpreemit-option).
46
34
47
-
The plugin relies on [svgo](https://github.com/svg/svgo) to optimise SVGs. You can configure it's settings, check [config](#config) for more details.
35
+
The plugin relies on [svgo](https://github.com/svg/svgo) to optimise SVGs. You can configure it's settings, check [Config](#config) for more details.
36
+
37
+
38
+
## Features
39
+
40
+
* Optimises/minimizes the output SVG.
41
+
* Allows for deep nested SVGs.
42
+
* Supports webpack aliases for file locations.
43
+
* Ignores broken tags (in case you are outputting templates for various parts of the page).
44
+
* Performs no html decoding (supports language tags, i.e. `<?php echo 'foo bar'; ?>`).
45
+
* Can load image files locally and from an online URL with the `allowFromUrl` option.
@@ -88,32 +87,35 @@ Add `img` tags with `inline` attribute and `.svg` file as src to your template/s
88
87
<imginlinesrc="images/i-will-be-ignored.png">
89
88
```
90
89
91
-
### Getting to your SVGs
90
+
### Getting To Your SVGs
92
91
93
92
> Breaking change: As of version 1.0.0 the plugin waits for webpack to resolve image locations and write them to disk. If you were using a version prior to 1.0.0 then it is likely you'll need to update the src paths to your inline SVGs to reflect this change. See below for more info.
94
93
95
-
There are **three ways** of working with your `<img>`**src** attributes and this plugin.
94
+
There are three ways of working with your `<img>`**src** attributes and this plugin:
95
+
96
+
1. If you are **not working with loaders** to allow webpack to parse and resolve the `img` tags `src` attributes within your *html-webpack-plugin* templates. Use paths that are relative to your **svg** images from the **output** location of the template that is referencing it.
97
+
98
+
1.**Alternatively use loaders** such as [html-loader](https://github.com/webpack-contrib/html-loader) to parse the html templates, and [file-loader](https://github.com/webpack-contrib/file-loader) or something similar, to resolve the paths of your `img` tags `src` attributes. As the plugin works after webpack has emitted all its assets and *html-webpack-plugin* has output your templates, it will read the SVGs that webpack places in your output directory, and replace any **inlined img tags** with this content.
96
99
97
-
1. If you are **not working with loaders** to allow webpack to parse and resolve the `img` tags `src` attributes within your *html-webpack-plugin* templates. Use paths that are relative to your **svg** images from the **output** location of the template that is referencing it.
98
-
2.**Alternatively use loaders** such as [html-loader](https://github.com/webpack-contrib/html-loader) to parse the html templates, and [file-loader](https://github.com/webpack-contrib/file-loader) or something similar, to resolve the paths of your `img` tags `src` attributes. As the plugin works after webpack has emitted all its assets and *html-webpack-plugin* has output your templates, it will read the SVGs that webpack places in your output directory, and replace any **inlined img tags** with this content.
99
-
3.**Set the `runPreEmit` flag** and reference files relative to your `package.json` file. This feature is only available with version >= 1.1.0. More info is provided below: [Setting runPreEmit option](#setting-runpreemit-option).
100
+
1.**Set the `runPreEmit` flag** and reference files relative to your `package.json` file. This feature is only available with version >= 1.1.0. More info is provided below: [Setting runPreEmit option](#setting-runpreemit-option).
With the above structure inlining `icons.svg` would look like:
117
119
```html
118
120
<imginlinesrc="images/icons.svg">
119
121
```
@@ -124,10 +126,10 @@ If an alias was in place for the images directory, i.e.
124
126
```
125
127
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
128
127
-
####Incorrect file paths or URLs
129
+
### Incorrect file paths and URLs
128
130
If for any reason the path to a local SVG file is incorrect, or the file fails to be read, or an image retrieved with an URL fails to download, the webpack build process will fail with an error, like `ENOENT`.
129
131
130
-
####Duplicated attributes
132
+
### Duplicated attributes
131
133
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).
132
134
133
135
For example:
@@ -146,9 +148,10 @@ will result in:
146
148
147
149
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.
148
150
149
-
## Config options
150
151
151
-
The plugin accepts three options:
152
+
## Config
153
+
154
+
The plugin accepts the below options:
152
155
153
156
-`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`:
154
157
@@ -170,6 +173,7 @@ The plugin accepts three options:
170
173
```
171
174
172
175
-`inlineAll`: defaults to `false`. It will inline all SVG images on the template without the need of the `inline` attribute on every image:
176
+
173
177
```javascript
174
178
plugins: [
175
179
newHtmlWebpackPlugin(),
@@ -188,8 +192,19 @@ The plugin accepts three options:
188
192
</div>
189
193
```
190
194
191
-
-`allowFromUrl`: defaults to `false`. It allows to use SVG images coming from an URL online in addition to local files. For example:
195
+
-`allowFromUrl`: defaults to `false`. It allows to use SVG images coming from an URL online in addition to local files:
192
196
197
+
```javascript
198
+
plugins: [
199
+
newHtmlWebpackPlugin(),
200
+
newHtmlWebpackInlineSVGPlugin({
201
+
allowFromUrl:true
202
+
})
203
+
]
204
+
```
205
+
206
+
For example:
207
+
193
208
```html
194
209
<div>
195
210
<imginlinesrc="https://badge.fury.io/js/html-webpack-inline-svg-plugin.svg"> <!-- it will be inlined from the online SVG -->
@@ -212,20 +227,29 @@ The plugin accepts three options:
212
227
213
228
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.
214
229
215
-
## Features
216
230
217
-
* Optimises / minimizes the output SVG
218
-
* Allows for deep nested SVGs
219
-
* Supports webpack aliases for file locations
220
-
* Ignores broken tags - incase you are outputting templates for various parts of the page
221
-
* Performs no html decoding so supports language tags, i.e. `<?php echo 'foo bar'; ?>`
231
+
## Versions
232
+
233
+
The latest version of this package supports webpack 4. All versions marked v2.x.x will target webpack 4 and html-webpack-plugin v4.
234
+
235
+
For webpack 3 and html-webpack-plugin v3 support use v1.3.0 of this package.
236
+
237
+
### v2.x.x
238
+
- Support webpack v4.
239
+
- Support html-webpack-plugin v4.
240
+
241
+
### v1.3.0
242
+
- Support webpack v3.
243
+
- Support html-webpack-plugin v3.
244
+
222
245
223
246
## Contribution
224
247
225
248
You're free to contribute to this project by submitting issues and/or pull requests. This project is test-driven, so keep in mind that every change and new feature should be covered by tests.
226
249
227
250
I'm happy for someone to take over the project as I don't find myself using it anylonger due to changes in workflow. Therefore others are likely in a better position to support this project and roll out the right enhancements.
228
251
252
+
229
253
## License
230
254
231
-
This project is licensed under [MIT](https://github.com/theGC/html-webpack-inline-svg-plugin/blob/master/LICENSE).
255
+
This project is licensed under [MIT](https://github.com/theGC/html-webpack-inline-svg-plugin/blob/master/LICENSE).
0 commit comments