Skip to content

Commit f0a9d9c

Browse files
authored
Add support for SVGO v3 (#8)
1 parent 5559b17 commit f0a9d9c

File tree

10 files changed

+857
-15
lines changed

10 files changed

+857
-15
lines changed

.github/workflows/test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Build
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
strategy:
10+
matrix:
11+
node-version: [14.x, 16.x]
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Use Node.js ${{ matrix.node-version }}
16+
uses: actions/setup-node@v3
17+
with:
18+
node-version: ${{ matrix.node-version }}
19+
- run: yarn
20+
- run: yarn test

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ tsconfig.json
3636
tslint.json
3737
yarn.lock
3838

39+
test

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ In order to use this plugin correctly, you SVGs should have their `width` and `h
3030

3131
## Usage
3232

33-
**This plugin should be used with SVGO v2 and above.**
33+
**v2.x.x of this plugin should be used with SVGO v3 and above.**
34+
35+
**v1.x.x of this plugin should be used with SVGO v2.**
3436

3537
1. Create a `svgo.config.js` file following the [official configuration guide](https://github.com/svg/svgo#configuration)
3638
2. Use the option to specify a custom plugin.

index.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,20 @@ exports.description = 'Adds viewBox attribute based on width and height';
2020
* @author Atanas Atanasov
2121
*/
2222
exports.fn = function (item) {
23+
if (item.type !== 'root') {
24+
return item;
25+
}
26+
2327
const {
2428
name,
25-
type,
26-
attributes: { width, height, viewBox }
27-
} = item;
29+
attributes,
30+
attributes: { width, height }
31+
} = item.children[0];
2832

2933
if (
30-
name === 'svg' &&
31-
type === 'element' &&
32-
width != null &&
33-
height != null &&
34-
viewBox == null &&
35-
Number.isNaN(Number(width)) === false &&
36-
Number.isNaN(Number(height)) === false
34+
(name === 'svg' && !!width && !!height && !attributes.viewBox) ||
35+
(Number.isNaN(Number(width)) === false && Number.isNaN(Number(height)) === false)
3736
) {
38-
item.attributes.viewBox = `0 0 ${Number(width)} ${Number(height)}`;
37+
item.children[0].attributes.viewBox = `0 0 ${Number(width)} ${Number(height)}`;
3938
}
4039
};

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svgo-add-viewbox",
3-
"version": "1.1.0",
3+
"version": "2.0.0",
44
"description": "SVGO plugin which adds 'viewBox' attribute based on 'width' and 'height' attributes",
55
"keywords": [
66
"viewBox",
@@ -21,7 +21,14 @@
2121
"type": "git",
2222
"url": "github:scriptex/svgo-add-viewbox"
2323
},
24-
"scripts": {},
24+
"scripts": {
25+
"svg": "svgo -f ./test/input -o ./test/output --config ./test/svgo.config.js",
26+
"test": "yarn svg && tape test/index.js"
27+
},
2528
"dependencies": {},
26-
"devDependencies": {}
29+
"devDependencies": {
30+
"svg-parser": "2.0.4",
31+
"svgo": "3.0.0",
32+
"tape": "5.6.1"
33+
}
2734
}

test/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { resolve } = require('path');
2+
const { existsSync, readFileSync } = require('fs');
3+
4+
const tape = require('tape');
5+
const { parse } = require('svg-parser');
6+
7+
tape('Has viewBox attribute', async t => {
8+
const file = resolve(__dirname, 'output', 'test.svg');
9+
const svg = readFileSync(file, 'utf-8');
10+
const parsed = parse(svg);
11+
12+
const fileExists = existsSync(resolve(__dirname, file));
13+
14+
// @ts-ignore
15+
const hasViewBox = !!parsed.children[0].properties.viewBox;
16+
17+
t.ok(fileExists, 'File exists');
18+
t.ok(hasViewBox, 'Has viewBox attribute');
19+
t.end();
20+
});

test/input/test.svg

Lines changed: 45 additions & 0 deletions
Loading

test/output/test.svg

Lines changed: 1 addition & 0 deletions
Loading

test/svgo.config.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const addViewBox = require('..');
2+
3+
module.exports = {
4+
plugins: [
5+
{
6+
name: 'preset-default',
7+
params: {
8+
overrides: {
9+
removeViewBox: false
10+
}
11+
}
12+
},
13+
{
14+
fn: addViewBox.fn,
15+
name: 'addViewBox',
16+
type: addViewBox.type,
17+
active: addViewBox.active,
18+
description: addViewBox.description
19+
}
20+
]
21+
};

0 commit comments

Comments
 (0)