-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgatsby-config.ts
More file actions
313 lines (304 loc) · 7.17 KB
/
gatsby-config.ts
File metadata and controls
313 lines (304 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import type { GatsbyConfig } from 'gatsby';
import util from 'util';
import { exec } from 'child_process';
const execProm = util.promisify(exec);
const siteUrl = 'https://www.lordliverpool.com/';
const title = 'Britain’s Greatest Prime Minister: Lord Liverpool';
const short_sub_name = 'Lord Liverpool';
const description =
'Britain’s Greatest Prime Minister: Lord Liverpool unpicks two centuries of Whig history to redeem Lord Liverpool (1770-1828) from ‘arch-mediocrity’ and establish him as the greatest political leader the country has ever seen.';
const author = 'Martin Hutchinson';
async function getGitModifiedTime(
absolutePath: string | null,
curTime: string
): Promise<string> {
if (!absolutePath) {
return curTime;
}
const { stdout, stderr } = await execProm(
`git log -1 --pretty=format:%aI -- ${absolutePath}`
);
if (stderr || !stdout) {
return curTime;
}
return stdout;
}
function getFolderAndNormalizedFilename(path: string): {
folder: string;
filename: string;
} {
const parts = path.split('/');
const filenameWithExtension = parts.pop() || '';
const filename = filenameWithExtension.split('.')[0];
const folder = parts.join('/');
return { folder, filename };
}
const config: GatsbyConfig = {
siteMetadata: {
title,
description,
author,
siteUrl,
},
trailingSlash: 'ignore',
graphqlTypegen: {
typesOutputPath: 'src/gatsby-types.d.ts',
generateOnBuild: true,
documentSearchPaths: [
'./gatsby-node.ts',
'./plugins/**/gatsby-node.ts',
'./src/utils/queries.ts',
],
},
flags: {
FAST_DEV: true,
PARALLEL_SOURCING: true,
},
plugins: [
'gatsby-plugin-perf-budgets',
{
resolve: 'gatsby-plugin-webpack-bundle-analyser-v2',
options: {
devMode: false,
analyzerMode: 'static',
},
},
{
resolve: 'gatsby-plugin-typescript',
options: {
isTSX: true,
allExtensions: true,
},
},
{
resolve: 'gatsby-plugin-typography',
options: {
pathToConfigModule: 'src/utils/typography.tsx',
omitGoogleFont: true,
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
name: 'images',
path: `${__dirname}/src/images`,
},
},
'gatsby-plugin-remove-fingerprints',
'gatsby-plugin-image',
{
resolve: 'gatsby-plugin-sharp',
options: {
defaults: {
formats: ['auto', 'webp'],
placeholder: 'dominantColor',
quality: 50,
breakpoints: [750, 1080, 1366, 1920],
backgroundColor: 'transparent',
tracedSVGOptions: {},
blurredOptions: {},
jpgOptions: {},
pngOptions: {},
webpOptions: {},
avifOptions: {},
},
},
},
'gatsby-transformer-sharp',
{
resolve: 'gatsby-plugin-sitemap',
options: {
/* NOTE: types generated from repeated code in src/utils/queries.ts */
query: `
query sitemapInConfig {
allSitePage {
nodes {
component
path
}
}
site {
siteMetadata {
siteUrl
}
}
allMarkdownRemark {
edges {
node {
fields {
slug
sourceInstanceName
}
fileAbsolutePath
}
}
}
}`,
resolvePages: async (
queryData: Queries.sitemapInConfigQuery
) => {
/* Logic: Use last git modified time for all pages. Base
page on content markdown if available, most recent modified
markdown for templates, or component in that order. Use
current time as fallback. */
const curTime = new Date().toISOString();
const allPagesFromQuery = queryData.allSitePage.nodes;
const allMarkdownNodesFromQuery =
queryData.allMarkdownRemark.edges;
const allMarkdownNodesWithGitTimes = await Promise.all(
allMarkdownNodesFromQuery.map(async ({ node }) => {
const { slug, sourceInstanceName } =
node.fields as {
slug: string;
sourceInstanceName: string;
};
const { fileAbsolutePath } = node;
const gitTime = await getGitModifiedTime(
fileAbsolutePath,
curTime
);
return {
slug,
sourceInstanceName,
fileAbsolutePath,
mtime: gitTime,
};
})
);
return await Promise.all(
allPagesFromQuery.map(async (page) => {
const component = page.component;
const path = page.path;
const normalizedPathObject =
getFolderAndNormalizedFilename(path);
const markdownNode =
allMarkdownNodesWithGitTimes.find(
(node) => node.slug === path
);
let mtime;
if (markdownNode) {
/* Regular markdown node */
mtime = markdownNode.mtime;
} else if (
[
'contenders',
'chronology',
'miscellany',
].includes(normalizedPathObject.filename)
) {
/* The latest modified markdown node
corresponding to this template index */
mtime = allMarkdownNodesWithGitTimes
.filter(
(node) =>
node.sourceInstanceName ===
normalizedPathObject.filename
)
.sort(
(a, b) =>
parseInt(b.mtime, 10) -
parseInt(a.mtime, 10)
)[0].mtime;
} else {
/* Non-markdown, non-template index regular page
uses component modified time */
mtime = await getGitModifiedTime(
component,
curTime
);
}
if (mtime) {
return {
...page,
mtime: new Date(mtime).toISOString(),
};
} else {
return { ...page, mtime: curTime };
}
})
);
},
serialize: ({
path,
mtime,
}: {
path: string;
mtime: string;
}) => {
return {
url: path,
lastmod: mtime,
};
},
resolveSiteUrl: () => siteUrl,
},
},
{
resolve: 'gatsby-plugin-robots-txt',
options: {
sitemap: `${siteUrl}sitemap-index.xml`,
policy: [{ userAgent: '*', allow: '/' }],
},
},
{
resolve: 'gatsby-plugin-manifest',
options: {
name: title,
short_name: short_sub_name,
start_url: '/',
lang: 'en',
background_color: '#260101',
theme_color: '#260101',
display: 'minimal-ui',
icon: 'src/images/bgpmicon.png', // This path is relative to the root of the site.
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/content/miscellany`,
name: 'miscellany',
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/content/chronology`,
name: 'chronology',
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/content/contenders`,
name: 'contenders',
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/content/pages`,
name: 'pages',
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/content/navigation`,
name: 'navigation',
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/content/retailers`,
name: 'retailers',
},
},
'gatsby-transformer-remark',
'gatsby-plugin-catch-links',
'gatsby-plugin-netlify',
'gatsby-plugin-netlify-cms', // should be last in the array or close to it
],
};
export default config;