Skip to content

Commit

Permalink
SVG optimizations (#1745)
Browse files Browse the repository at this point in the history
* remove legacy svgo script

* update svgo dep + new updated SVG minifier script

* minify SVGs

* update SVG paths to use *.mini.svg
  • Loading branch information
fturmel authored Oct 17, 2024
1 parent 7a537bd commit 3715c12
Show file tree
Hide file tree
Showing 108 changed files with 180 additions and 164 deletions.
63 changes: 63 additions & 0 deletions node-scripts/minify-svgs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import fs from 'node:fs/promises';
import { optimize } from 'svgo';
import { glob } from 'glob';
import path from 'node:path';

// Find non-minified SVG imports in codebase with regex search: (\S+(?<!\.mini)\.svg)

const config = {
multipass: true,
plugins: [
{
name: 'preset-default',
params: {
overrides: {
inlineStyles: {
onlyMatchedOnce: false
}
}
}
},
'removeStyleElement'
]
};

const srcImages = await glob(
['src/images/**/*.svg', 'static/images/**/*.svg'],
{ ignore: '**/*.mini.svg' }
);

let bytesSaved = 0;

const promises = srcImages.map(async (file) => {
const data = await fs.readFile(file, 'utf-8');
const result = optimize(data, config);

if (result.error) {
console.error(result.error);
process.exit(1);
}

const sizeDelta = result.data.length - data.length;
bytesSaved += sizeDelta;

console.log(file);
console.log(
sizeDelta <= 0 ? ' 🟢' : ' 🟡',
`${((sizeDelta / data.length) * 100).toFixed(2)}%`
);
console.log(' ', (sizeDelta / 1024).toFixed(2), 'kB');

const { dir, name } = path.parse(file);
const destinationFile = path.join(dir, `${name}.mini.svg`);

await fs.writeFile(destinationFile, result.data, 'utf-8');
});

await Promise.all(promises);

console.log(
'Done!',
(bytesSaved / 1024).toFixed(2),
'kB size difference total'
);
12 changes: 7 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@
"prettier": "^3.0.3",
"sharp": "^0.32.6",
"slugify": "^1.6.6",
"svgo": "^3.0.2"
"svgo": "^3.3.2"
}
}
6 changes: 3 additions & 3 deletions src/components/CodeExampleList.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import Image from './Image';

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

import NodeIcon from '../images/node-icon.svg';
import P5Icon from '../images/p5js-icon.svg';
import ProcessingLogo from '../images/processing-icon.svg';
import NodeIcon from '../images/node-icon.mini.svg';
import P5Icon from '../images/p5js-icon.mini.svg';
import ProcessingLogo from '../images/processing-icon.mini.svg';

const icons = {
p5: () => <P5Icon className={css.p5} />,
Expand Down
4 changes: 2 additions & 2 deletions src/components/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import cn from 'classnames';

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

import Logo from '../images/logo.svg';
import Logo from '../images/logo.mini.svg';

import {
FaGithub,
Expand Down Expand Up @@ -37,7 +37,7 @@ const Footer = () => {
</div>
<div className={css.train}>
<img
src="/images/train.svg"
src="/images/train.mini.svg"
width={371}
height={150}
alt="The Coding Train illustration"
Expand Down
8 changes: 4 additions & 4 deletions src/components/HomepageScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import cn from 'classnames';
import { Heading1 } from '../components/Heading';
import Spacer from './Spacer';

import Train from '../images/characters/homepage.svg';
import TrainTrack from '../images/tracks.svg';
import Sun from '../images/sun.svg';
import Cloud from '../images/cloud.svg';
import Train from '../images/characters/homepage.mini.svg';
import TrainTrack from '../images/tracks.mini.svg';
import Sun from '../images/sun.mini.svg';
import Cloud from '../images/cloud.mini.svg';

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

Expand Down
2 changes: 1 addition & 1 deletion src/components/NebulaVideoRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const NebulaVideoRow = ({ nebulaSlug, variant }) => {
rel="noopener noreferrer">
<img
className={css.svg}
src="/images/nebula-button.svg"
src="/images/nebula-button.mini.svg"
alt="Nebula logo"
/>
</a>
Expand Down
2 changes: 1 addition & 1 deletion src/components/PassengerShowcasePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Image from './Image';

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

import PlayButton from '../images/playbutton.svg';
import PlayButton from '../images/playbutton.mini.svg';
import { useIsFirstRender } from '../hooks';
import { shuffleCopy } from '../utils';

Expand Down
2 changes: 1 addition & 1 deletion src/components/Question.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import YouTubeVideo from './YouTubeVideo';

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

import Open from '../images/open.svg';
import Open from '../images/open.mini.svg';

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

Expand Down
2 changes: 1 addition & 1 deletion src/components/TimestampTimeline.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { memo } from 'react';
import cn from 'classnames';

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/components/TopBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';
import { Link } from 'gatsby';

import Menu from './Menu';
import NewLogo from '../images/new-logo.svg';
import NewLogo from '../images/new-logo.mini.svg';

import * as css from './TopBar.module.css';

Expand Down
2 changes: 1 addition & 1 deletion src/components/VideoCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Link } from 'gatsby';
import cn from 'classnames';

import Image from './Image';
import Play from '../images/play.svg';
import Play from '../images/play.mini.svg';

import * as css from './VideoCard.module.css';

Expand Down
2 changes: 1 addition & 1 deletion src/components/tracks/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { filteredPath } from '../../utils';
import * as css from './Card.module.css';
import { pattern } from '../../styles/styles.module.css';

import PlayButton from '../../images/playbutton.svg';
import PlayButton from '../../images/playbutton.mini.svg';

const FilteredVideosSection = ({ videos: allVideos, trackSlug }) => {
const [expanded, setExpanded] = useState(false);
Expand Down
2 changes: 1 addition & 1 deletion src/images/characters/AllAboard.mini.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/images/characters/Asterik_1.mini.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/images/characters/Asterik_2.mini.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/images/characters/Asterik_3.mini.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/images/characters/Asterik_4.mini.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3715c12

Please sign in to comment.