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

Commit 7c19f1f

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
1 parent 612ec9a commit 7c19f1f

File tree

105 files changed

+1020
-681
lines changed

Some content is hidden

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

105 files changed

+1020
-681
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

+96-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,101 @@ 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+
// TODO: svg css modules
142+
143+
// Standard SVGs referenced by a URL
144+
config.loader('standard-svgs-1', () => ({
145+
test: /\.svg$/,
146+
exclude: [/\.sprite\.svg$/, /\.inline\.svg$/],
147+
loader: 'file-loader'
148+
}))
149+
150+
config.loader('standard-svgs-2', () => ({
151+
test: /\.svg$/,
152+
exclude: [/\.sprite\.svg$/, /\.inline\.svg$/],
153+
loader: 'svgo-loader',
154+
query: {
155+
plugins: [
156+
{ inlineStyles: { onlyMatchedOnce: false } },
157+
{ removeAttrs: { attrs: 'class' } }
158+
]
159+
}
160+
}))
161+
162+
// Ensure that the Gatsby's default SVG handling doesn't mess with the previous declarations
163+
config.loader('url-loader', (current) => ({
164+
...current,
165+
exclude: /\.svg$/
166+
}))
167+
168+
return current
169+
})
170+
171+
// Do not inline images
172+
// We have a lot of small images that summed together increase the inital HTML files by a lot
173+
config.loader('url-loader', (current) => ({
174+
...current,
175+
loader: 'file-loader'
176+
}))
177+
178+
// Shrink CSS module class names in production
179+
if (stage === 'build-css' || stage === 'build-javascript' || stage === 'build-html') {
180+
config.loader('cssModules', (current) => ({
181+
...current,
182+
loader: current.loader.replace(/localIdentName=[^!]+/, 'localIdentName=[hash:base64:10]')
183+
}))
184+
}
117185
}
118186

119187
exports.createLayouts = () => {
120188
// Create a layout for each locale, based on a template
121189
const layoutTemplate = fs.readFileSync(path.join(__dirname, 'src/layouts/index.js'))
122190

123-
availableLocalesAcronym.forEach((locale) => {
124-
const localeLayout = template(layoutTemplate)({ locale })
125-
.replace(/LayoutQuery/, `LayoutQuery_${locale}`)
191+
availableLocales.forEach((locale) => {
192+
const acronym = locale.acronym
193+
const localeLayout = template(layoutTemplate)({ locale: acronym })
194+
.replace(/LayoutQuery/, `LayoutQuery_${acronym}`)
126195

127-
fs.writeFileSync(path.join(__dirname, `src/layouts/index-${locale}.js`), localeLayout)
196+
fs.writeFileSync(path.join(__dirname, `src/layouts/index-${acronym}.js`), localeLayout)
128197
})
129198
}
130199

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

135204
deletePage(page)
136205

137-
availableLocalesAcronym.forEach((locale) => {
206+
availableLocales.forEach((locale) => {
207+
const acronym = locale.acronym
208+
138209
createPage({
139210
...page,
140-
layout: `index-${locale}`,
141-
path: locale === defaultLocale ? page.path : `/${locale}${page.path}`
211+
layout: `index-${acronym}`,
212+
path: acronym === defaultLocale ? page.path : `/${acronym}${page.path}`
142213
})
143214
})
144215
}

0 commit comments

Comments
 (0)