Skip to content

Commit 3715c12

Browse files
authored
SVG optimizations (#1745)
* remove legacy svgo script * update svgo dep + new updated SVG minifier script * minify SVGs * update SVG paths to use *.mini.svg
1 parent 7a537bd commit 3715c12

File tree

108 files changed

+180
-164
lines changed

Some content is hidden

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

108 files changed

+180
-164
lines changed

node-scripts/minify-svgs.mjs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import fs from 'node:fs/promises';
2+
import { optimize } from 'svgo';
3+
import { glob } from 'glob';
4+
import path from 'node:path';
5+
6+
// Find non-minified SVG imports in codebase with regex search: (\S+(?<!\.mini)\.svg)
7+
8+
const config = {
9+
multipass: true,
10+
plugins: [
11+
{
12+
name: 'preset-default',
13+
params: {
14+
overrides: {
15+
inlineStyles: {
16+
onlyMatchedOnce: false
17+
}
18+
}
19+
}
20+
},
21+
'removeStyleElement'
22+
]
23+
};
24+
25+
const srcImages = await glob(
26+
['src/images/**/*.svg', 'static/images/**/*.svg'],
27+
{ ignore: '**/*.mini.svg' }
28+
);
29+
30+
let bytesSaved = 0;
31+
32+
const promises = srcImages.map(async (file) => {
33+
const data = await fs.readFile(file, 'utf-8');
34+
const result = optimize(data, config);
35+
36+
if (result.error) {
37+
console.error(result.error);
38+
process.exit(1);
39+
}
40+
41+
const sizeDelta = result.data.length - data.length;
42+
bytesSaved += sizeDelta;
43+
44+
console.log(file);
45+
console.log(
46+
sizeDelta <= 0 ? ' 🟢' : ' 🟡',
47+
`${((sizeDelta / data.length) * 100).toFixed(2)}%`
48+
);
49+
console.log(' ', (sizeDelta / 1024).toFixed(2), 'kB');
50+
51+
const { dir, name } = path.parse(file);
52+
const destinationFile = path.join(dir, `${name}.mini.svg`);
53+
54+
await fs.writeFile(destinationFile, result.data, 'utf-8');
55+
});
56+
57+
await Promise.all(promises);
58+
59+
console.log(
60+
'Done!',
61+
(bytesSaved / 1024).toFixed(2),
62+
'kB size difference total'
63+
);

package-lock.json

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@
6262
"prettier": "^3.0.3",
6363
"sharp": "^0.32.6",
6464
"slugify": "^1.6.6",
65-
"svgo": "^3.0.2"
65+
"svgo": "^3.3.2"
6666
}
6767
}

src/components/CodeExampleList.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import Image from './Image';
55

66
import * as css from './CodeExampleList.module.css';
77

8-
import NodeIcon from '../images/node-icon.svg';
9-
import P5Icon from '../images/p5js-icon.svg';
10-
import ProcessingLogo from '../images/processing-icon.svg';
8+
import NodeIcon from '../images/node-icon.mini.svg';
9+
import P5Icon from '../images/p5js-icon.mini.svg';
10+
import ProcessingLogo from '../images/processing-icon.mini.svg';
1111

1212
const icons = {
1313
p5: () => <P5Icon className={css.p5} />,

src/components/Footer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import cn from 'classnames';
55

66
import * as css from './Footer.module.css';
77

8-
import Logo from '../images/logo.svg';
8+
import Logo from '../images/logo.mini.svg';
99

1010
import {
1111
FaGithub,
@@ -37,7 +37,7 @@ const Footer = () => {
3737
</div>
3838
<div className={css.train}>
3939
<img
40-
src="/images/train.svg"
40+
src="/images/train.mini.svg"
4141
width={371}
4242
height={150}
4343
alt="The Coding Train illustration"

src/components/HomepageScene.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import cn from 'classnames';
44
import { Heading1 } from '../components/Heading';
55
import Spacer from './Spacer';
66

7-
import Train from '../images/characters/homepage.svg';
8-
import TrainTrack from '../images/tracks.svg';
9-
import Sun from '../images/sun.svg';
10-
import Cloud from '../images/cloud.svg';
7+
import Train from '../images/characters/homepage.mini.svg';
8+
import TrainTrack from '../images/tracks.mini.svg';
9+
import Sun from '../images/sun.mini.svg';
10+
import Cloud from '../images/cloud.mini.svg';
1111

1212
import SemiColonCharacter from '../images/characters/SemiColon_hello.mini.svg';
1313

src/components/NebulaVideoRow.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const NebulaVideoRow = ({ nebulaSlug, variant }) => {
1313
rel="noopener noreferrer">
1414
<img
1515
className={css.svg}
16-
src="/images/nebula-button.svg"
16+
src="/images/nebula-button.mini.svg"
1717
alt="Nebula logo"
1818
/>
1919
</a>

src/components/PassengerShowcasePanel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Image from './Image';
55

66
import * as css from './PassengerShowcasePanel.module.css';
77

8-
import PlayButton from '../images/playbutton.svg';
8+
import PlayButton from '../images/playbutton.mini.svg';
99
import { useIsFirstRender } from '../hooks';
1010
import { shuffleCopy } from '../utils';
1111

src/components/Question.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import YouTubeVideo from './YouTubeVideo';
66

77
import * as css from './Question.module.css';
88

9-
import Open from '../images/open.svg';
9+
import Open from '../images/open.mini.svg';
1010

1111
import { useLinkParsedText } from '../hooks';
1212

src/components/TimestampTimeline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { memo } from 'react';
22
import cn from 'classnames';
33

4-
import PlayButton from '../images/playbutton-filled.svg';
4+
import PlayButton from '../images/playbutton-filled.mini.svg';
55

66
import * as css from './TimestampTimeline.module.css';
77

0 commit comments

Comments
 (0)