-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
build-html.ts
880 lines (778 loc) · 23.9 KB
/
build-html.ts
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
import Bluebird from "bluebird"
import fs from "fs-extra"
import reporter from "gatsby-cli/lib/reporter"
import { createErrorFromString } from "gatsby-cli/lib/reporter/errors"
import { chunk } from "lodash"
import { build, watch } from "../utils/webpack/bundle"
import * as path from "path"
import fastq from "fastq"
import { emitter, store } from "../redux"
import webpack from "webpack"
import webpackConfig from "../utils/webpack.config"
import { structureWebpackErrors } from "../utils/webpack-error-utils"
import * as buildUtils from "./build-utils"
import { getPageData } from "../utils/get-page-data"
import { Span } from "opentracing"
import { IProgram, Stage } from "./types"
import { ROUTES_DIRECTORY } from "../constants"
import { PackageJson } from "../.."
import { getPublicPath } from "../utils/get-public-path"
import type { GatsbyWorkerPool } from "../utils/worker/pool"
import { stitchSliceForAPage } from "../utils/slices/stitching"
import type { ISlicePropsEntry } from "../utils/worker/child/render-html"
import { getPageMode } from "../utils/page-mode"
import { extractUndefinedGlobal } from "../utils/extract-undefined-global"
import { modifyPageDataForErrorMessage } from "../utils/page-data"
import { setFilesFromDevelopHtmlCompilation } from "../utils/webpack/utils/is-file-inside-compilations"
type IActivity = any // TODO
const isPreview = process.env.GATSBY_IS_PREVIEW === `true`
export interface IBuildArgs extends IProgram {
directory: string
sitePackageJson: PackageJson
prefixPaths: boolean
noUglify: boolean
logPages: boolean
writeToFile: boolean
profile: boolean
graphqlTracing: boolean
openTracingConfigFile: string
// TODO remove in v4
keepPageRenderer: boolean
}
interface IBuildRendererResult {
rendererPath: string
stats: webpack.Stats
close: ReturnType<typeof watch>["close"]
}
let devssrWebpackCompiler: webpack.Watching | undefined
let SSRBundleReceivedInvalidEvent = true
let SSRBundleWillInvalidate = false
export function devSSRWillInvalidate(): void {
// we only get one `invalid` callback, so if we already
// set this to true, we can't really await next `invalid` event
if (!SSRBundleReceivedInvalidEvent) {
SSRBundleWillInvalidate = true
}
}
let activeRecompilations = 0
export const getDevSSRWebpack = (): {
recompileAndResumeWatching: (
allowTimedFallback: boolean
) => Promise<() => void>
needToRecompileSSRBundle: boolean
} => {
if (process.env.gatsby_executing_command !== `develop`) {
throw new Error(`This function can only be called in development`)
}
const watcher = devssrWebpackCompiler as webpack.Watching
const compiler = (devssrWebpackCompiler as webpack.Watching).compiler
if (watcher && compiler) {
return {
recompileAndResumeWatching: async function recompileAndResumeWatching(
allowTimedFallback: boolean
): Promise<() => void> {
let isResolved = false
return await new Promise<() => void>(resolve => {
function stopWatching(): void {
activeRecompilations--
if (activeRecompilations === 0) {
watcher.suspend()
}
}
let timeout
function finish(): void {
emitter.off(`DEV_SSR_COMPILATION_DONE`, finish)
if (!isResolved) {
isResolved = true
resolve(stopWatching)
}
if (timeout) {
clearTimeout(timeout)
}
}
emitter.on(`DEV_SSR_COMPILATION_DONE`, finish)
// we reset it before we start compilation to be able to catch any invalid events
SSRBundleReceivedInvalidEvent = false
if (activeRecompilations === 0) {
watcher.resume()
}
activeRecompilations++
if (allowTimedFallback) {
// Timeout after 1.5s.
timeout = setTimeout(() => {
if (!isResolved) {
isResolved = true
resolve(stopWatching)
}
}, 1500)
}
})
},
needToRecompileSSRBundle:
SSRBundleReceivedInvalidEvent || SSRBundleWillInvalidate,
}
} else {
return {
needToRecompileSSRBundle: false,
recompileAndResumeWatching: (): Promise<() => void> =>
Promise.resolve((): void => {}),
}
}
}
let oldHash = ``
let newHash = ``
const runWebpack = (
compilerConfig,
stage: Stage,
directory: string
): Promise<{
stats: webpack.Stats
close: ReturnType<typeof watch>["close"]
}> => {
const isDevSSREnabledAndViable =
process.env.GATSBY_EXPERIMENTAL_DEV_SSR && stage === `develop-html`
return new Promise((resolve, reject) => {
if (isDevSSREnabledAndViable) {
const compiler = webpack(compilerConfig)
// because of this line we can't use our watch helper
// These things should use emitter
compiler.hooks.invalid.tap(`ssr file invalidation`, () => {
SSRBundleReceivedInvalidEvent = true
SSRBundleWillInvalidate = false // we were waiting for this event, we are no longer waiting for it
})
let isFirstCompile = true
const watcher = compiler.watch(
{
ignored: /node_modules/,
},
(err, stats) => {
// this runs multiple times
emitter.emit(`DEV_SSR_COMPILATION_DONE`)
if (isFirstCompile) {
watcher.suspend()
isFirstCompile = false
}
if (err) {
return reject(err)
} else {
newHash = stats?.hash || ``
const {
restartWorker,
} = require(`../utils/dev-ssr/render-dev-html`)
// Make sure we use the latest version during development
if (oldHash !== `` && newHash !== oldHash) {
restartWorker(`${directory}/${ROUTES_DIRECTORY}render-page.js`)
}
oldHash = newHash
return resolve({
stats: stats as webpack.Stats,
close: () =>
new Promise((resolve, reject): void =>
watcher.close(err => (err ? reject(err) : resolve()))
),
})
}
}
)
devssrWebpackCompiler = watcher
} else {
build(compilerConfig).then(
({ stats, close }) => {
resolve({ stats, close })
},
err => reject(err)
)
}
})
}
const doBuildRenderer = async (
directory: string,
webpackConfig: webpack.Configuration,
stage: Stage
): Promise<IBuildRendererResult> => {
const { stats, close } = await runWebpack(webpackConfig, stage, directory)
if (stats?.hasErrors()) {
reporter.panicOnBuild(
structureWebpackErrors(stage, stats.compilation.errors)
)
}
if (stage === `develop-html`) {
setFilesFromDevelopHtmlCompilation(stats.compilation)
}
// render-page.js is hard coded in webpack.config
return {
rendererPath: `${directory}/${ROUTES_DIRECTORY}render-page.js`,
stats,
close,
}
}
const doBuildPartialHydrationRenderer = async (
directory: string,
webpackConfig: webpack.Configuration,
stage: Stage
): Promise<IBuildRendererResult> => {
const { stats, close } = await runWebpack(webpackConfig, stage, directory)
if (stats?.hasErrors()) {
reporter.panicOnBuild(
structureWebpackErrors(stage, stats.compilation.errors)
)
}
// render-page.js is hard coded in webpack.config
return {
rendererPath: `${directory}/${ROUTES_DIRECTORY}render-page.js`,
stats,
close,
}
}
export const buildRenderer = async (
program: IProgram,
stage: Stage,
parentSpan?: IActivity
): Promise<IBuildRendererResult> => {
const config = await webpackConfig(program, program.directory, stage, null, {
parentSpan,
})
return doBuildRenderer(program.directory, config, stage)
}
export const buildPartialHydrationRenderer = async (
program: IProgram,
stage: Stage,
parentSpan?: IActivity
): Promise<IBuildRendererResult> => {
const config = await webpackConfig(program, program.directory, stage, null, {
parentSpan,
})
for (const rule of config.module.rules) {
if (`./test.js`.match(rule.test)) {
if (!rule.use) {
rule.use = []
}
if (!Array.isArray(rule.use)) {
rule.use = [rule.use]
}
rule.use.push({
loader: require.resolve(
`../utils/webpack/loaders/partial-hydration-reference-loader`
),
})
}
}
// TODO add caching
config.cache = false
config.output.path = path.join(
program.directory,
`.cache`,
`partial-hydration`
)
// require.resolve might fail the build if the package is not installed
// Instead of failing it'll be ignored
try {
// TODO collect javascript aliases to match the partial hydration bundle
config.resolve.alias[`gatsby-plugin-image`] = require.resolve(
`gatsby-plugin-image/dist/gatsby-image.browser.modern`
)
} catch (e) {
// do nothing
}
return doBuildPartialHydrationRenderer(program.directory, config, stage)
}
// TODO remove after v4 release and update cloud internals
export const deleteRenderer = async (rendererPath: string): Promise<void> => {
try {
await fs.remove(rendererPath)
await fs.remove(`${rendererPath}.map`)
} catch (e) {
// This function will fail on Windows with no further consequences.
}
}
export interface IRenderHtmlResult {
unsafeBuiltinsUsageByPagePath: Record<string, Array<string>>
previewErrors: Record<string, any>
slicesPropsPerPage: Record<
string,
Record<
string,
{
props: Record<string, unknown>
sliceName: string
hasChildren: boolean
}
>
>
}
const renderHTMLQueue = async (
workerPool: GatsbyWorkerPool,
activity: IActivity,
htmlComponentRendererPath: string,
pages: Array<string>,
stage: Stage = Stage.BuildHTML
): Promise<void> => {
// We need to only pass env vars that are set programmatically in gatsby-cli
// to child process. Other vars will be picked up from environment.
const envVars: Array<[string, string | undefined]> = [
[`NODE_ENV`, process.env.NODE_ENV],
[`gatsby_executing_command`, process.env.gatsby_executing_command],
[`gatsby_log_level`, process.env.gatsby_log_level],
]
const segments = chunk(pages, 50)
const sessionId = Date.now()
const { webpackCompilationHash } = store.getState()
const renderHTML =
stage === `build-html`
? workerPool.single.renderHTMLProd
: workerPool.single.renderHTMLDev
const uniqueUnsafeBuiltinUsedStacks = new Set<string>()
try {
await Bluebird.map(segments, async pageSegment => {
const renderHTMLResult = await renderHTML({
envVars,
htmlComponentRendererPath,
paths: pageSegment,
sessionId,
webpackCompilationHash,
})
if (isPreview) {
const htmlRenderMeta = renderHTMLResult as IRenderHtmlResult
const seenErrors = new Set()
const errorMessages = new Map()
await Promise.all(
Object.entries(htmlRenderMeta.previewErrors).map(
async ([pagePath, error]) => {
if (!seenErrors.has(error.stack)) {
errorMessages.set(error.stack, {
pagePaths: [pagePath],
})
seenErrors.add(error.stack)
const prettyError = createErrorFromString(
error.stack,
`${htmlComponentRendererPath}.map`
)
const errorMessageStr = `${prettyError.stack}${
prettyError.codeFrame ? `\n\n${prettyError.codeFrame}\n` : ``
}`
const errorMessage = errorMessages.get(error.stack)
errorMessage.errorMessage = errorMessageStr
errorMessages.set(error.stack, errorMessage)
} else {
const errorMessage = errorMessages.get(error.stack)
errorMessage.pagePaths.push(pagePath)
errorMessages.set(error.stack, errorMessage)
}
}
)
)
for (const value of errorMessages.values()) {
const errorMessage = `The following page(s) saw this error when building their HTML:\n\n${value.pagePaths
.map(p => `- ${p}`)
.join(`\n`)}\n\n${value.errorMessage}`
reporter.error({
id: `95314`,
context: { errorMessage },
})
}
}
if (stage === `build-html`) {
const htmlRenderMeta = renderHTMLResult as IRenderHtmlResult
store.dispatch({
type: `HTML_GENERATED`,
payload: pageSegment,
})
if (_CFLAGS_.GATSBY_MAJOR === `5` && process.env.GATSBY_SLICES) {
store.dispatch({
type: `SET_SLICES_PROPS`,
payload: htmlRenderMeta.slicesPropsPerPage,
})
}
for (const [_pagePath, arrayOfUsages] of Object.entries(
htmlRenderMeta.unsafeBuiltinsUsageByPagePath
)) {
for (const unsafeUsageStack of arrayOfUsages) {
uniqueUnsafeBuiltinUsedStacks.add(unsafeUsageStack)
}
}
}
if (activity && activity.tick) {
activity.tick(pageSegment.length)
}
})
} catch (e) {
if (e?.context?.unsafeBuiltinsUsageByPagePath) {
for (const [_pagePath, arrayOfUsages] of Object.entries(
e.context.unsafeBuiltinsUsageByPagePath
)) {
// @ts-ignore TS doesn't know arrayOfUsages is Iterable
for (const unsafeUsageStack of arrayOfUsages) {
uniqueUnsafeBuiltinUsedStacks.add(unsafeUsageStack)
}
}
}
throw e
} finally {
if (uniqueUnsafeBuiltinUsedStacks.size > 0) {
console.warn(
`Unsafe builtin method was used, future builds will need to rebuild all pages`
)
store.dispatch({
type: `SSR_USED_UNSAFE_BUILTIN`,
})
}
for (const unsafeBuiltinUsedStack of uniqueUnsafeBuiltinUsedStacks) {
const prettyError = createErrorFromString(
unsafeBuiltinUsedStack,
`${htmlComponentRendererPath}.map`
)
const warningMessage = `${prettyError.stack}${
prettyError.codeFrame ? `\n\n${prettyError.codeFrame}\n` : ``
}`
reporter.warn(warningMessage)
}
}
}
const renderPartialHydrationQueue = async (
workerPool: GatsbyWorkerPool,
activity: IActivity,
pages: Array<string>,
program: IProgram
): Promise<void> => {
// We need to only pass env vars that are set programmatically in gatsby-cli
// to child process. Other vars will be picked up from environment.
const envVars: Array<[string, string | undefined]> = [
[`NODE_ENV`, process.env.NODE_ENV],
[`gatsby_executing_command`, process.env.gatsby_executing_command],
[`gatsby_log_level`, process.env.gatsby_log_level],
]
const segments = chunk(pages, 50)
const sessionId = Date.now()
const { config } = store.getState()
const { assetPrefix, pathPrefix } = config
// Let the error bubble up
await Promise.all(
segments.map(async pageSegment => {
await workerPool.single.renderPartialHydrationProd({
envVars,
paths: pageSegment,
sessionId,
pathPrefix: getPublicPath({ assetPrefix, pathPrefix, ...program }),
})
if (activity && activity.tick) {
activity.tick(pageSegment.length)
}
})
)
}
class BuildHTMLError extends Error {
codeFrame = ``
context?: {
path: string
}
constructor(error: Error) {
super(error.message)
// We must use getOwnProperty because keys like `stack` are not enumerable,
// but we want to copy over the entire error
Object.getOwnPropertyNames(error).forEach(key => {
this[key] = error[key]
})
}
}
export const doBuildPages = async (
rendererPath: string,
pagePaths: Array<string>,
activity: IActivity,
workerPool: GatsbyWorkerPool,
stage: Stage
): Promise<void> => {
try {
await renderHTMLQueue(workerPool, activity, rendererPath, pagePaths, stage)
} catch (error) {
const prettyError = createErrorFromString(error, `${rendererPath}.map`)
const buildError = new BuildHTMLError(prettyError)
buildError.context = error.context
if (error?.context?.path) {
const pageData = await getPageData(error.context.path)
const modifiedPageDataForErrorMessage =
modifyPageDataForErrorMessage(pageData)
const errorMessage = `Truncated page data information for the failed page "${
error.context.path
}": ${JSON.stringify(modifiedPageDataForErrorMessage, null, 2)}`
// This is our only error during preview so customize it a bit + add the
// pretty build error.
if (isPreview) {
reporter.error({
id: `95314`,
context: { errorMessage },
error: buildError,
})
} else {
reporter.error(errorMessage)
}
}
// Don't crash the builder when we're in preview-mode.
if (!isPreview) {
throw buildError
}
}
}
// TODO remove in v4 - this could be a "public" api
export const buildHTML = async ({
program,
stage,
pagePaths,
activity,
workerPool,
}: {
program: IProgram
stage: Stage
pagePaths: Array<string>
activity: IActivity
workerPool: GatsbyWorkerPool
}): Promise<void> => {
const rendererPath = `${program.directory}/${ROUTES_DIRECTORY}render-page.js`
await doBuildPages(rendererPath, pagePaths, activity, workerPool, stage)
if (
(process.env.GATSBY_PARTIAL_HYDRATION === `true` ||
process.env.GATSBY_PARTIAL_HYDRATION === `1`) &&
_CFLAGS_.GATSBY_MAJOR === `5`
) {
await renderPartialHydrationQueue(workerPool, activity, pagePaths, program)
}
}
export async function buildHTMLPagesAndDeleteStaleArtifacts({
workerPool,
parentSpan,
program,
}: {
workerPool: GatsbyWorkerPool
parentSpan?: Span
program: IBuildArgs
}): Promise<{
toRegenerate: Array<string>
toDelete: Array<string>
}> {
const rendererPath = `${program.directory}/${ROUTES_DIRECTORY}render-page.js`
buildUtils.markHtmlDirtyIfResultOfUsedStaticQueryChanged()
const { toRegenerate, toDelete, toCleanupFromTrackedState } =
buildUtils.calcDirtyHtmlFiles(store.getState())
store.dispatch({
type: `HTML_TRACKED_PAGES_CLEANUP`,
payload: toCleanupFromTrackedState,
})
if (toRegenerate.length > 0) {
const buildHTMLActivityProgress = reporter.createProgress(
`Building static HTML for pages`,
toRegenerate.length,
0,
{
parentSpan,
}
)
buildHTMLActivityProgress.start()
try {
await doBuildPages(
rendererPath,
toRegenerate,
buildHTMLActivityProgress,
workerPool,
Stage.BuildHTML
)
} catch (err) {
let id = `95313`
const context = {
errorPath: err.context && err.context.path,
undefinedGlobal: ``,
}
const undefinedGlobal = extractUndefinedGlobal(err)
if (undefinedGlobal) {
id = `95312`
context.undefinedGlobal = undefinedGlobal
}
buildHTMLActivityProgress.panic({
id,
context,
error: err,
})
}
buildHTMLActivityProgress.end()
} else {
reporter.info(`There are no new or changed html files to build.`)
}
if (
(process.env.GATSBY_PARTIAL_HYDRATION === `true` ||
process.env.GATSBY_PARTIAL_HYDRATION === `1`) &&
_CFLAGS_.GATSBY_MAJOR === `5`
) {
if (toRegenerate.length > 0) {
const buildHTMLActivityProgress = reporter.createProgress(
`Building partial HTML for pages`,
toRegenerate.length,
0,
{
parentSpan,
}
)
try {
buildHTMLActivityProgress.start()
await renderPartialHydrationQueue(
workerPool,
buildHTMLActivityProgress,
toRegenerate,
program
)
} catch (error) {
// Generic error with page path and useful stack trace, accurate code frame can be a future improvement
buildHTMLActivityProgress.panic({
id: `80000`,
context: error.context,
error,
})
} finally {
buildHTMLActivityProgress.end()
}
}
}
if (_CFLAGS_.GATSBY_MAJOR !== `5` && !program.keepPageRenderer) {
try {
await deleteRenderer(rendererPath)
} catch (err) {
// pass through
}
}
if (_CFLAGS_.GATSBY_MAJOR === `5` && process.env.GATSBY_SLICES) {
await buildSlices({
program,
workerPool,
parentSpan,
})
await stitchSlicesIntoPagesHTML({
publicDir: path.join(program.directory, `public`),
parentSpan,
})
}
if (toDelete.length > 0) {
const publicDir = path.join(program.directory, `public`)
const deletePageDataActivityTimer = reporter.activityTimer(
`Delete previous page data`
)
deletePageDataActivityTimer.start()
await buildUtils.removePageFiles(publicDir, toDelete)
deletePageDataActivityTimer.end()
}
return { toRegenerate, toDelete }
}
export async function buildSlices({
program,
workerPool,
parentSpan,
}: {
workerPool: GatsbyWorkerPool
parentSpan?: Span
program: IBuildArgs
}): Promise<void> {
const state = store.getState()
// for now we always render everything, everytime
const slicesProps: Array<ISlicePropsEntry> = []
for (const [
sliceId,
{ props, sliceName, hasChildren, pages, dirty },
] of state.html.slicesProps.bySliceId.entries()) {
if (dirty && pages.size > 0) {
slicesProps.push({
sliceId,
props,
sliceName,
hasChildren,
})
}
}
if (slicesProps.length > 0) {
const buildHTMLActivityProgress = reporter.activityTimer(
`Building slices HTML (${slicesProps.length})`,
{
parentSpan,
}
)
buildHTMLActivityProgress.start()
const htmlComponentRendererPath = `${program.directory}/${ROUTES_DIRECTORY}render-page.js`
try {
const slices = Array.from(state.slices.entries())
const staticQueriesBySliceTemplate = {}
for (const slice of state.slices.values()) {
staticQueriesBySliceTemplate[slice.componentPath] =
state.staticQueriesByTemplate.get(slice.componentPath)
}
await workerPool.single.renderSlices({
publicDir: path.join(program.directory, `public`),
htmlComponentRendererPath,
slices,
slicesProps,
staticQueriesBySliceTemplate,
})
} catch (err) {
const prettyError = createErrorFromString(
err.stack,
`${htmlComponentRendererPath}.map`
)
const undefinedGlobal = extractUndefinedGlobal(err)
let id = `11339`
if (undefinedGlobal) {
id = `11340`
err.context.undefinedGlobal = undefinedGlobal
}
buildHTMLActivityProgress.panic({
id,
context: err.context,
error: prettyError,
})
} finally {
buildHTMLActivityProgress.end()
}
// for now separate action for cleaning dirty flag and removing stale entries
store.dispatch({
type: `SLICES_PROPS_RENDERED`,
payload: slicesProps,
})
} else {
reporter.info(`There are no new or changed slice html files to build.`)
}
store.dispatch({
type: `SLICES_PROPS_REMOVE_STALE`,
})
}
export async function stitchSlicesIntoPagesHTML({
publicDir,
parentSpan,
}: {
publicDir: string
parentSpan?: Span
}): Promise<void> {
const stitchSlicesActivity = reporter.activityTimer(`stitching slices`, {
parentSpan,
})
stitchSlicesActivity.start()
try {
const {
html: { pagesThatNeedToStitchSlices },
pages,
} = store.getState()
const stitchQueue = fastq<void, string, void>(async (pagePath, cb) => {
await stitchSliceForAPage({ pagePath, publicDir })
cb(null)
}, 25)
for (const pagePath of pagesThatNeedToStitchSlices) {
const page = pages.get(pagePath)
if (!page) {
continue
}
if (getPageMode(page) === `SSG`) {
stitchQueue.push(pagePath)
}
}
if (!stitchQueue.idle()) {
await new Promise(resolve => {
stitchQueue.drain = resolve as () => unknown
})
}
store.dispatch({ type: `SLICES_STITCHED` })
} finally {
stitchSlicesActivity.end()
}
}