Skip to content

Commit 8e27c82

Browse files
authored
Correctly load and use SVGO config (#4)
1 parent 6707546 commit 8e27c82

File tree

5 files changed

+87
-70
lines changed

5 files changed

+87
-70
lines changed

config/svgo.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ module.exports = {
1414
'convertStyleToAttrs',
1515
'removeUselessStrokeAndFill',
1616
'removeDimensions',
17+
{
18+
name: 'removeViewBox',
19+
enabled: false
20+
},
1721
{
1822
name: 'cleanupIDs',
1923
active: true,

example/sprite.svg

Lines changed: 1 addition & 1 deletion
Loading

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svg-symbol-sprite",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A script to generate a symbol sprite from SVG files",
55
"keywords": [
66
"SVG",
@@ -57,7 +57,7 @@
5757
},
5858
"devDependencies": {
5959
"@types/cheerio": "0.22.30",
60-
"@types/node": "16.11.18",
60+
"@types/node": "17.0.7",
6161
"@types/svgo": "2.6.0",
6262
"tslib": "2.3.1",
6363
"typescript": "4.5.4"

src/index.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#!/usr/bin/env node
22

3-
import { exec } from 'child_process';
43
import { existsSync, promises as fs } from 'fs';
54
import { extname, resolve, basename } from 'path';
65

76
import { load } from 'cheerio';
87
import { Command } from 'commander';
8+
import { optimize, loadConfig } from 'svgo';
99

1010
const { rm, readdir, readFile, writeFile } = fs;
1111

1212
const cli = new Command();
1313

1414
const SVG_PROPS = 'xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true"';
15-
const SVG_STYLE = 'style="width: 0; height: 0; visibility: hidden; position: absolute;"';
15+
const SVG_STYLE = 'style="width: 0; height: 0; position: absolute;"';
16+
const DEFAULT_CONFIG = './config/svgo.config.js';
17+
18+
const CHEERIO_OPTIONS = {
19+
lowerCaseTags: true,
20+
lowerCaseAttributeNames: true,
21+
_useHtmlParser2: true
22+
};
1623

1724
cli.option('-i, --input [input]', 'Specifies input dir (current dir by default)', '.')
1825
.option('-o, --output [output]', 'Specifies output file ("./sprite.svg" by default)', 'sprite.svg')
@@ -24,7 +31,7 @@ cli.option('-i, --input [input]', 'Specifies input dir (current dir by default)'
2431
const { input: INPUT, output: OUTPUT, viewbox: VIEWBOX, prefix: PREFIX, quiet: QUIET, config: CONFIG } = cli.opts();
2532

2633
const onEnd = (): void => console.log(`File ‘${OUTPUT}’ successfully generated.`);
27-
const getSvg = (content: string) => load(content)('svg').first();
34+
const getSvg = (content: string) => load(content, CHEERIO_OPTIONS)('svg').first();
2835
const filterFile = (file: string) => extname(file) === '.svg';
2936
const processFiles = (files: string[]) => Promise.all(files.filter(filterFile).map(processFile));
3037
const removeOutput = async () => (existsSync(OUTPUT) ? await rm(OUTPUT) : undefined);
@@ -66,8 +73,18 @@ const onError = (err: Error) => {
6673

6774
removeOutput()
6875
.then(readSrcFolder)
69-
.then((files: string[]) => {
70-
exec(`svgo-viewbox -i ${INPUT} -f ${CONFIG}`);
76+
.then(async (files: string[]) => {
77+
let svgoConfig = await loadConfig(DEFAULT_CONFIG);
78+
79+
try {
80+
svgoConfig = await loadConfig(CONFIG);
81+
} catch (e: any) {
82+
console.log('SVG Symbol Sprite: SVGO configuration file not found. Using default SVGO configuration.');
83+
}
84+
85+
for (const file of files) {
86+
optimize(file, svgoConfig);
87+
}
7188

7289
return processFiles(files);
7390
})

0 commit comments

Comments
 (0)