Skip to content

Commit 443f8bc

Browse files
Tests added for v2.3
1 parent 130d2ec commit 443f8bc

9 files changed

Lines changed: 185 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* BREAKING CHANGE: On the webpack configuration file the `svgoConfig` option must now go inside `HtmlWebpackInlineSVGPlugin({})` instead of `HtmlWebpackPlugin({})`. An error in console is written at build time otherwise.
44
* 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+
56
## v2.2.0
67

78
* Ability added to load SVGs from an URL (`<img inline src="https://host.com/image.svg">`).

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ The plugin accepts the below options:
219219
</div>
220220
```
221221

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

224224
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:
225225

@@ -268,4 +268,4 @@ I'm happy for someone to take over the project as I don't find myself using it a
268268

269269
## License
270270

271-
This project is licensed under [MIT](https://github.com/theGC/html-webpack-inline-svg-plugin/blob/master/LICENSE).
271+
This project is licensed under [MIT](https://github.com/theGC/html-webpack-inline-svg-plugin/blob/master/LICENSE).

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class HtmlWebpackInlineSVGPlugin {
190190
getUserConfig (htmlPluginData) {
191191

192192
if (_.get(htmlPluginData, 'plugin.options.svgoConfig', false)) {
193-
console.error('html-webpack-inline-svg-plugin: svgoConfig option must now go inside HtmlWebpackInlineSVGPlugin({}) instead of HtmlWebpackPlugin({}): https://github.com/theGC/html-webpack-inline-svg-plugin#config')
193+
throw new Error('html-webpack-inline-svg-plugin: on your webpack configuration file svgoConfig option must now go inside HtmlWebpackInlineSVGPlugin({}) instead of HtmlWebpackPlugin({}): https://github.com/theGC/html-webpack-inline-svg-plugin#config')
194194
}
195195

196196
}
@@ -452,7 +452,9 @@ class HtmlWebpackInlineSVGPlugin {
452452
*
453453
*/
454454
optimizeSvg ({ html, inlineImage, data, resolve }) {
455-
const svgo = new SVGO(this.getSvgoConfig())
455+
const svgo = new SVGO({
456+
plugins: this.getSvgoConfig()
457+
})
456458

457459
svgo.optimize(data)
458460
.then((result) => {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/images/moods.svg

Lines changed: 2 additions & 1 deletion
Loading

spec/fixtures/index-svgo.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta http-equiv="x-ua-compatible" content="ie=edge">
7+
<title>Inline SVG Tests index</title>
8+
</head>
9+
10+
<body>
11+
<div>
12+
<p>
13+
<span>if below SVG is inlined correctly we can reference its content:</span>
14+
<svg class="icon mood-good"><use xlink:href="#icon-mood-good"></use></svg>
15+
</p>
16+
</div>
17+
18+
<h2>Inline images</h2>
19+
20+
<img id="replace-me" src="~img/moods.svg" inline>
21+
22+
<img id="not-an-svg" inline src="images/not-an-svg.png">
23+
24+
<div id="do-not-decode"><?= $foo->bar; ?></div>
25+
26+
should output broken tags</p>
27+
28+
<p>should output unclosed tags
29+
</body>
30+
31+
</html>

spec/index.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ var webpackPostEmitConfig = require('./webpack.post-emit.config')
88
var webpackPreEmitConfig = require('./webpack.pre-emit.config')
99
var webpackInlineAllConfig = require('./webpack.inline-all.config')
1010
var webpackAllowFromUrlConfig = require('./webpack.allow-from-url.config')
11+
var webpackSvgoConfig = require('./webpack.svgo.config')
1112
var jasmineTests = require('./jasmine.tests')
1213
var jasmineInlineAllTests = require('./jasmine-inline-all.tests')
1314
var jasmineAllowFromUrlTests = require('./jasmine-allow-from-url.tests')
15+
var jasmineSvgoTests = require('./jasmine-svgo.tests')
1416
var rm = require('rimraf')
1517

1618
rm(webpackConfig.outputDir, (err) => {
@@ -171,3 +173,36 @@ describe('HtmlWebpackInlineSVGPlugin: inlineAll resolve', function () {
171173
})
172174

173175
})
176+
177+
178+
describe('HtmlWebpackInlineSVGPlugin: custom SVGO config resolve.', function () {
179+
180+
beforeAll(function (done) {
181+
182+
// clone the config
183+
184+
const webpackTestConfig = Object.assign({}, webpackConfig.options, webpackSvgoConfig)
185+
186+
187+
// run webpack
188+
189+
webpack(webpackTestConfig, (err) => {
190+
191+
expect(err).toBeFalsy()
192+
193+
// callback is fired before all files hve been written to disk
194+
// due to use of after-emit - place a timeout to try and avoid the issue
195+
196+
setTimeout(done, 2000)
197+
198+
})
199+
200+
})
201+
202+
jasmineSvgoTests.forEach((test) => {
203+
204+
it(test.label, test.func)
205+
206+
})
207+
208+
})

spec/jasmine-svgo.tests.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var path = require('path')
2+
var fs = require('fs')
3+
var cheerio = require('cheerio')
4+
var webpackConfig = require('./webpack.base.config')
5+
6+
module.exports = [
7+
8+
{
9+
label: 'It should inline SVG images following the custom SVGO config',
10+
func: function (done) {
11+
12+
var htmlFile = path.resolve(webpackConfig.outputDir, 'index-svgo.html')
13+
14+
fs.readFile(htmlFile, 'utf8', function (err, data) {
15+
expect(err).toBeFalsy()
16+
17+
var $ = cheerio.load(data)
18+
19+
// Assertions affecting any html-webpack-inline-svg-plugin use
20+
expect($('img#replace-me').length).toBe(0)
21+
expect($('svg#replace-me').length).toBe(1)
22+
expect($('img#not-an-svg').length).toBe(1)
23+
expect($('div#do-not-decode').length).toBe(1)
24+
25+
// Assertions affecting only svgoConfig option
26+
// The configuration found on webpack.svgo.config.js is the opposite to SVGO defaults
27+
expect($('svg#replace-me').attr('xmlns')).toBe(undefined)
28+
expect($('svg#replace-me title#icon-mood-bad-title').length).toBe(1)
29+
expect($('svg#replace-me symbol#icon-mood-bad').contents()[0].data).toBe('Random comment')
30+
31+
done()
32+
33+
})
34+
35+
},
36+
37+
},
38+
39+
]

spec/webpack.svgo.config.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const path = require('path')
2+
const webpackConfig = require('./webpack.base.config')
3+
const HtmlWebpackPlugin = require('html-webpack-plugin')
4+
const HtmlWebpackInlineSVGPlugin = require('../')
5+
6+
module.exports = {
7+
8+
resolve: {
9+
alias: {
10+
'img': path.join(__dirname, 'fixtures', 'images'),
11+
},
12+
},
13+
14+
module: {
15+
rules: [
16+
{
17+
test: /\.(svg)(\?.*)?$/,
18+
use: [
19+
{
20+
loader: 'file-loader',
21+
options: {
22+
name: 'images/svgs/[name].[ext]',
23+
}
24+
}
25+
],
26+
},
27+
{
28+
test: /\.(png|jpe?g|gif)(\?.*)?$/,
29+
use: [
30+
{
31+
loader: 'file-loader',
32+
options: {
33+
name: 'images/[name].[ext]',
34+
}
35+
}
36+
],
37+
},
38+
{
39+
test: /\.(html)$/,
40+
use: [
41+
{
42+
loader: 'html-loader',
43+
options: {}
44+
}
45+
],
46+
},
47+
]
48+
},
49+
50+
plugins: [
51+
new HtmlWebpackPlugin({
52+
filename: path.resolve(webpackConfig.outputDir, 'index-svgo.html'),
53+
template: path.join(__dirname, 'fixtures', 'index-svgo.html')
54+
}),
55+
new HtmlWebpackInlineSVGPlugin({
56+
svgoConfig: [
57+
{
58+
removeXMLNS: true
59+
},
60+
{
61+
removeTitle: false
62+
},
63+
{
64+
removeComments: false
65+
}
66+
]
67+
}),
68+
],
69+
70+
}

0 commit comments

Comments
 (0)