Skip to content

Commit d98a72c

Browse files
authored
Optimized SVG using SVGO (#597)
* Added script to optimize svgs * Updated the svgs using svgo * Made the optimize svg script into a workflow * Added npm install step * Change the env variable bug * Clean up and updated check svg * Change label name
1 parent 2c6a21d commit d98a72c

File tree

534 files changed

+1432
-10831
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

534 files changed

+1432
-10831
lines changed

.github/scripts/check_svgs_on_pr.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,11 @@ def check_svgs(svg_file_paths: List[Path]):
6969
if root.get("viewBox") != "0 0 128 128":
7070
err_msg.append("-'viewBox' is not '0 0 128 128' -> Set it or scale the file using https://www.iloveimg.com/resize-image/resize-svg")
7171

72-
acceptable_size = [None, "128px", "128"]
73-
if root.get("height") not in acceptable_size:
74-
err_msg.append("-'height' is present in svg element but is not '128' or '128px' -> Remove it or set it to '128' or '128px'")
75-
76-
if root.get("width") not in acceptable_size:
77-
err_msg.append("-'width' is present in svg element but is not '128' or '128px' -> Remove it or set it to '128' or '128px'")
78-
79-
if root.get("style") is not None and "enable-background" in root.get("style"):
80-
err_msg.append("-deprecated 'enable-background' in style attribute -> Remove it")
81-
8272
if root.get("x") is not None:
83-
err_msg.append("-unneccessary 'x' attribute in svg element -> Remove it")
73+
err_msg.append("-unneccessary 'x' attribute in svg root element -> Remove it")
8474

8575
if root.get("y") is not None:
86-
err_msg.append("-unneccessary 'y' attribute in svg element -> Remove it")
76+
err_msg.append("-unneccessary 'y' attribute in svg root element -> Remove it")
8777

8878
style = root.findtext(f".//{namespace}style")
8979
if style != None and "fill" in style:

.github/workflows/optimize_svg.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Optimize the added/changed svgs
2+
on:
3+
pull_request:
4+
types: [labeled]
5+
jobs:
6+
peek:
7+
name: Optimize the added/changed svgs
8+
if: github.event.label.name == 'bot:optimize'
9+
runs-on: ubuntu-18.04
10+
steps:
11+
- uses: actions/checkout@v2
12+
with:
13+
ref: ${{ github.head_ref }}
14+
15+
- name: Set up gulp
16+
run: npm install
17+
18+
- name: Get Changed Files and generate files_added.json & files_modified.json
19+
uses: lots0logs/gh-action-get-changed-files@2.1.4
20+
id: get_added_and_modified_files
21+
with:
22+
token: ${{ secrets.GITHUB_TOKEN }}
23+
24+
- name: Run the update_id.py
25+
env:
26+
ADDED_FILES: ${{ steps.get_added_and_modified_files.outputs.added }}
27+
MODIFIED_FILES: ${{ steps.get_added_and_modified_files.outputs.modified }}
28+
run: npm run optimize-svg -- --filesAddedJson=$ADDED_FILES --filesModifiedJson=$MODIFIED_FILES
29+
30+
- name: Commit the changes
31+
uses: stefanzweifel/git-auto-commit-action@v4
32+
with:
33+
commit_message: Optimized the SVGs

gulpfile.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var gulp = require('gulp');
2+
const svgmin = require("gulp-svgmin")
23
const sass = require('gulp-sass');
34
sass.compiler = require('sass')
5+
const yargs = require("yargs")
46
const fsPromise = require('fs').promises;
57
const path = require("path");
68

@@ -149,5 +151,70 @@ function cleanUp() {
149151
}
150152

151153

154+
//////// Update SVG Task ////////
155+
/**
156+
* Update the svg by optimizing it
157+
* and prefixing its ids so it's unique across the repo.
158+
*
159+
* This requires a json list of svg file names to update.
160+
* This must be passed through the commandline arguments.
161+
*/
162+
function optimizeSvg() {
163+
let svgPaths = getAddedModifiedSvg(yargs.argv.filesAddedJson,
164+
yargs.argv.filesModifiedJson)
165+
166+
return gulp.src(svgPaths)
167+
.pipe(svgmin(configOptionCallback))
168+
.pipe(gulp.dest(file => {
169+
return file.base
170+
}))
171+
}
172+
173+
/**
174+
* Get the svgs added and modified from the '/icons' folder only.
175+
* @param {*} filesAddedJson - the files that were added in this commit.
176+
* @param {*} filesModifiedJson - the files that were modified in this commit.
177+
* @returns a list of the svg file paths that were added/modified in this pr as Path.
178+
* It will only return icons in '/icons' path (see https://github.com/devicons/devicon/issues/505)
179+
*/
180+
function getAddedModifiedSvg(filesAddedJson, filesModifiedJson) {
181+
const filesAdded = JSON.parse(filesAddedJson),
182+
filesModified = JSON.parse(filesModifiedJson)
183+
184+
files = filesAdded.concat(filesModified)
185+
return files.filter(filename => {
186+
if (path.extname(filename) == ".svg"
187+
&& path.dirname(filename).includes('icons/'))
188+
return filename
189+
})
190+
}
191+
192+
/**
193+
* Create a config option for each file.
194+
* @param {Object} file - Gulp Vinyl instance of the file
195+
* being processed.
196+
* @returns a SVGO config object.
197+
*/
198+
function configOptionCallback(file) {
199+
return {
200+
plugins: [
201+
{
202+
prefixIds: {
203+
prefix: file.stem, // add file name to ids
204+
delim: "-"
205+
}
206+
},
207+
{
208+
removeViewBox: false // keep viewbox
209+
},
210+
{
211+
removeDimensions: true // remove height and width
212+
}
213+
]
214+
}
215+
}
216+
217+
152218
exports.updateCss = createDeviconMinCSS;
153219
exports.clean = cleanUp;
220+
exports.optimizeSvg = optimizeSvg;

icons/aarch64/aarch64-original.svg

Lines changed: 1 addition & 23 deletions
Loading

icons/aarch64/aarch64-plain.svg

Lines changed: 1 addition & 23 deletions
Loading
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 1 addition & 1 deletion
Loading

icons/amazonwebservices/amazonwebservices-original-wordmark.svg

Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 1 addition & 1 deletion
Loading

icons/amazonwebservices/amazonwebservices-plain-wordmark.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)