Skip to content

Commit c5c2a7e

Browse files
authoredMar 4, 2021
Merge pull request #36 from rpominov/svgo2
Migrate to svgo 2
2 parents b5b9144 + 0e1dfb5 commit c5c2a7e

File tree

9 files changed

+229
-516
lines changed

9 files changed

+229
-516
lines changed
 

‎README.md

+68-26
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,60 @@
33
## Install
44

55
```
6-
$ npm install svgo svgo-loader --save-dev
6+
$ npm install svgo-loader --save-dev
77
```
88

99
... or with Yarn
1010

1111
```
12-
$ yarn add svgo svgo-loader -D
12+
$ yarn add svgo-loader -D
1313
```
1414

15-
DON'T FORGET TO INSTALL / UPDATE THE `svgo` PACKAGE after you update `svg-loader` (see [#20](https://github.com/rpominov/svgo-loader/issues/20))
16-
1715
## Usage
1816

19-
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
17+
[Documentation: Using loaders](https://webpack.js.org/concepts/loaders/#using-loaders)
18+
19+
```js
20+
module.exports = {
21+
...,
22+
module: {
23+
rules: [
24+
{
25+
test: /\.svg$/,
26+
use: [
27+
{
28+
loader: 'file-loader'
29+
},
30+
{
31+
loader: 'svgo-loader',
32+
}
33+
]
34+
}
35+
]
36+
}
37+
}
38+
```
2039

21-
Svgo-loader just passes config to the [svgo](https://github.com/svg/svgo) library.
40+
By default svgo-loader uses config from `svgo.config.js` similar to svgo cli.
41+
See [how to configure svgo](https://github.com/svg/svgo#configuration).
2242

23-
### Put the SVGO config into loader's `options`
43+
Specify configFile option to load custom config module:
2444

25-
``` javascript
45+
```js
2646
module.exports = {
2747
...,
2848
module: {
2949
rules: [
3050
{
3151
test: /\.svg$/,
3252
use: [
33-
{loader: 'file-loader'},
53+
{
54+
loader: 'file-loader'
55+
},
3456
{
3557
loader: 'svgo-loader',
3658
options: {
37-
plugins: [
38-
{removeTitle: true},
39-
{convertColors: {shorthex: false}},
40-
{convertPathData: false}
41-
]
59+
configFile: './scripts/svgo.config.js'
4260
}
4361
}
4462
]
@@ -48,21 +66,23 @@ module.exports = {
4866
}
4967
```
5068

51-
### Or use an external config like you would with SVGO CLI
69+
or to disable loading config:
5270

53-
``` javascript
71+
```js
5472
module.exports = {
5573
...,
5674
module: {
5775
rules: [
5876
{
5977
test: /\.svg$/,
6078
use: [
61-
{loader: 'file-loader'},
79+
{
80+
loader: 'file-loader'
81+
},
6282
{
6383
loader: 'svgo-loader',
6484
options: {
65-
externalConfig: "svgo-config.yml"
85+
configFile: false
6686
}
6787
}
6888
]
@@ -72,14 +92,36 @@ module.exports = {
7292
}
7393
```
7494

75-
In `svgo-config.yml`:
95+
You can also specify options which override loaded from config
7696

77-
```yml
78-
plugins:
79-
- removeTitle: true
80-
- convertPathData: false
81-
- convertColors:
82-
shorthex: false
97+
```js
98+
module.exports = {
99+
...,
100+
module: {
101+
rules: [
102+
{
103+
test: /\.svg$/,
104+
use: [
105+
{
106+
loader: 'file-loader'
107+
},
108+
{
109+
loader: 'svgo-loader',
110+
options: {
111+
multipass: true,
112+
js2svg: {
113+
indent: 2,
114+
pretty: true,
115+
}
116+
}
117+
}
118+
]
119+
}
120+
]
121+
}
122+
}
83123
```
84124

85-
You can use `YML` or `JSON` files as external configs.
125+
## License and Copyright
126+
127+
This software is released under the terms of the [MIT license](https://github.com/rpominov/svgo-loader/blob/master/LICENSE).

‎example/basic/webpack.config.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,11 @@ module.exports = {
1111
{
1212
test: /\.svg$/,
1313
use: [
14-
{loader: 'file-loader'},
14+
{
15+
loader: 'file-loader'
16+
},
1517
{
1618
loader: '../../index.js',
17-
options: {
18-
plugins: [
19-
{removeTitle: true},
20-
{convertColors: {shorthex: false}},
21-
{convertPathData: false}
22-
]
23-
}
2419
}
2520
]
2621
}

‎example/external-config/svgo-config.yml

-5
This file was deleted.
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { extendDefaultPlugins } = require('svgo');
2+
module.exports = {
3+
plugins: extendDefaultPlugins([
4+
{
5+
name: 'removeTitle',
6+
active: false
7+
},
8+
{
9+
name: 'convertPathData',
10+
active: false
11+
},
12+
{
13+
name: 'convertColors',
14+
params: {
15+
shorthex: false
16+
}
17+
}
18+
])
19+
};

‎example/external-config/webpack.config.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ module.exports = {
1111
{
1212
test: /\.svg$/,
1313
use: [
14-
{loader: "file-loader"},
14+
{
15+
loader: "file-loader"
16+
},
1517
{
1618
loader: "../../index.js",
17-
options: {
18-
externalConfig: "svgo-config.yml"
19-
}
2019
}
2120
]
2221
}

‎example/svgo-error/webpack.config.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,11 @@ module.exports = {
1111
{
1212
test: /\.svg$/,
1313
use: [
14-
{loader: 'file-loader'},
14+
{
15+
loader: 'file-loader'
16+
},
1517
{
1618
loader: '../../index.js',
17-
options: {
18-
plugins: [
19-
{removeTitle: true},
20-
{convertColors: {shorthex: false}},
21-
{convertPathData: false}
22-
]
23-
}
2419
}
2520
]
2621
}

‎index.js

+20-46
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,24 @@
1-
var Svgo = require('svgo');
2-
var loaderUtils = require('loader-utils');
3-
var yaml = require('js-yaml');
4-
var fs = require('fs');
5-
var path = require('path');
1+
const { optimize, loadConfig } = require('svgo');
2+
const loaderUtils = require('loader-utils');
63

7-
module.exports = function(source) {
8-
this.cacheable(true);
9-
var callback = this.async();
10-
11-
var config = loaderUtils.getOptions(this) || {};
12-
13-
// Ported from:
14-
// https://github.com/svg/svgo/blob/174c37208017e5909d5f7db2e8faba49663a945a/lib/svgo/coa.js#L175-L192
15-
if (typeof config.externalConfig === 'string') {
16-
var configPath = config.externalConfig;
17-
var configText;
18-
try {
19-
configText = fs.readFileSync(path.resolve(configPath), 'utf8');
20-
config = JSON.parse(configText);
21-
} catch (err) {
22-
if (err.code === 'ENOENT') {
23-
callback(new Error("Couldn't find file with external svgo config '" + configPath + "'."));
24-
return;
25-
} else if (err.code === 'EISDIR') {
26-
callback(new Error("Directory '" + configPath + "' is not a config file."));
27-
return;
28-
}
29-
config = yaml.safeLoad(configText);
30-
if (!config || Array.isArray(config)) {
31-
callback(new Error("Invalid external svgo config file '" + configPath + "'."));
32-
return;
33-
}
34-
}
4+
async function loader(source) {
5+
const { configFile, ...options } = loaderUtils.getOptions(this) || {};
6+
let config;
7+
if (typeof configFile === 'string') {
8+
config = await loadConfig(configFile, this.context);
9+
} else if (configFile !== false) {
10+
config = await loadConfig(null, this.context);
11+
}
12+
const result = optimize(source, { path: this.resourcePath, ...config, ...options });
13+
if (result.error) {
14+
throw Error(result.error);
3515
}
16+
return result.data;
17+
}
3618

37-
var svgo = new Svgo({ ...config });
38-
svgo.optimize(source, { path: this.resourcePath })
39-
.then(function(result) {
40-
callback(null, result.data);
41-
return;
42-
}, function(error) {
43-
if (error instanceof Error) {
44-
callback(error);
45-
return;
46-
}
47-
callback(new Error(error));
48-
return;
49-
});
19+
module.exports = function (source) {
20+
const callback = this.async();
21+
loader.call(this, source)
22+
.then(result => callback(null, result))
23+
.catch(error => callback(error));
5024
};

‎package-lock.json

+111-413
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+2-6
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@
2222
"homepage": "https://github.com/pozadi/svgo-loader",
2323
"devDependencies": {
2424
"file-loader": "^2.0.0",
25-
"svgo": "^1.0.5",
2625
"webpack": "^4.17.2",
2726
"webpack-cli": "^3.1.0"
2827
},
29-
"peerDependencies": {
30-
"svgo": "^1.0.0"
31-
},
3228
"dependencies": {
33-
"js-yaml": "^3.13.1",
34-
"loader-utils": "^1.0.3"
29+
"loader-utils": "^1.0.3",
30+
"svgo": "^2.2.0"
3531
}
3632
}

0 commit comments

Comments
 (0)
Please sign in to comment.