This repository has been archived by the owner on Jul 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
gatsby-node.js
253 lines (220 loc) · 7.36 KB
/
gatsby-node.js
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
const yaml = require('js-yaml')
const _ = require('lodash')
const fs = require('fs')
const path = require(`path`)
const { computeSitemap } = require('./node_src/sitemap')
const {
fetchMdnResource,
fetchCaniuseResource,
fetchGithubResource,
normalizeGithubResource
} = require('./node_src/resources')
const { omit } = require('lodash')
require('dotenv').config({
path: `.env`
})
const rawSitemap = yaml.safeLoad(fs.readFileSync('./config/raw_sitemap.yml', 'utf8'))
const locales = yaml.safeLoad(fs.readFileSync('./config/locales.yml', 'utf8'))
const features = yaml.safeLoad(fs.readFileSync('./src/data/features.yml', 'utf8'))
const entities = yaml.safeLoad(fs.readFileSync('./src/data/entities.yml', 'utf8'))
const guessPageTemplate = type => {
let template
switch (type) {
case 'features':
template = 'modules/features/Features'
break
case 'tools':
template = 'modules/tools/Tools'
break
default:
throw new Error(`no template defined for page type: ${type}`)
}
return path.resolve(`./src/${template}Template.js`)
}
const localizedPath = (path, locale) =>
locale.path === 'default' ? path : `/${locale.path}${path}`
const getPageContext = page => {
const context = omit(page, ['path', 'children', 'is_hidden'])
context.basePath = page.path
return {
...context,
...page.data
}
}
const createBlockPages = (page, context, createPage) => {
const blocks = page.blocks
if (!Array.isArray(blocks) || blocks.length === 0) {
return
}
blocks.forEach(block => {
locales.forEach(locale => {
const blockPage = {
path: localizedPath(block.path, locale),
component: path.resolve(`./src/core/share/ShareBlockTemplate.js`),
context: {
...context,
redirect: `${localizedPath(page.path, locale)}#${block.id}`,
block: block.id,
locale: locale.locale,
localePath: locale.path === 'default' ? '' : `/${locale.path}`
}
}
createPage(blockPage)
})
})
}
exports.createPages = async ({ actions: { createPage } }) => {
const { flat } = await computeSitemap(rawSitemap)
flat.forEach(page => {
const context = getPageContext(page)
if (page.type !== 'page') {
const template = guessPageTemplate(page.type)
locales.forEach(locale => {
createPage({
path: localizedPath(page.path, locale),
component: template,
context: {
...context,
locale: locale.locale,
localeLabel: locale.label,
localePath: locale.path === 'default' ? '' : `/${locale.path}`
}
})
})
}
createBlockPages(page, context, createPage)
})
}
/**
* Fix case for pages path, it's not obvious on OSX which is case insensitive,
* but on some environments (eg. travis), it's a problem.
*
* Many pages are created from components, and we use upper first in that case
* for the file name, so when gatsby generates the static page, it has the same name.
*
* Implement the Gatsby API “onCreatePage”.
* This is called after every page is created.
*/
exports.onCreatePage = async ({ page, actions }) => {
const { createPage, deletePage } = actions
const { flat } = await computeSitemap(rawSitemap)
// handle 404 page separately
const is404 = page.path.includes('404')
const pagePath = page.path.toLowerCase()
const matchingPage = flat.find(p => p.path === (is404 ? '/404/' : pagePath))
// if there's no matching page
// it means we're dealing with an internal page
// thus, we don't create one for each locale
if (matchingPage === undefined) {
if (pagePath !== page.path) {
deletePage(page)
createPage({
...page,
path: pagePath
})
}
return
}
// add context, required for pagination
const context = {
...page.context,
...getPageContext(matchingPage)
}
const newPage = {
...page,
path: pagePath,
context
}
deletePage(page)
// create page for each available locale
for (let locale of locales) {
createPage({
...newPage,
path: localizedPath(newPage.path, locale),
context: {
...newPage.context,
locale: locale.locale,
localeLabel: locale.label,
localePath: locale.path === 'default' ? '' : `/${locale.path}`
}
})
}
createBlockPages(page, context, createPage)
}
// Allow absolute imports and inject `ENV`
exports.onCreateWebpackConfig = ({ stage, actions, plugins }) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules']
},
plugins: [
plugins.define({
ENV: stage === `develop` || stage === `develop-html` ? 'development' : 'production'
})
]
})
}
/*
When GitHub API rate limits are hit, GraphQL API will break
unless default values are provided.
*/
const defaultGitHubObject = {
name: '',
full_name: '',
description: '',
url: '',
stars: -99,
forks: -99,
opened_issues: -99,
homepage: ''
}
exports.onCreateNode = async ({ node, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `FeaturesUsageYaml`) {
const nodeResources = []
for (const agg of node.aggregations) {
const aggResources = {
id: agg.id
}
const featureResourcesConfig = features.find(f => f.id === agg.id)
if (featureResourcesConfig !== undefined) {
if (featureResourcesConfig.mdn !== undefined) {
aggResources.mdn = await fetchMdnResource(featureResourcesConfig.mdn)
}
if (featureResourcesConfig.caniuse !== undefined) {
aggResources.caniuse = await fetchCaniuseResource(
featureResourcesConfig.caniuse
)
}
}
nodeResources.push(aggResources)
}
await createNodeField({
name: `resources`,
node,
value: nodeResources
})
}
if (node.internal.type === 'ToolsYaml') {
const nodeResources = []
for (const agg of node.aggregations) {
const aggResources = {
id: agg.id,
github: defaultGitHubObject // pass default object to avoid GraphQL errors
}
const entityResourcesConfig = entities.find(e => e.id === agg.id)
if (entityResourcesConfig) {
aggResources.entity = entityResourcesConfig
if (entityResourcesConfig.github) {
aggResources.github = await fetchGithubResource(entityResourcesConfig.github)
}
}
nodeResources.push(aggResources)
}
await createNodeField({
name: `resources`,
node,
value: nodeResources
})
}
}