-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
index.js
512 lines (451 loc) · 14.9 KB
/
index.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* @flow */
const _ = require(`lodash`)
const slash = require(`slash`)
const fs = require(`fs-extra`)
const md5File = require(`md5-file/promise`)
const crypto = require(`crypto`)
const del = require(`del`)
const path = require(`path`)
const convertHrtime = require(`convert-hrtime`)
const Promise = require(`bluebird`)
const apiRunnerNode = require(`../utils/api-runner-node`)
const mergeGatsbyConfig = require(`../utils/merge-gatsby-config`)
const { graphql } = require(`graphql`)
const { store, emitter } = require(`../redux`)
const loadPlugins = require(`./load-plugins`)
const report = require(`gatsby-cli/lib/reporter`)
const getConfigFile = require(`./get-config-file`)
const tracer = require(`opentracing`).globalTracer()
const preferDefault = require(`./prefer-default`)
// Show stack trace on unhandled promises.
process.on(`unhandledRejection`, (reason, p) => {
report.panic(reason)
})
const {
extractQueries,
} = require(`../internal-plugins/query-runner/query-watcher`)
const {
runInitialQueries,
} = require(`../internal-plugins/query-runner/page-query-runner`)
const queryQueue = require(`../internal-plugins/query-runner/query-queue`)
const { writePages } = require(`../internal-plugins/query-runner/pages-writer`)
const {
writeRedirects,
} = require(`../internal-plugins/query-runner/redirects-writer`)
// Override console.log to add the source file + line number.
// Useful for debugging if you lose a console.log somewhere.
// Otherwise leave commented out.
// require(`./log-line-function`)
type BootstrapArgs = {
directory: string,
prefixPaths?: boolean,
parentSpan: Object,
}
module.exports = async (args: BootstrapArgs) => {
const spanArgs = args.parentSpan ? { childOf: args.parentSpan } : {}
const bootstrapSpan = tracer.startSpan(`bootstrap`, spanArgs)
const program = {
...args,
// Fix program directory path for windows env.
directory: slash(args.directory),
}
store.dispatch({
type: `SET_PROGRAM`,
payload: program,
})
// Try opening the site's gatsby-config.js file.
let activity = report.activityTimer(`open and validate gatsby-configs`, {
parentSpan: bootstrapSpan,
})
activity.start()
let config = await preferDefault(
getConfigFile(program.directory, `gatsby-config`)
)
// theme gatsby configs can be functions or objects
if (config.__experimentalThemes) {
const themesConfig = await Promise.mapSeries(
config.__experimentalThemes,
async ([themeName, themeConfig]) => {
const theme = await preferDefault(
getConfigFile(themeName, `gatsby-config`)
)
// if theme is a function, call it with the themeConfig
let themeConfigObj = theme
if (_.isFunction(theme)) {
themeConfigObj = theme(themeConfig)
}
// themes function as plugins too (gatsby-node, etc)
return {
...themeConfigObj,
plugins: [
...(themeConfigObj.plugins || []),
// theme plugin is last so it's gatsby-node, etc can override it's declared plugins, like a normal site.
{ resolve: themeName, options: themeConfig },
],
}
}
).reduce(mergeGatsbyConfig, {})
config = mergeGatsbyConfig(themesConfig, config)
}
if (config && config.polyfill) {
report.warn(
`Support for custom Promise polyfills has been removed in Gatsby v2. We only support Babel 7's new automatic polyfilling behavior.`
)
}
store.dispatch({
type: `SET_SITE_CONFIG`,
payload: config,
})
activity.end()
activity = report.activityTimer(`load plugins`)
activity.start()
const flattenedPlugins = await loadPlugins(config)
activity.end()
// onPreInit
activity = report.activityTimer(`onPreInit`, {
parentSpan: bootstrapSpan,
})
activity.start()
await apiRunnerNode(`onPreInit`, { parentSpan: activity.span })
activity.end()
// Delete html and css files from the public directory as we don't want
// deleted pages and styles from previous builds to stick around.
activity = report.activityTimer(
`delete html and css files from previous builds`,
{
parentSpan: bootstrapSpan,
}
)
activity.start()
await del([
`public/*.{html,css}`,
`public/**/*.{html,css}`,
`!public/static`,
`!public/static/**/*.{html,css}`,
])
activity.end()
activity = report.activityTimer(`initialize cache`)
activity.start()
// Check if any plugins have been updated since our last run. If so
// we delete the cache is there's likely been changes
// since the previous run.
//
// We do this by creating a hash of all the version numbers of installed
// plugins, the site's package.json, gatsby-config.js, and gatsby-node.js.
// The last, gatsby-node.js, is important as many gatsby sites put important
// logic in there e.g. generating slugs for custom pages.
const pluginVersions = flattenedPlugins.map(p => p.version)
const hashes = await Promise.all([
md5File(`package.json`),
Promise.resolve(
md5File(`${program.directory}/gatsby-config.js`).catch(() => {})
), // ignore as this file isn't required),
Promise.resolve(
md5File(`${program.directory}/gatsby-node.js`).catch(() => {})
), // ignore as this file isn't required),
])
const pluginsHash = crypto
.createHash(`md5`)
.update(JSON.stringify(pluginVersions.concat(hashes)))
.digest(`hex`)
let state = store.getState()
const oldPluginsHash = state && state.status ? state.status.PLUGINS_HASH : ``
// Check if anything has changed. If it has, delete the site's .cache
// directory and tell reducers to empty themselves.
//
// Also if the hash isn't there, then delete things just in case something
// is weird.
if (oldPluginsHash && pluginsHash !== oldPluginsHash) {
report.info(report.stripIndent`
One or more of your plugins have changed since the last time you ran Gatsby. As
a precaution, we're deleting your site's cache to ensure there's not any stale
data
`)
}
if (!oldPluginsHash || pluginsHash !== oldPluginsHash) {
try {
await fs.remove(`${program.directory}/.cache`)
} catch (e) {
report.error(`Failed to remove .cache files.`, e)
}
// Tell reducers to delete their data (the store will already have
// been loaded from the file system cache).
store.dispatch({
type: `DELETE_CACHE`,
})
}
// Update the store with the new plugins hash.
store.dispatch({
type: `UPDATE_PLUGINS_HASH`,
payload: pluginsHash,
})
// Now that we know the .cache directory is safe, initialize the cache
// directory.
await fs.ensureDir(`${program.directory}/.cache`)
// Ensure the public/static directory
await fs.ensureDir(`${program.directory}/public/static`)
activity.end()
// Copy our site files to the root of the site.
activity = report.activityTimer(`copy gatsby files`, {
parentSpan: bootstrapSpan,
})
activity.start()
const srcDir = `${__dirname}/../../cache-dir`
const siteDir = `${program.directory}/.cache`
const tryRequire = `${__dirname}/../utils/test-require-error.js`
try {
await fs.copy(srcDir, siteDir, {
clobber: true,
})
await fs.copy(tryRequire, `${siteDir}/test-require-error.js`, {
clobber: true,
})
await fs.ensureDirSync(`${program.directory}/.cache/json`)
// Ensure .cache/fragments exists and is empty. We want fragments to be
// added on every run in response to data as fragments can only be added if
// the data used to create the schema they're dependent on is available.
await fs.emptyDir(`${program.directory}/.cache/fragments`)
} catch (err) {
report.panic(`Unable to copy site files to .cache`, err)
}
// Find plugins which implement gatsby-browser and gatsby-ssr and write
// out api-runners for them.
const hasAPIFile = (env, plugin) => {
// The plugin loader has disabled SSR APIs for this plugin. Usually due to
// multiple implementations of an API that can only be implemented once
if (env === `ssr` && plugin.skipSSR === true) return undefined
const envAPIs = plugin[`${env}APIs`]
// Always include the site's gatsby-browser.js if it exists as it's
// a handy place to include global styles and other global imports.
try {
if (env === `browser` && plugin.name === `default-site-plugin`) {
return slash(
require.resolve(path.join(plugin.resolve, `gatsby-${env}`))
)
}
} catch (e) {
// ignore
}
if (envAPIs && Array.isArray(envAPIs) && envAPIs.length > 0) {
return slash(path.join(plugin.resolve, `gatsby-${env}`))
}
return undefined
}
const ssrPlugins = _.filter(
flattenedPlugins.map(plugin => {
return {
resolve: hasAPIFile(`ssr`, plugin),
options: plugin.pluginOptions,
}
}),
plugin => plugin.resolve
)
const browserPlugins = _.filter(
flattenedPlugins.map(plugin => {
return {
resolve: hasAPIFile(`browser`, plugin),
options: plugin.pluginOptions,
}
}),
plugin => plugin.resolve
)
const browserPluginsRequires = browserPlugins
.map(
plugin =>
`{
plugin: require('${plugin.resolve}'),
options: ${JSON.stringify(plugin.options)},
}`
)
.join(`,`)
const browserAPIRunner = `module.exports = [${browserPluginsRequires}]\n`
let sSRAPIRunner = ``
try {
sSRAPIRunner = fs.readFileSync(`${siteDir}/api-runner-ssr.js`, `utf-8`)
} catch (err) {
report.panic(`Failed to read ${siteDir}/api-runner-ssr.js`, err)
}
const ssrPluginsRequires = ssrPlugins
.map(
plugin =>
`{
plugin: require('${plugin.resolve}'),
options: ${JSON.stringify(plugin.options)},
}`
)
.join(`,`)
sSRAPIRunner = `var plugins = [${ssrPluginsRequires}]\n${sSRAPIRunner}`
fs.writeFileSync(
`${siteDir}/api-runner-browser-plugins.js`,
browserAPIRunner,
`utf-8`
)
fs.writeFileSync(`${siteDir}/api-runner-ssr.js`, sSRAPIRunner, `utf-8`)
activity.end()
/**
* Start the main bootstrap processes.
*/
// onPreBootstrap
activity = report.activityTimer(`onPreBootstrap`)
activity.start()
await apiRunnerNode(`onPreBootstrap`)
activity.end()
// Source nodes
activity = report.activityTimer(`source and transform nodes`, {
parentSpan: bootstrapSpan,
})
activity.start()
await require(`../utils/source-nodes`)({ parentSpan: activity.span })
activity.end()
// Create Schema.
activity = report.activityTimer(`building schema`, {
parentSpan: bootstrapSpan,
})
activity.start()
await require(`../schema`)({ parentSpan: activity.span })
activity.end()
// Collect resolvable extensions and attach to program.
const extensions = [`.mjs`, `.js`, `.jsx`, `.wasm`, `.json`]
// Change to this being an action and plugins implement `onPreBootstrap`
// for adding extensions.
const apiResults = await apiRunnerNode(`resolvableExtensions`, {
traceId: `initial-resolvableExtensions`,
parentSpan: bootstrapSpan,
})
store.dispatch({
type: `SET_PROGRAM_EXTENSIONS`,
payload: _.flattenDeep([extensions, apiResults]),
})
const graphqlRunner = (query, context = {}) => {
const schema = store.getState().schema
return graphql(schema, query, context, context, context)
}
// Collect pages.
activity = report.activityTimer(`createPages`, {
parentSpan: bootstrapSpan,
})
activity.start()
await apiRunnerNode(`createPages`, {
graphql: graphqlRunner,
traceId: `initial-createPages`,
waitForCascadingActions: true,
parentSpan: activity.span,
})
activity.end()
// A variant on createPages for plugins that want to
// have full control over adding/removing pages. The normal
// "createPages" API is called every time (during development)
// that data changes.
activity = report.activityTimer(`createPagesStatefully`, {
parentSpan: bootstrapSpan,
})
activity.start()
await apiRunnerNode(`createPagesStatefully`, {
graphql: graphqlRunner,
traceId: `initial-createPagesStatefully`,
waitForCascadingActions: true,
parentSpan: activity.span,
})
activity.end()
activity = report.activityTimer(`onPreExtractQueries`, {
parentSpan: bootstrapSpan,
})
activity.start()
await apiRunnerNode(`onPreExtractQueries`, { parentSpan: activity.span })
activity.end()
// Update Schema for SitePage.
activity = report.activityTimer(`update schema`, {
parentSpan: bootstrapSpan,
})
activity.start()
await require(`../schema`)({ parentSpan: activity.span })
activity.end()
require(`../schema/type-conflict-reporter`).printConflicts()
// Extract queries
activity = report.activityTimer(`extract queries from components`, {
parentSpan: bootstrapSpan,
})
activity.start()
await extractQueries()
activity.end()
// Start the createPages hot reloader.
if (process.env.NODE_ENV !== `production`) {
require(`./page-hot-reloader`)(graphqlRunner)
}
// Run queries
activity = report.activityTimer(`run graphql queries`, {
parentSpan: bootstrapSpan,
})
activity.start()
const startQueries = process.hrtime()
queryQueue.on(`task_finish`, () => {
const stats = queryQueue.getStats()
activity.setStatus(
`${stats.total}/${stats.peak} ${(
stats.total / convertHrtime(process.hrtime(startQueries)).seconds
).toFixed(2)} queries/second`
)
})
await runInitialQueries(activity)
activity.end()
// Write out files.
activity = report.activityTimer(`write out page data`, {
parentSpan: bootstrapSpan,
})
activity.start()
try {
await writePages()
} catch (err) {
report.panic(`Failed to write out page data`, err)
}
activity.end()
// Write out redirects.
activity = report.activityTimer(`write out redirect data`, {
parentSpan: bootstrapSpan,
})
activity.start()
await writeRedirects()
activity.end()
const checkJobsDone = _.debounce(resolve => {
const state = store.getState()
if (state.jobs.active.length === 0) {
report.log(``)
report.info(`bootstrap finished - ${process.uptime()} s`)
report.log(``)
// onPostBootstrap
activity = report.activityTimer(`onPostBootstrap`, {
parentSpan: bootstrapSpan,
})
activity.start()
apiRunnerNode(`onPostBootstrap`, { parentSpan: activity.span }).then(
() => {
activity.end()
bootstrapSpan.finish()
resolve({ graphqlRunner })
}
)
}
}, 100)
if (store.getState().jobs.active.length === 0) {
// onPostBootstrap
activity = report.activityTimer(`onPostBootstrap`, {
parentSpan: bootstrapSpan,
})
activity.start()
await apiRunnerNode(`onPostBootstrap`, { parentSpan: activity.span })
activity.end()
bootstrapSpan.finish()
report.log(``)
report.info(`bootstrap finished - ${process.uptime()} s`)
report.log(``)
emitter.emit(`BOOTSTRAP_FINISHED`)
return {
graphqlRunner,
}
} else {
return new Promise(resolve => {
// Wait until all side effect jobs are finished.
emitter.on(`END_JOB`, () => checkJobsDone(resolve))
})
}
}