Skip to content

Commit 5c8163a

Browse files
committed
Switch to "type: module" and support latest SVGO (#9)
1 parent 62725ea commit 5c8163a

File tree

11 files changed

+273
-214
lines changed

11 files changed

+273
-214
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build
1+
name: Test
22

33
on: [push, pull_request]
44

@@ -8,7 +8,7 @@ jobs:
88

99
strategy:
1010
matrix:
11-
node-version: [14.x, 16.x]
11+
node-version: [lts/*]
1212

1313
steps:
1414
- uses: actions/checkout@v3

.npmignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,3 @@ renovate.json
3535
tsconfig.json
3636
tslint.json
3737
yarn.lock
38-
39-
test

README.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Codebeat Badge](https://codebeat.co/badges/d765a4c8-2c0e-44f2-89c3-fa364fdc14e6)](https://codebeat.co/projects/github-com-scriptex-svgo-add-viewbox-master)
44
[![CodeFactor Badge](https://www.codefactor.io/repository/github/scriptex/svgo-add-viewbox/badge)](https://www.codefactor.io/repository/github/scriptex/svgo-add-viewbox)
55
[![DeepScan grade](https://deepscan.io/api/teams/3574/projects/5257/branches/40799/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=3574&pid=5257&bid=40799)
6-
[![Analytics](https://ga-beacon-361907.ew.r.appspot.com/UA-83446952-1/github.com/scriptex/svgo-add-viewbox/README.md?pixel)](https://github.com/scriptex/svgo-add-viewbox/)
6+
[![Analytics](https://ga-beacon.atanas.info/api/analytics?account=UA-83446952-1&page=github.com/scriptex/svgo-add-viewbox&pixel)](https://github.com/scriptex/svgo-add-viewbox/)
77

88
# SVGO Add viewBox
99

@@ -26,40 +26,40 @@
2626

2727
This plugin adds the `viewBox` attribute to your SVGs based on the `width` and `height` attributes. The difference between this plugin and the built-in [`removeDimensions`](https://github.com/svg/svgo/blob/master/plugins/removeDimensions.js) plugin is that `svgo-add-viewbox` does not remove the `width` and `height` of your SVGs.
2828

29-
In order to use this plugin correctly, you SVGs should have their `width` and `height` attributes specified.
29+
In order to use this plugin correctly, your SVGs should have their `width` and `height` attributes specified.
3030

3131
## Usage
3232

33-
**v2.x.x of this plugin should be used with SVGO v3 and above.**
33+
**v2.x.x or 3.x.x of this plugin should be used with SVGO v3 and above.**
3434

3535
**v1.x.x of this plugin should be used with SVGO v2.**
3636

3737
1. Create a `svgo.config.js` file following the [official configuration guide](https://github.com/svg/svgo#configuration)
3838
2. Use the option to specify a custom plugin.
3939
3. Install this module from NPM
40-
```sh
41-
npm install svgo-add-viewbox --save-dev
42-
# or
43-
yarn add svgo-add-viewbox -D
44-
```
45-
4. `require` the module which you just created in your `svgo.config.js` file:
46-
```javascript
47-
const addViewBox = require('svgo-add-viewbox');
48-
```
40+
41+
```sh
42+
npm install svgo-add-viewbox --save-dev
43+
# or
44+
yarn add svgo-add-viewbox -D
45+
```
46+
47+
4. `import` the module in your `svgo.config.js` file:
48+
49+
```javascript
50+
import addViewBox from 'svgo-add-viewbox';
51+
```
52+
4953
5. In the `plugins` array in your `svgo.config.js` file add the following:
50-
```javascript
51-
plugins: [
52-
// ... more plugins
53-
{
54-
fn: addViewBox.fn,
55-
name: 'addViewBox',
56-
type: addViewBox.type,
57-
active: addViewBox.active,
58-
description: addViewBox.description
59-
}
60-
// ... more plugins
61-
];
62-
```
54+
55+
```javascript
56+
plugins: [
57+
// ... more plugins
58+
addViewBox
59+
// ... more plugins
60+
];
61+
```
62+
6363
6. Execute your SVG transformation NPM script.
6464

6565
## LICENSE

index.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

index.mjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @typedef {import("svgo").CustomPlugin} SVGOPlugin
3+
* @typedef {SVGOPlugin['fn']} SVGOPluginFunction
4+
* @typedef {SVGOPlugin['name']} SVGOPluginName
5+
* @typedef {import("svgo/lib/types").XastElement} SVGOElement
6+
*/
7+
8+
/**
9+
* @type {SVGOPluginName}
10+
*/
11+
const name = 'SVGOAddViewBox';
12+
13+
/**
14+
* @type {SVGOPluginFunction}
15+
*/
16+
const fn = () => ({
17+
root: {
18+
enter: node => {
19+
const element = /** @type {SVGOElement} */ (node?.children?.[0]);
20+
21+
if (!element) {
22+
return;
23+
}
24+
25+
const { width, height } = element.attributes;
26+
27+
if (typeof width === 'undefined' || typeof height === 'undefined') {
28+
return;
29+
}
30+
31+
element.attributes.viewBox = `0 0 ${Number(width)} ${Number(height)}`;
32+
}
33+
}
34+
});
35+
36+
/**
37+
* @type {SVGOPlugin}
38+
*/
39+
export default {
40+
fn,
41+
name
42+
};

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svgo-add-viewbox",
3-
"version": "2.0.1",
3+
"version": "3.0.0",
44
"description": "SVGO plugin which adds 'viewBox' attribute based on 'width' and 'height' attributes",
55
"keywords": [
66
"viewBox",
@@ -16,14 +16,15 @@
1616
"license": "MIT",
1717
"author": "Atanas Atanasov <hi@atanas.info> (https://atanas.info)",
1818
"funding": "https://github.com/sponsors/scriptex",
19-
"main": "index.js",
19+
"main": "index.mjs",
20+
"type": "module",
2021
"repository": {
2122
"type": "git",
2223
"url": "github:scriptex/svgo-add-viewbox"
2324
},
2425
"scripts": {
25-
"svg": "svgo -f ./test/input -o ./test/output --config ./test/svgo.config.js",
26-
"test": "yarn svg && tape test/index.js"
26+
"svg": "svgo -f ./test/input -o ./test/output --config ./test/svgo.config.mjs",
27+
"test": "yarn svg && tape test/index.mjs"
2728
},
2829
"dependencies": {},
2930
"devDependencies": {

test/index.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/index.mjs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import tape from 'tape';
2+
import { parse } from 'svg-parser';
3+
import { optimize, loadConfig } from 'svgo';
4+
5+
import { resolve } from 'node:path';
6+
import { existsSync, readFileSync } from 'node:fs';
7+
8+
tape('Has viewBox attribute', async t => {
9+
const input = resolve('test', 'input', 'test.svg');
10+
const output = resolve('test', 'output', 'test.svg');
11+
const inputSVG = readFileSync(input, 'utf-8');
12+
const outputSVG = readFileSync(output, 'utf-8');
13+
14+
const config = await loadConfig(resolve('test', 'svgo.config.mjs'));
15+
const optimized = optimize(inputSVG, config).data;
16+
const parsed = parse(optimized);
17+
const rootNode = /** @type {svgParser.ElementNode} */ (parsed.children[0]);
18+
19+
t.ok(existsSync(input), 'Input file exists.');
20+
t.ok(existsSync(output), 'Output file exists.');
21+
t.equals(optimized, outputSVG, 'The parsed input file and saved output files match.');
22+
t.ok(!!rootNode?.properties?.viewBox, 'Has viewBox attribute.');
23+
24+
t.end();
25+
});

test/svgo.config.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

test/svgo.config.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @typedef {Array<import('svgo').PluginConfig>} SVGOPlugins
3+
*/
4+
5+
import addViewBox from '../index.mjs';
6+
7+
export default {
8+
/**
9+
* @type {SVGOPlugins}
10+
*/
11+
plugins: [
12+
{
13+
name: 'preset-default',
14+
params: {
15+
overrides: {
16+
removeViewBox: false
17+
}
18+
}
19+
},
20+
addViewBox
21+
]
22+
};

0 commit comments

Comments
 (0)