Skip to content

Commit 3024839

Browse files
lipiswardpeet
andcommitted
chore: minify svg husky hook (#10560)
and added hooks for *.svg files Co-authored-by: Ward Peeters <[email protected]>
1 parent 22c41fb commit 3024839

File tree

4 files changed

+44
-30
lines changed

4 files changed

+44
-30
lines changed
Lines changed: 3 additions & 12 deletions
Loading

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
"*.{md,css,scss,yaml,yml}": [
5252
"prettier --write",
5353
"git add"
54+
],
55+
"*.svg": [
56+
"svgo --pretty --indent=2 --config=svgo.yml --multipass",
57+
"git add"
5458
]
5559
},
5660
"husky": {
@@ -61,10 +65,10 @@
6165
"scripts": {
6266
"bootstrap": "npm-run-all -s check-versions lerna-prepare",
6367
"check-versions": "babel-node scripts/check-versions.js",
64-
"format": "npm run format:code && npm run format:other",
68+
"format": "npm run format:code && npm run format:other && npm run format:svg",
6569
"format:other": "npm run prettier -- --write",
6670
"format:code": "npm run lint:code -- --fix",
67-
"format-svg": "find www examples packages | grep '\\.svg$' | xargs -Iz -n 1 svgo --pretty --indent=2 --config=svgo.yml z",
71+
"format:svg": "node scripts/format-svg.js",
6872
"hooks:uninstall": "node node_modules/husky/husky.js uninstall",
6973
"hooks:install": "node node_modules/husky/husky.js install",
7074
"jest": "jest",

scripts/format-svg.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// this file exists because svgo only accepts folders or files, to make this work you'll need to do
2+
// find www examples packages | grep '\\.svg$' | xargs -Iz -n 1 svgo --pretty --indent=2 --config=svgo.yml z
3+
// this only works on bash not inside windows so we create a simple js file that does the globbing for us
4+
const glob = require(`glob`)
5+
const execa = require(`execa`)
6+
const path = require(`path`)
7+
8+
// list of ignored files because they don't get optimized correctly
9+
const ignoreList = [
10+
`./docs/blog/2017-11-08-migrate-from-jekyll-to-gatsby/diagram-ci.svg`,
11+
]
12+
13+
const files = glob.sync(`./**/*.svg`, {
14+
ignore: [`./node_modules/**`].concat(ignoreList),
15+
})
16+
17+
const proc = execa(
18+
`./node_modules/.bin/svgo`,
19+
[`--pretty`, `--indent=2`, `--config=svgo.yml`, `--multipass`].concat(files),
20+
{ cwd: path.join(__dirname, `..`) }
21+
)
22+
23+
proc.stdout.pipe(process.stdout)
24+
proc.stderr.pipe(process.stderr)
Lines changed: 11 additions & 16 deletions
Loading

0 commit comments

Comments
 (0)