Skip to content

Commit 3d03510

Browse files
committed
feat: add option keepUselessDefs
Fix #36
1 parent dffa26c commit 3d03510

5 files changed

Lines changed: 58 additions & 2 deletions

File tree

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ Options:
9191
--no-prettier disable Prettier (default: true)
9292
--template <file> specify a custom template to use
9393
--no-expand-props disable props expanding (default: true)
94-
--ids keep ids within the svg
9594
--ref add svgRef prop to svg
9695
--icon use "1em" as width and height
9796
--no-view-box remove viewBox (default: true)
9897
--native add react-native support with react-native-svg
9998
--replace-attr-value [old=new] replace an attribute value
10099
-p, --precision <value> set the number of digits in the fractional part (svgo)
100+
--ids keep ids within the svg (svgo)
101+
--keep-useless-defs keep elements of <defs> without id (svgo)
101102
--no-title remove title tag (svgo) (default: true)
102103
--tab-width specify the number of spaces by indentation-level (prettier)
103104
--use-tabs indent lines with tabs instead of spaces (prettier)
@@ -389,6 +390,15 @@ Set number of digits in the fractional part. See
389390
| ------- | ------------------- | ------------------ |
390391
| `3` | `--precision <int>` | `precision: <int>` |
391392

393+
### Useless Defs
394+
395+
Keep elements of `<defs>` without `id`. It also keep unused symbols. See
396+
[SVGO `removeUselessDefs` plugin](https://github.com/svg/svgo).
397+
398+
| Default | CLI Override | API Override |
399+
| ------- | --------------------- | ------------------------- |
400+
| `false` | `--keep-useless-defs` | `keepUselessDefs: <bool>` |
401+
392402
### Title
393403

394404
Remove the title from SVG. See

src/__snapshots__/index.test.js.snap

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`convert options keepUselessDefs: default 1`] = `
4+
"import React from \\"react\\";
5+
6+
const SvgComponent = props => (
7+
<svg width={0} height={0} style={{ position: \\"absolute\\" }} {...props} />
8+
);
9+
10+
export default SvgComponent;
11+
"
12+
`;
13+
14+
exports[`convert options keepUselessDefs: keepUselessDefs: true 1`] = `
15+
"import React from \\"react\\";
16+
17+
const SvgComponent = props => (
18+
<svg width={0} height={0} style={{ position: \\"absolute\\" }} {...props}>
19+
<symbol viewBox=\\"0 0 24 24\\">
20+
<path d=\\"M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z\\" />
21+
<path d=\\"M0 0h24v24H0z\\" fill=\\"none\\" />
22+
</symbol>
23+
</svg>
24+
);
25+
26+
export default SvgComponent;
27+
"
28+
`;
29+
330
exports[`convert should accept initial state (for webpack) 1`] = `
431
"import React from \\"react\\";
532

src/cli/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ program
2323
.option('--no-prettier', 'disable Prettier')
2424
.option('--template <file>', 'specify a custom template to use')
2525
.option('--no-expand-props', 'disable props expanding')
26-
.option('--ids', 'keep ids within the svg')
2726
.option('--ref', 'add svgRef prop to svg')
2827
.option('--icon', 'use "1em" as width and height')
2928
.option('--no-view-box', 'remove viewBox')
@@ -37,6 +36,8 @@ program
3736
'-p, --precision <value>',
3837
'set the number of digits in the fractional part (svgo)',
3938
)
39+
.option('--ids', 'keep ids within the svg (svgo)')
40+
.option('--keep-useless-defs', 'keep elements of <defs> without id (svgo)')
4041
.option('--no-title', 'remove title tag (svgo)')
4142
.option(
4243
'--tab-width',

src/configToOptions.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const defaultConfig = {
2020
replaceAttrValues: [],
2121
expandProps: true,
2222
title: true,
23+
keepUselessDefs: false,
2324
ids: false,
2425
precision: 3, // default to svgo
2526
semi: undefined, // default to prettier
@@ -56,6 +57,7 @@ function configToOptions(config = {}) {
5657
if (!config.title || config.icon) plugins.push({ removeTitle: {} })
5758
else if (config.title) plugins.push({ removeTitle: false })
5859
if (config.viewBox) plugins.push({ removeViewBox: false })
60+
if (config.keepUselessDefs) plugins.push({ removeUselessDefs: false })
5961
if (config.ids) plugins.push({ cleanupIDs: { remove: false } })
6062
if (config.precision === 'number')
6163
svgoConfig.floatPrecision = Number(svgoConfig.precision)

src/index.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,20 @@ describe('convert', () => {
274274

275275
expect(result).toMatchSnapshot()
276276
})
277+
278+
describe('options', () => {
279+
it('keepUselessDefs', async () => {
280+
const svgWithUselessDefs = `<?xml version="1.0" encoding="UTF-8"?>
281+
<svg width="0" height="0" style="position:absolute">
282+
<symbol xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" id="filter">
283+
<path d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z" />
284+
<path d="M0 0h24v24H0z" fill="none" />
285+
</symbol>
286+
</svg>`
287+
expect(await convert(svgWithUselessDefs)).toMatchSnapshot('default')
288+
expect(
289+
await convert(svgWithUselessDefs, { keepUselessDefs: true }),
290+
).toMatchSnapshot('keepUselessDefs: true')
291+
})
292+
})
277293
})

0 commit comments

Comments
 (0)