Skip to content

Commit d7aff06

Browse files
Merge pull request #34 from theGC/i27
v2.3.0 changes addressing issue 27
2 parents b1f273a + 4c9c453 commit d7aff06

11 files changed

+762
-923
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
## v2.3.0
2+
3+
* BREAKING CHANGE: On the webpack configuration file the `svgoConfig` option must now go inside `HtmlWebpackInlineSVGPlugin({})` instead of `HtmlWebpackPlugin({})`. An error is thrown at webpack build time otherwise.
4+
* The defaults for the `svgo` module aren't hardcoded anymore and –excepting the `cleanupIDs` option– the defaults are now set by the own module `svgo` and not `html-webpack-inline-svg-plugin`.
5+
16
## v2.2.0
27

3-
* _Ability added to load SVGs from an URL (`<img inline src="https://host.com/image.svg">`)._
8+
* Ability added to load SVGs from an URL (`<img inline src="https://host.com/image.svg">`).
49

510
## v2.0.1
611

README.md

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Converts .svg files into inlined `<svg>` tags within the output html of template
1313
1. [Usage](#usage)
1414
1. [Getting To Your SVGs](#getting-to-your-svgs)
1515
1. [Sample Project Structure](#sample-project-structure)
16-
1. [Default Config (not setting `runPreEmit` option)](#default-config-not-setting-runpreemit-option)
17-
1. [Incorrect File Paths And URLs](#incorrect-file-paths-and-urls)
16+
1. [Default Config](#default-config)
17+
1. [Incorrect File Paths Or URLs](#incorrect-file-paths-or-urls)
1818
1. [Duplicated Attributes](#duplicated-attributes)
1919
1. [Config](#config)
2020
1. [Versions](#versions)
@@ -28,11 +28,11 @@ By inlining SVGs you can combine them with techniques such as: [Icon System with
2828

2929
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.
3030

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).
31+
> Please note: to use **aliases** you will need to install loaders to resolve your svg paths and parse the templates html. Check [Getting To Your SVGs](#getting-to-your-svgs) section for more info.
3232
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).
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).
3434

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.
35+
The plugin relies on [SVGO](https://github.com/svg/svgo) to optimise SVGs. Check [Config](#config) for more details.
3636

3737

3838
## Features
@@ -48,13 +48,15 @@ The plugin relies on [svgo](https://github.com/svg/svgo) to optimise SVGs. You c
4848
## Installation
4949

5050
Install the plugin with npm:
51+
5152
```shell
5253
$ npm install --save-dev html-webpack-inline-svg-plugin
5354
```
5455

5556
or [Yarn](https://yarnpkg.com/):
57+
5658
```shell
57-
$ yarn add html-webpack-inline-svg-plugin --dev
59+
$ yarn add --dev html-webpack-inline-svg-plugin
5860
```
5961

6062
## Usage
@@ -74,7 +76,7 @@ plugins: [
7476
]
7577
```
7678

77-
Add `img` tags with `inline` attribute and `.svg` file as src to your template/s that the html-webpack-plugin is processing (the default is `index.html`).
79+
Add `img` tags with `inline` attribute and `.svg` file as `src` to your template/s that the html-webpack-plugin is processing (the default is `index.html`).
7880

7981
```html
8082
<!-- Works: below img tag will be removed and replaced by the content of the svg in its src -->
@@ -97,7 +99,7 @@ There are three ways of working with your `<img>` **src** attributes and this pl
9799

98100
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.
99101

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).
102+
1. **Set the `runPreEmit` flag** and reference files relative to your `package.json` file. This feature is only available with version >= 1.1.0. Check [Config](#config) and the `runPreEmit` option for more info.
101103

102104

103105
### Sample Project Structure
@@ -114,22 +116,28 @@ my-project
114116
└── foo.svg
115117
```
116118

117-
### Default Config (not setting `runPreEmit` option)
119+
### Default Config
120+
118121
With the above structure inlining `icons.svg` would look like:
122+
119123
```html
120124
<img inline src="images/icons.svg">
121125
```
122126

123127
If an alias was in place for the images directory, i.e.
128+
124129
```javascript
125130
'img': path.join(__dirname, 'src', 'images')
126131
```
132+
127133
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.
128134

129-
### Incorrect file paths and URLs
135+
### Incorrect file paths or URLs
136+
130137
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`.
131138

132139
### Duplicated attributes
140+
133141
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).
134142

135143
For example:
@@ -146,7 +154,7 @@ will result in:
146154
<svg id="myImageIMG" class="square" id="myImageSVG">...</svg>
147155
```
148156

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.
157+
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.
150158

151159

152160
## Config
@@ -211,21 +219,29 @@ The plugin accepts the below options:
211219
</div>
212220
```
213221

214-
- `svgoConfig`: defaults to `undefinded`. to configure SVGO (module used to optimise your SVGs), add an `svgoConfig` object to your `html-webpack-plugin` config:
222+
- `svgoConfig`: defaults to `[]`. [SVGO](https://github.com/svg/svgo) is used to optimise the SVGs inlined. You can configure SVGO by setting this `svgoConfig` array with the [SVGO plugins](https://github.com/svg/svgo#what-it-can-do) you need in the same way it's done in this [SVGO official Node.js example](https://github.com/svg/svgo/blob/master/examples/test.js).
223+
224+
Note `svgoConfig` is an array of `Object`s that will be assigned to the `.plugins` SVGO config variable by `html-webpack-inline-svg-plugin`. You don't need to pass an `Object` with a `plugins` property assigned an array of SVGO plugins, just pass the array:
215225

216226
```javascript
217227
plugins: [
218-
new HtmlWebpackPlugin({
219-
svgoConfig: {
220-
removeTitle: false,
221-
removeViewBox: true,
222-
},
223-
}),
224-
new HtmlWebpackInlineSVGPlugin()
228+
new HtmlWebpackPlugin(),
229+
new HtmlWebpackInlineSVGPlugin({
230+
svgoConfig: [
231+
{
232+
removeViewBox: false
233+
},
234+
{
235+
inlineStyles: {
236+
onlyMatchedOnce: false
237+
}
238+
}
239+
]
240+
})
225241
]
226242
```
227243

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.
244+
`html-webpack-inline-svg-plugin` modifies one SVGO default: `cleanupIDs`, from `true` to `false`, since IDs allow to reference individual symbols. You can still override this or any other SVGO plugin default configuration with this `svgoConfig` option.
229245

230246

231247
## Versions
@@ -245,9 +261,9 @@ For webpack 3 and html-webpack-plugin v3 support use v1.3.0 of this package.
245261

246262
## Contribution
247263

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.
264+
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 must be covered by tests.
249265

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.
266+
I'm happy for someone to take over the project as I don't find myself using it any longer due to changes in workflow. Therefore others are likely to be in a better position to support this project and roll out the right enhancements.
251267

252268

253269
## License

index.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const parse5 = require('parse5')
66
const _ = require('lodash')
77
const fs = require('fs')
88
const SVGO = require('svgo')
9-
const svgoDefaultConfig = require(path.resolve(__dirname, 'svgo-config.js'))
109
const HtmlWebpackPlugin = require('html-webpack-plugin')
1110
const axios = require('axios')
1211

@@ -22,7 +21,7 @@ class HtmlWebpackInlineSVGPlugin {
2221
this.runPreEmit = _.get(options, 'runPreEmit', false)
2322
this.inlineAll = _.get(options, 'inlineAll', false)
2423
this.allowFromUrl = _.get(options, 'allowFromUrl', false)
25-
this.userConfig = ''
24+
this.svgoConfig = _.get(options, 'svgoConfig', [])
2625
this.outputPath = ''
2726

2827
this.files = []
@@ -181,18 +180,18 @@ class HtmlWebpackInlineSVGPlugin {
181180

182181

183182
/**
183+
* DEPRECATED: svgoConfig option must now go inside HtmlWebpackInlineSVGPlugin({}) instead of HtmlWebpackPlugin({}).
184+
* This method will be removed in future releases.
185+
*
184186
* get the users custom config
185187
* @param {Object} htmlPluginData
186188
*
187189
*/
188190
getUserConfig (htmlPluginData) {
189191

190-
this.userConfig =
191-
htmlPluginData &&
192-
htmlPluginData.plugin.options &&
193-
_.isObject(htmlPluginData.plugin.options.svgoConfig) ?
194-
htmlPluginData.plugin.options.svgoConfig :
195-
{}
192+
if (_.get(htmlPluginData, 'plugin.options.svgoConfig', false)) {
193+
throw new Error('html-webpack-inline-svg-plugin: on your webpack configuration file svgoConfig option must now go inside HtmlWebpackInlineSVGPlugin({}) instead of HtmlWebpackPlugin({}). Also note the SVGO configuration format has changed and the one you had will need tweaking: https://github.com/theGC/html-webpack-inline-svg-plugin#config')
194+
}
196195

197196
}
198197

@@ -453,20 +452,9 @@ class HtmlWebpackInlineSVGPlugin {
453452
*
454453
*/
455454
optimizeSvg ({ html, inlineImage, data, resolve }) {
456-
const configObj = Object.assign(svgoDefaultConfig, this.userConfig)
457-
458-
const config = {}
459-
460-
461-
// pass all objects to the config.plugins array
462-
463-
config.plugins = _.map(configObj, (value, key) => ({ [key]: value }));
464-
465-
466-
// create a new instance of SVGO
467-
// passing it the merged config, to optimize the svg
468-
469-
const svgo = new SVGO(config)
455+
const svgo = new SVGO({
456+
plugins: this.getSvgoConfig()
457+
})
470458

471459
svgo.optimize(data)
472460
.then((result) => {
@@ -481,6 +469,23 @@ class HtmlWebpackInlineSVGPlugin {
481469
.catch((err) => console.log(chalk.red(err)))
482470
}
483471

472+
/**
473+
* Returns an array with he default SVGO configuration merged with the configuration provided by the user.
474+
* The configuration provided by the user overrides the default one.
475+
* @returns {Array}
476+
*
477+
*/
478+
getSvgoConfig() {
479+
const svgoDefaultConfig = [
480+
{ cleanupIDs: false }
481+
]
482+
483+
const svgoDefaultConfigFiltered = svgoDefaultConfig.filter(di =>
484+
!this.svgoConfig.some(i => Object.keys(di)[0] === Object.keys(i)[0])
485+
)
486+
487+
return svgoDefaultConfigFiltered.concat(this.svgoConfig)
488+
}
484489

485490
/**
486491
* append the inlineImages SVG data to the output HTML and remove the original img by

0 commit comments

Comments
 (0)