Skip to content

Commit

Permalink
chore: minify svg husky hook (#10560)
Browse files Browse the repository at this point in the history
and added hooks for *.svg files

Co-authored-by: Ward Peeters <ward@coding-tech.com>
  • Loading branch information
lipis and wardpeet committed Feb 13, 2019
1 parent 22c41fb commit 3024839
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
15 changes: 3 additions & 12 deletions docs/blog/2017-11-08-migrate-from-jekyll-to-gatsby/gatsby.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions package.json
Expand Up @@ -51,6 +51,10 @@
"*.{md,css,scss,yaml,yml}": [
"prettier --write",
"git add"
],
"*.svg": [
"svgo --pretty --indent=2 --config=svgo.yml --multipass",
"git add"
]
},
"husky": {
Expand All @@ -61,10 +65,10 @@
"scripts": {
"bootstrap": "npm-run-all -s check-versions lerna-prepare",
"check-versions": "babel-node scripts/check-versions.js",
"format": "npm run format:code && npm run format:other",
"format": "npm run format:code && npm run format:other && npm run format:svg",
"format:other": "npm run prettier -- --write",
"format:code": "npm run lint:code -- --fix",
"format-svg": "find www examples packages | grep '\\.svg$' | xargs -Iz -n 1 svgo --pretty --indent=2 --config=svgo.yml z",
"format:svg": "node scripts/format-svg.js",
"hooks:uninstall": "node node_modules/husky/husky.js uninstall",
"hooks:install": "node node_modules/husky/husky.js install",
"jest": "jest",
Expand Down
24 changes: 24 additions & 0 deletions scripts/format-svg.js
@@ -0,0 +1,24 @@
// this file exists because svgo only accepts folders or files, to make this work you'll need to do
// find www examples packages | grep '\\.svg$' | xargs -Iz -n 1 svgo --pretty --indent=2 --config=svgo.yml z
// this only works on bash not inside windows so we create a simple js file that does the globbing for us
const glob = require(`glob`)
const execa = require(`execa`)
const path = require(`path`)

// list of ignored files because they don't get optimized correctly
const ignoreList = [
`./docs/blog/2017-11-08-migrate-from-jekyll-to-gatsby/diagram-ci.svg`,
]

const files = glob.sync(`./**/*.svg`, {
ignore: [`./node_modules/**`].concat(ignoreList),
})

const proc = execa(
`./node_modules/.bin/svgo`,
[`--pretty`, `--indent=2`, `--config=svgo.yml`, `--multipass`].concat(files),
{ cwd: path.join(__dirname, `..`) }
)

proc.stdout.pipe(process.stdout)
proc.stderr.pipe(process.stderr)
27 changes: 11 additions & 16 deletions www/src/assets/showcase-feather.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3024839

Please sign in to comment.