Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.

Commit 5ea4ad3

Browse files
committed
perf: use svg sprites & urls instead of all inline
- Moved images to folders closest to components, allowing for future optimizations with sprites - Renamed some files, variables and data - Fixed footer element placements - Fixed code consistenty with spaces in some files - Removed locale utils, it's not necessary at alls - Removed unused data files - Improved css modules hash in production - Improved videos sections hover animations - Fixed play icon being loaded from imgur instead of IPFS - Used standard transitions whenever possible
1 parent 87012a3 commit 5ea4ad3

File tree

147 files changed

+9041
-8596
lines changed

Some content is hidden

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

147 files changed

+9041
-8596
lines changed

gatsby-config.js

-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ module.exports = {
55
pathPrefix: '.',
66
plugins: [
77
'gatsby-plugin-react-helmet',
8-
{
9-
resolve: 'gatsby-plugin-react-svg',
10-
options: {
11-
include: /media/
12-
}
13-
},
148
'gatsby-plugin-ipfs'
159
]
1610
}

gatsby-node.js

+102-25
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ const path = require('path')
1010
const fs = require('fs')
1111
const template = require('lodash/template')
1212
const wrap = require('lodash/wrap')
13-
const { defaultLocale } = require('./intl/config')
13+
const { defaultLocale, availableLocales } = require('./intl/config')
1414
const ServiceWorkerWebpackPlugin = require('serviceworker-webpack-plugin')
15-
const { getLocalesAcronym } = require('./src/utils/getLocalesUtils')
16-
const availableLocalesAcronym = getLocalesAcronym()
15+
const SvgStorePlugin = require('external-svg-sprite-loader/lib/SvgStorePlugin')
1716

