Skip to content

Commit e11ecd1

Browse files
committed
Merge branch 'release/2.1.0'
2 parents 713200d + 5079cde commit e11ecd1

13 files changed

Lines changed: 435 additions & 249 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/node_modules/
22
/dist/
33
npm-debug.log
4+
.history

README.md

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -124,59 +124,82 @@ If an alias was in place for the images directory, i.e.
124124
```
125125
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.
126126

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).
129129

130-
```javascript
131-
plugins: [
132-
new HtmlWebpackPlugin(),
133-
new HtmlWebpackInlineSVGPlugin({
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:
142131

143132
```html
144-
<img inline src="src/images/icons.svg">
145-
```
146-
147-
## Config
133+
<img inline src="images/icons.svg" id="myImageIMG" class="square"> <!-- img element to be replaced -->
148134

149-
#### HtmlWebpackInlineSVGPlugin Options
150-
151-
```javascript
152-
new HtmlWebpackInlineSVGPlugin({
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+
<svg id="myImageSVG">...</svg> <!-- icons.svg file to be inlined -->
161136
```
162137

163-
#### SVGO
138+
will result in:
164139

165-
To configure SVGO (module used to optimise your SVGs), add an `svgoConfig` object to your `html-webpack-plugin` config:
166-
167-
```javascript
168-
plugins: [
169-
new HtmlWebpackPlugin({
170-
svgoConfig: {
171-
removeTitle: false,
172-
removeViewBox: true,
173-
},
174-
}),
175-
new HtmlWebpackInlineSVGPlugin()
176-
]
140+
```html
141+
<svg id="myImageIMG" class="square" id="myImageSVG">...</svg>
177142
```
178143

179-
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+
new HtmlWebpackPlugin(),
155+
new HtmlWebpackInlineSVGPlugin({
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+
<img inline src="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+
new HtmlWebpackPlugin(),
173+
new HtmlWebpackInlineSVGPlugin({
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+
<img src="src/images/icon1.svg"> <!-- it will be inlined -->
184+
<img inline-exclude src="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+
new HtmlWebpackPlugin({
193+
svgoConfig: {
194+
removeTitle: false,
195+
removeViewBox: true,
196+
},
197+
}),
198+
new HtmlWebpackInlineSVGPlugin()
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.
180203

181204
## Features
182205

index.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,8 @@ class HtmlWebpackInlineSVGPlugin {
1818

1919
constructor (options) {
2020

21-
if (options) {
22-
23-
if (options.runPreEmit) this.runPreEmit = true
24-
25-
if (options.inlineAll) this.inlineAll = true
26-
27-
}
28-
21+
this.runPreEmit = _.get(options, 'runPreEmit', false)
22+
this.inlineAll = _.get(options, 'inlineAll', false)
2923
this.userConfig = ''
3024
this.outputPath = ''
3125

@@ -83,8 +77,8 @@ class HtmlWebpackInlineSVGPlugin {
8377
})
8478

8579
} else {
86-
87-
HtmlWebpackPlugin
80+
// https://github.com/jantimon/html-webpack-plugin/issues/1091
81+
compiler.options.plugins.find((plugin) => plugin.constructor.name === 'HtmlWebpackPlugin').constructor
8882
.getHooks(compilation)
8983
.beforeEmit
9084
.tapAsync('HtmlWebpackInlineSVGPlugin', (htmlPluginData, callback) => {
@@ -412,8 +406,9 @@ class HtmlWebpackInlineSVGPlugin {
412406
*/
413407
isNodeValidInlineImage (node) {
414408

415-
return !!(node.nodeName === 'img'
416-
&& (this.inlineAll
409+
return !!(
410+
node.nodeName === 'img'
411+
&& ((this.inlineAll && _.filter(node.attrs, { name: 'inline-exclude' }).length === 0)
417412
|| _.filter(node.attrs, { name: 'inline' }).length)
418413
&& this.getImagesSrc(node))
419414

@@ -525,7 +520,7 @@ class HtmlWebpackInlineSVGPlugin {
525520
return name !== 'inline'
526521
&& name !== 'src'
527522
? acc + `${name}="${value}" `
528-
: ''
523+
: acc
529524

530525
}, '')
531526

0 commit comments

Comments
 (0)