18-
module.exports.modifyWebpackConfig = ({ config, program }) => {
17+
module.exports.modifyWebpackConfig = ({ config, program, stage }) => {
1918
// Allow requires from the src/ folder
2019
config.merge({
2120
resolve: {
@@ -33,20 +32,6 @@ module.exports.modifyWebpackConfig = ({ config, program }) => {
3332
return current
3433
})
3534

36-
// Add prefix to all svg class or id selectors to avoid styles override
37-
// config.merge((current) => {
38-
// config.loader('svg-react-loader', (current) => {
39-
// current.query = {
40-
// ...current.query,
41-
// classIdPrefix: true,
42-
// };
43-
//
44-
// return current;
45-
// });
46-
//
47-
// return current;
48-
// });
49-
5035
// Allow requires from the src/ folder for postcss
5136
config.merge((current) => {
5237
const importPath = [
@@ -114,17 +99,107 @@ module.exports.modifyWebpackConfig = ({ config, program }) => {
11499

115100
return current
116101
})
102+
103+
// SVGs (external + inline + standard)
104+
config.merge((current) => {
105+
// External SVGs
106+
config.loader('external-svgs-1', (current) => ({
107+
test: /\.sprite\.svg$/,
108+
loader: 'external-svg-sprite-loader',
109+
query: {
110+
test: /\.svg$/,
111+
name: 'static/svg-sprite.[hash].svg'
112+
}
113+
}))
114+
115+
config.loader('external-svgs-2', (current) => ({
116+
test: /\.sprite\.svg$/,
117+
loader: 'svgo-loader',
118+
query: {
119+
plugins: [
120+
{ removeViewBox: false },
121+
{ removeDimensions: true },
122+
{ inlineStyles: { onlyMatchedOnce: false } },
123+
{ removeAttrs: { attrs: 'class' } }
124+
]
125+
}
126+
}))
127+
128+
current.plugins.push(new SvgStorePlugin())
129+
130+
// Inline SVGs
131+
config.loader('inline-svgs-1', () => ({
132+
loader: 'raw-loader',
133+
test: /\.inline\.svg$/
134+
}))
135+
136+
config.loader('inline-svgs-2', () => ({
137+
loader: 'svgo-loader',
138+
test: /\.inline\.svg$/
139+
}))
140+
141+
config.loader('inline-svgs-3', () => ({
142+
loader: 'svg-css-modules-loader',
143+
test: /\.inline\.svg$/,
144+
query: {
145+
transformId: true
146+
}
147+
}))
148+
149+
// Standard SVGs referenced by a URL
150+
config.loader('standard-svgs-1', () => ({
151+
test: /\.svg$/,
152+
exclude: [/\.sprite\.svg$/, /\.inline\.svg$/],
153+
loader: 'file-loader'
154+
}))
155+
156+
config.loader('standard-svgs-2', () => ({
157+
test: /\.svg$/,
158+
exclude: [/\.sprite\.svg$/, /\.inline\.svg$/],
159+
loader: 'svgo-loader',
160+
query: {
161+
plugins: [
162+
{ inlineStyles: { onlyMatchedOnce: false } },
163+
{ removeAttrs: { attrs: 'class' } }
164+
]
165+
}
166+
}))
167+
168+
// Ensure that the Gatsby's default SVG handling doesn't mess with the previous declarations
169+
config.loader('url-loader', (current) => ({
170+
...current,
171+
exclude: /\.svg$/
172+
}))
173+
174+
return current
175+
})
176+
177+
// Do not inline images
178+
// We have a lot of small images that summed together increase the inital HTML files by a lot
179+
config.loader('url-loader', (current) => ({
180+
...current,
181+
loader: 'file-loader'
182+
}))
183+
184+
// Shrink CSS module class names in production
185+
if (stage === 'build-css' || stage === 'build-javascript' || stage === 'build-html') {
186+
config.loader('cssModules', (current) => ({
187+
...current,
188+
loader: current.loader.replace(/localIdentName=[^!]+/, 'localIdentName=[hash:base64:10]')
189+
}))
190+
}
117191
}
118192

119193
exports.createLayouts = () => {
120194
// Create a layout for each locale, based on a template
121195
const layoutTemplate = fs.readFileSync(path.join(__dirname, 'src/layouts/index.js'))
122196

123-
availableLocalesAcronym.forEach((locale) => {
124-
const localeLayout = template(layoutTemplate)({ locale })
125-
.replace(/LayoutQuery/, `LayoutQuery_${locale}`)
197+
availableLocales.forEach((locale) => {
198+
const acronym = locale.acronym
199+
const localeLayout = template(layoutTemplate)({ locale: acronym })
200+
.replace(/LayoutQuery/, `LayoutQuery_${acronym}`)
126201

127-
fs.writeFileSync(path.join(__dirname, `src/layouts/index-${locale}.js`), localeLayout)
202+
fs.writeFileSync(path.join(__dirname, `src/layouts/index-${acronym}.js`), localeLayout)
128203
})
129204
}
130205

@@ -134,11 +209,13 @@ exports.onCreatePage = ({ page, boundActionCreators }) => {
134209

135210
deletePage(page)
136211

137-
availableLocalesAcronym.forEach((locale) => {
212+
availableLocales.forEach((locale) => {
213+
const acronym = locale.acronym
214+
138215
createPage({
139216
...page,
140-
layout: `index-${locale}`,
141-
path: locale === defaultLocale ? page.path : `/${locale}${page.path}`
217+
layout: `index-${acronym}`,
218+
path: acronym === defaultLocale ? page.path : `/${acronym}${page.path}`
142219
})
143220
})
144221
}

intl/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ module.exports = {
55
{ acronym: 'pl', fullForm: 'Polski' },
66
{ acronym: 'it', fullForm: 'Italiano' },
77
{ acronym: 'zh', fullForm: '简体中文' },
8-
{ acronym: 'ko', fullForm: '한국어' },
8+
{ acronym: 'ko', fullForm: '한국어' }
99
]
1010
}

0 commit comments

Comments
 (0)