-
Notifications
You must be signed in to change notification settings - Fork 27.4k
/
Copy pathhot-reloader.ts
1251 lines (1126 loc) · 40 KB
/
hot-reloader.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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { webpack, StringXor } from 'next/dist/compiled/webpack/webpack'
import type { NextConfigComplete } from '../config-shared'
import type { CustomRoutes } from '../../lib/load-custom-routes'
import { getOverlayMiddleware } from 'next/dist/compiled/@next/react-dev-overlay/dist/middleware'
import { IncomingMessage, ServerResponse } from 'http'
import { WebpackHotMiddleware } from './hot-middleware'
import { join, relative, isAbsolute, posix } from 'path'
import { UrlObject } from 'url'
import {
createEntrypoints,
createPagesMapping,
finalizeEntrypoint,
getClientEntry,
getEdgeServerEntry,
getAppEntry,
runDependingOnPageType,
} from '../../build/entries'
import { watchCompilers } from '../../build/output'
import * as Log from '../../build/output/log'
import getBaseWebpackConfig, {
loadProjectInfo,
} from '../../build/webpack-config'
import { APP_DIR_ALIAS, WEBPACK_LAYERS } from '../../lib/constants'
import { recursiveDelete } from '../../lib/recursive-delete'
import {
BLOCKED_PAGES,
COMPILER_NAMES,
RSC_MODULE_TYPES,
} from '../../shared/lib/constants'
import { __ApiPreviewProps } from '../api-utils'
import { getPathMatch } from '../../shared/lib/router/utils/path-match'
import { findPageFile } from '../lib/find-page-file'
import {
BUILDING,
getEntries,
EntryTypes,
getInvalidator,
onDemandEntryHandler,
} from './on-demand-entry-handler'
import { denormalizePagePath } from '../../shared/lib/page-path/denormalize-page-path'
import { normalizePathSep } from '../../shared/lib/page-path/normalize-path-sep'
import getRouteFromEntrypoint from '../get-route-from-entrypoint'
import { fileExists } from '../../lib/file-exists'
import { difference, isMiddlewareFilename } from '../../build/utils'
import { DecodeError } from '../../shared/lib/utils'
import { Span, trace } from '../../trace'
import { getProperError } from '../../lib/is-error'
import ws from 'next/dist/compiled/ws'
import { promises as fs } from 'fs'
import { getPageStaticInfo } from '../../build/analysis/get-page-static-info'
import { UnwrapPromise } from '../../lib/coalesced-function'
import { getRegistry } from '../../lib/helpers/get-registry'
import { RouteMatch } from '../future/route-matches/route-match'
import type { Telemetry } from '../../telemetry/storage'
import { parseVersionInfo, VersionInfo } from './parse-version-info'
function diff(a: Set<any>, b: Set<any>) {
return new Set([...a].filter((v) => !b.has(v)))
}
const wsServer = new ws.Server({ noServer: true })
export async function renderScriptError(
res: ServerResponse,
error: Error,
{ verbose = true } = {}
): Promise<{ finished: true | undefined }> {
// Asks CDNs and others to not to cache the errored page
res.setHeader(
'Cache-Control',
'no-cache, no-store, max-age=0, must-revalidate'
)
if ((error as any).code === 'ENOENT') {
return { finished: undefined }
}
if (verbose) {
console.error(error.stack)
}
res.statusCode = 500
res.end('500 - Internal Error')
return { finished: true }
}
function addCorsSupport(req: IncomingMessage, res: ServerResponse) {
// Only rewrite CORS handling when URL matches a hot-reloader middleware
if (!req.url!.startsWith('/__next')) {
return { preflight: false }
}
if (!req.headers.origin) {
return { preflight: false }
}
res.setHeader('Access-Control-Allow-Origin', req.headers.origin)
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET')
// Based on https://github.com/primus/access-control/blob/4cf1bc0e54b086c91e6aa44fb14966fa5ef7549c/index.js#L158
if (req.headers['access-control-request-headers']) {
res.setHeader(
'Access-Control-Allow-Headers',
req.headers['access-control-request-headers'] as string
)
}
if (req.method === 'OPTIONS') {
res.writeHead(200)
res.end()
return { preflight: true }
}
return { preflight: false }
}
const matchNextPageBundleRequest = getPathMatch(
'/_next/static/chunks/pages/:path*.js(\\.map|)'
)
// Iteratively look up the issuer till it ends up at the root
function findEntryModule(
module: webpack.Module,
compilation: webpack.Compilation
): any {
for (;;) {
const issuer = compilation.moduleGraph.getIssuer(module)
if (!issuer) return module
module = issuer
}
}
function erroredPages(compilation: webpack.Compilation) {
const failedPages: { [page: string]: any[] } = {}
for (const error of compilation.errors) {
if (!error.module) {
continue
}
const entryModule = findEntryModule(error.module, compilation)
const { name } = entryModule
if (!name) {
continue
}
// Only pages have to be reloaded
const enhancedName = getRouteFromEntrypoint(name)
if (!enhancedName) {
continue
}
if (!failedPages[enhancedName]) {
failedPages[enhancedName] = []
}
failedPages[enhancedName].push(error)
}
return failedPages
}
export default class HotReloader {
private dir: string
private buildId: string
private interceptors: any[]
private pagesDir?: string
private distDir: string
private webpackHotMiddleware?: WebpackHotMiddleware
private config: NextConfigComplete
public hasServerComponents: boolean
public clientStats: webpack.Stats | null
public serverStats: webpack.Stats | null
public edgeServerStats: webpack.Stats | null
private clientError: Error | null = null
private serverError: Error | null = null
private serverPrevDocumentHash: string | null
private prevChunkNames?: Set<any>
private onDemandEntries?: ReturnType<typeof onDemandEntryHandler>
private previewProps: __ApiPreviewProps
private watcher: any
private rewrites: CustomRoutes['rewrites']
private fallbackWatcher: any
private hotReloaderSpan: Span
private pagesMapping: { [key: string]: string } = {}
private appDir?: string
private telemetry: Telemetry
private versionInfo: VersionInfo = {
staleness: 'unknown',
installed: '0.0.0',
}
public multiCompiler?: webpack.MultiCompiler
public activeConfigs?: Array<
UnwrapPromise<ReturnType<typeof getBaseWebpackConfig>>
>
constructor(
dir: string,
{
config,
pagesDir,
distDir,
buildId,
previewProps,
rewrites,
appDir,
telemetry,
}: {
config: NextConfigComplete
pagesDir?: string
distDir: string
buildId: string
previewProps: __ApiPreviewProps
rewrites: CustomRoutes['rewrites']
appDir?: string
telemetry: Telemetry
}
) {
this.buildId = buildId
this.dir = dir
this.interceptors = []
this.pagesDir = pagesDir
this.appDir = appDir
this.distDir = distDir
this.clientStats = null
this.serverStats = null
this.edgeServerStats = null
this.serverPrevDocumentHash = null
this.telemetry = telemetry
this.config = config
this.hasServerComponents = !!this.appDir
this.previewProps = previewProps
this.rewrites = rewrites
this.hotReloaderSpan = trace('hot-reloader', undefined, {
version: process.env.__NEXT_VERSION as string,
})
// Ensure the hotReloaderSpan is flushed immediately as it's the parentSpan for all processing
// of the current `next dev` invocation.
this.hotReloaderSpan.stop()
}
public async run(
req: IncomingMessage,
res: ServerResponse,
parsedUrl: UrlObject
): Promise<{ finished?: true }> {
// Usually CORS support is not needed for the hot-reloader (this is dev only feature)
// With when the app runs for multi-zones support behind a proxy,
// the current page is trying to access this URL via assetPrefix.
// That's when the CORS support is needed.
const { preflight } = addCorsSupport(req, res)
if (preflight) {
return {}
}
// When a request comes in that is a page bundle, e.g. /_next/static/<buildid>/pages/index.js
// we have to compile the page using on-demand-entries, this middleware will handle doing that
// by adding the page to on-demand-entries, waiting till it's done
// and then the bundle will be served like usual by the actual route in server/index.js
const handlePageBundleRequest = async (
pageBundleRes: ServerResponse,
parsedPageBundleUrl: UrlObject
): Promise<{ finished?: true }> => {
const { pathname } = parsedPageBundleUrl
const params = matchNextPageBundleRequest<{ path: string[] }>(pathname)
if (!params) {
return {}
}
let decodedPagePath: string
try {
decodedPagePath = `/${params.path
.map((param) => decodeURIComponent(param))
.join('/')}`
} catch (_) {
throw new DecodeError('failed to decode param')
}
const page = denormalizePagePath(decodedPagePath)
if (page === '/_error' || BLOCKED_PAGES.indexOf(page) === -1) {
try {
await this.ensurePage({ page, clientOnly: true })
} catch (error) {
return await renderScriptError(pageBundleRes, getProperError(error))
}
const errors = await this.getCompilationErrors(page)
if (errors.length > 0) {
return await renderScriptError(pageBundleRes, errors[0], {
verbose: false,
})
}
}
return {}
}
const { finished } = await handlePageBundleRequest(res, parsedUrl)
for (const fn of this.interceptors) {
await new Promise<void>((resolve, reject) => {
fn(req, res, (err: Error) => {
if (err) return reject(err)
resolve()
})
})
}
return { finished }
}
public onHMR(req: IncomingMessage, _res: ServerResponse, head: Buffer) {
wsServer.handleUpgrade(req, req.socket, head, (client) => {
this.webpackHotMiddleware?.onHMR(client)
this.onDemandEntries?.onHMR(client)
client.addEventListener('message', ({ data }) => {
data = typeof data !== 'string' ? data.toString() : data
try {
const payload = JSON.parse(data)
let traceChild:
| {
name: string
startTime?: bigint
endTime?: bigint
attrs?: Record<string, number | string>
}
| undefined
switch (payload.event) {
case 'client-hmr-latency': {
traceChild = {
name: payload.event,
startTime: BigInt(payload.startTime) * BigInt(1000 * 1000),
endTime: BigInt(payload.endTime) * BigInt(1000 * 1000),
}
break
}
case 'client-reload-page':
case 'client-success': {
traceChild = {
name: payload.event,
}
break
}
case 'client-error': {
traceChild = {
name: payload.event,
attrs: { errorCount: payload.errorCount },
}
break
}
case 'client-warning': {
traceChild = {
name: payload.event,
attrs: { warningCount: payload.warningCount },
}
break
}
case 'client-removed-page':
case 'client-added-page': {
traceChild = {
name: payload.event,
attrs: { page: payload.page || '' },
}
break
}
case 'client-full-reload': {
const { event, stackTrace, hadRuntimeError } = payload
traceChild = {
name: event,
attrs: { stackTrace: stackTrace ?? '' },
}
if (hadRuntimeError) {
Log.warn(
`Fast Refresh had to perform a full reload due to a runtime error.`
)
break
}
let fileMessage = ''
if (stackTrace) {
const file = /Aborted because (.+) is not accepted/.exec(
stackTrace
)?.[1]
if (file) {
fileMessage = ` when ${file} changed`
}
}
Log.warn(
`Fast Refresh had to perform a full reload${fileMessage}. Read more: https://nextjs.org/docs/messages/fast-refresh-reload`
)
break
}
default: {
break
}
}
if (traceChild) {
this.hotReloaderSpan.manualTraceChild(
traceChild.name,
traceChild.startTime || process.hrtime.bigint(),
traceChild.endTime || process.hrtime.bigint(),
{ ...traceChild.attrs, clientId: payload.id }
)
}
} catch (_) {
// invalid WebSocket message
}
})
})
}
private async clean(span: Span): Promise<void> {
return span
.traceChild('clean')
.traceAsyncFn(() =>
recursiveDelete(join(this.dir, this.config.distDir), /^cache/)
)
}
private async getVersionInfo(span: Span, enabled: boolean) {
const versionInfoSpan = span.traceChild('get-version-info')
return versionInfoSpan.traceAsyncFn<VersionInfo>(async () => {
let installed = '0.0.0'
if (!enabled) {
return { installed, staleness: 'unknown' }
}
try {
installed = require('next/package.json').version
const registry = getRegistry()
const res = await fetch(`${registry}-/package/next/dist-tags`)
if (!res.ok) return { installed, staleness: 'unknown' }
const tags = await res.json()
return parseVersionInfo({
installed,
latest: tags.latest,
canary: tags.canary,
})
} catch {
return { installed, staleness: 'unknown' }
}
})
}
private async getWebpackConfig(span: Span) {
const webpackConfigSpan = span.traceChild('get-webpack-config')
const pageExtensions = this.config.pageExtensions
return webpackConfigSpan.traceAsyncFn(async () => {
const pagePaths = !this.pagesDir
? ([] as (string | null)[])
: await webpackConfigSpan
.traceChild('get-page-paths')
.traceAsyncFn(() =>
Promise.all([
findPageFile(this.pagesDir!, '/_app', pageExtensions, false),
findPageFile(
this.pagesDir!,
'/_document',
pageExtensions,
false
),
])
)
this.pagesMapping = webpackConfigSpan
.traceChild('create-pages-mapping')
.traceFn(() =>
createPagesMapping({
isDev: true,
pageExtensions: this.config.pageExtensions,
pagesType: 'pages',
pagePaths: pagePaths.filter(
(i: string | null): i is string => typeof i === 'string'
),
pagesDir: this.pagesDir,
})
)
const entrypoints = await webpackConfigSpan
.traceChild('create-entrypoints')
.traceAsyncFn(() =>
createEntrypoints({
appDir: this.appDir,
buildId: this.buildId,
config: this.config,
envFiles: [],
isDev: true,
pages: this.pagesMapping,
pagesDir: this.pagesDir,
previewMode: this.previewProps,
rootDir: this.dir,
pageExtensions: this.config.pageExtensions,
})
)
const commonWebpackOptions = {
dev: true,
buildId: this.buildId,
config: this.config,
pagesDir: this.pagesDir,
rewrites: this.rewrites,
originalRewrites: this.config._originalRewrites,
originalRedirects: this.config._originalRedirects,
runWebpackSpan: this.hotReloaderSpan,
appDir: this.appDir,
}
return webpackConfigSpan
.traceChild('generate-webpack-config')
.traceAsyncFn(async () => {
const info = await loadProjectInfo({
dir: this.dir,
config: commonWebpackOptions.config,
dev: true,
})
return Promise.all([
// order is important here
getBaseWebpackConfig(this.dir, {
...commonWebpackOptions,
compilerType: COMPILER_NAMES.client,
entrypoints: entrypoints.client,
...info,
}),
getBaseWebpackConfig(this.dir, {
...commonWebpackOptions,
compilerType: COMPILER_NAMES.server,
entrypoints: entrypoints.server,
...info,
}),
getBaseWebpackConfig(this.dir, {
...commonWebpackOptions,
compilerType: COMPILER_NAMES.edgeServer,
entrypoints: entrypoints.edgeServer,
...info,
}),
])
})
})
}
public async buildFallbackError(): Promise<void> {
if (this.fallbackWatcher) return
const info = await loadProjectInfo({
dir: this.dir,
config: this.config,
dev: true,
})
const fallbackConfig = await getBaseWebpackConfig(this.dir, {
runWebpackSpan: this.hotReloaderSpan,
dev: true,
compilerType: COMPILER_NAMES.client,
config: this.config,
buildId: this.buildId,
pagesDir: this.pagesDir,
rewrites: {
beforeFiles: [],
afterFiles: [],
fallback: [],
},
originalRewrites: {
beforeFiles: [],
afterFiles: [],
fallback: [],
},
originalRedirects: [],
isDevFallback: true,
entrypoints: (
await createEntrypoints({
appDir: this.appDir,
buildId: this.buildId,
config: this.config,
envFiles: [],
isDev: true,
pages: {
'/_app': 'next/dist/pages/_app',
'/_error': 'next/dist/pages/_error',
},
pagesDir: this.pagesDir,
previewMode: this.previewProps,
rootDir: this.dir,
pageExtensions: this.config.pageExtensions,
})
).client,
...info,
})
const fallbackCompiler = webpack(fallbackConfig)
this.fallbackWatcher = await new Promise((resolve) => {
let bootedFallbackCompiler = false
fallbackCompiler.watch(
// @ts-ignore webpack supports an array of watchOptions when using a multiCompiler
fallbackConfig.watchOptions,
// Errors are handled separately
(_err: any) => {
if (!bootedFallbackCompiler) {
bootedFallbackCompiler = true
resolve(true)
}
}
)
})
}
public async start(): Promise<void> {
const startSpan = this.hotReloaderSpan.traceChild('start')
startSpan.stop() // Stop immediately to create an artificial parent span
this.versionInfo = await this.getVersionInfo(
startSpan,
!!process.env.NEXT_TEST_MODE || this.telemetry.isEnabled
)
await this.clean(startSpan)
// Ensure distDir exists before writing package.json
await fs.mkdir(this.distDir, { recursive: true })
const distPackageJsonPath = join(this.distDir, 'package.json')
// Ensure commonjs handling is used for files in the distDir (generally .next)
// Files outside of the distDir can be "type": "module"
await fs.writeFile(distPackageJsonPath, '{"type": "commonjs"}')
this.activeConfigs = await this.getWebpackConfig(startSpan)
for (const config of this.activeConfigs) {
const defaultEntry = config.entry
config.entry = async (...args) => {
const outputPath = this.multiCompiler?.outputPath || ''
const entries: ReturnType<typeof getEntries> = getEntries(outputPath)
// @ts-ignore entry is always a function
const entrypoints = await defaultEntry(...args)
const isClientCompilation = config.name === COMPILER_NAMES.client
const isNodeServerCompilation = config.name === COMPILER_NAMES.server
const isEdgeServerCompilation =
config.name === COMPILER_NAMES.edgeServer
await Promise.all(
Object.keys(entries).map(async (entryKey) => {
const entryData = entries[entryKey]
const { bundlePath, dispose } = entryData
const result = /^(client|server|edge-server)(.*)/g.exec(entryKey)
const [, key, page] = result! // this match should always happen
if (key === COMPILER_NAMES.client && !isClientCompilation) return
if (key === COMPILER_NAMES.server && !isNodeServerCompilation)
return
if (key === COMPILER_NAMES.edgeServer && !isEdgeServerCompilation)
return
const isEntry = entryData.type === EntryTypes.ENTRY
const isChildEntry = entryData.type === EntryTypes.CHILD_ENTRY
// Check if the page was removed or disposed and remove it
if (isEntry) {
const pageExists =
!dispose && (await fileExists(entryData.absolutePagePath))
if (!pageExists) {
delete entries[entryKey]
return
}
}
// For child entries, if it has an entry file and it's gone, remove it
if (isChildEntry) {
if (entryData.absoluteEntryFilePath) {
const pageExists =
!dispose &&
(await fileExists(entryData.absoluteEntryFilePath))
if (!pageExists) {
delete entries[entryKey]
return
}
}
}
const hasAppDir = !!this.appDir
const isAppPath = hasAppDir && bundlePath.startsWith('app/')
const staticInfo = isEntry
? await getPageStaticInfo({
pageFilePath: entryData.absolutePagePath,
nextConfig: this.config,
isDev: true,
pageType: isAppPath ? 'app' : 'pages',
})
: {}
const isServerComponent =
isAppPath && staticInfo.rsc !== RSC_MODULE_TYPES.client
const pageType = entryData.bundlePath.startsWith('pages/')
? 'pages'
: entryData.bundlePath.startsWith('app/')
? 'app'
: 'root'
await runDependingOnPageType({
page,
pageRuntime: staticInfo.runtime,
pageType,
onEdgeServer: () => {
// TODO-APP: verify if child entry should support.
if (!isEdgeServerCompilation || !isEntry) return
const appDirLoader = isAppPath
? getAppEntry({
name: bundlePath,
appPaths: entryData.appPaths,
pagePath: posix.join(
APP_DIR_ALIAS,
relative(
this.appDir!,
entryData.absolutePagePath
).replace(/\\/g, '/')
),
appDir: this.appDir!,
pageExtensions: this.config.pageExtensions,
rootDir: this.dir,
isDev: true,
tsconfigPath: this.config.typescript.tsconfigPath,
assetPrefix: this.config.assetPrefix,
}).import
: undefined
entries[entryKey].status = BUILDING
entrypoints[bundlePath] = finalizeEntrypoint({
compilerType: COMPILER_NAMES.edgeServer,
name: bundlePath,
value: getEdgeServerEntry({
absolutePagePath: entryData.absolutePagePath,
rootDir: this.dir,
buildId: this.buildId,
bundlePath,
config: this.config,
isDev: true,
page,
pages: this.pagesMapping,
isServerComponent,
appDirLoader,
pagesType: isAppPath ? 'app' : 'pages',
}),
hasAppDir,
})
},
onClient: () => {
if (!isClientCompilation) return
if (isChildEntry) {
entries[entryKey].status = BUILDING
entrypoints[bundlePath] = finalizeEntrypoint({
name: bundlePath,
compilerType: COMPILER_NAMES.client,
value: entryData.request,
hasAppDir,
})
} else {
entries[entryKey].status = BUILDING
entrypoints[bundlePath] = finalizeEntrypoint({
name: bundlePath,
compilerType: COMPILER_NAMES.client,
value: getClientEntry({
absolutePagePath: entryData.absolutePagePath,
page,
}),
hasAppDir,
})
}
},
onServer: () => {
// TODO-APP: verify if child entry should support.
if (!isNodeServerCompilation || !isEntry) return
entries[entryKey].status = BUILDING
let relativeRequest = relative(
config.context!,
entryData.absolutePagePath
)
if (
!isAbsolute(relativeRequest) &&
!relativeRequest.startsWith('../')
) {
relativeRequest = `./${relativeRequest}`
}
entrypoints[bundlePath] = finalizeEntrypoint({
compilerType: COMPILER_NAMES.server,
name: bundlePath,
isServerComponent,
value: isAppPath
? getAppEntry({
name: bundlePath,
appPaths: entryData.appPaths,
pagePath: posix.join(
APP_DIR_ALIAS,
relative(
this.appDir!,
entryData.absolutePagePath
).replace(/\\/g, '/')
),
appDir: this.appDir!,
pageExtensions: this.config.pageExtensions,
rootDir: this.dir,
isDev: true,
tsconfigPath: this.config.typescript.tsconfigPath,
assetPrefix: this.config.assetPrefix,
})
: relativeRequest,
hasAppDir,
})
},
})
})
)
return entrypoints
}
}
// Enable building of client compilation before server compilation in development
// @ts-ignore webpack 5
this.activeConfigs.parallelism = 1
this.multiCompiler = webpack(
this.activeConfigs
) as unknown as webpack.MultiCompiler
watchCompilers(
this.multiCompiler.compilers[0],
this.multiCompiler.compilers[1],
this.multiCompiler.compilers[2]
)
// Watch for changes to client/server page files so we can tell when just
// the server file changes and trigger a reload for GS(S)P pages
const changedClientPages = new Set<string>()
const changedServerPages = new Set<string>()
const changedEdgeServerPages = new Set<string>()
const changedServerComponentPages = new Set<string>()
const changedCSSImportPages = new Set<string>()
const prevClientPageHashes = new Map<string, string>()
const prevServerPageHashes = new Map<string, string>()
const prevEdgeServerPageHashes = new Map<string, string>()
const prevCSSImportModuleHashes = new Map<string, string>()
const trackPageChanges =
(
pageHashMap: Map<string, string>,
changedItems: Set<string>,
serverComponentChangedItems?: Set<string>
) =>
(stats: webpack.Compilation) => {
try {
stats.entrypoints.forEach((entry, key) => {
if (
key.startsWith('pages/') ||
key.startsWith('app/') ||
isMiddlewareFilename(key)
) {
// TODO this doesn't handle on demand loaded chunks
entry.chunks.forEach((chunk) => {
if (chunk.id === key) {
const modsIterable: any =
stats.chunkGraph.getChunkModulesIterable(chunk)
let hasCSSModuleChanges = false
let chunksHash = new StringXor()
let chunksHashServerLayer = new StringXor()
modsIterable.forEach((mod: any) => {
if (
mod.resource &&
mod.resource.replace(/\\/g, '/').includes(key)
) {
// use original source to calculate hash since mod.hash
// includes the source map in development which changes
// every time for both server and client so we calculate
// the hash without the source map for the page module
const hash = require('crypto')
.createHash('sha256')
.update(mod.originalSource().buffer())
.digest()
.toString('hex')
if (
mod.layer === WEBPACK_LAYERS.server &&
mod?.buildInfo?.rsc?.type !== 'client'
) {
chunksHashServerLayer.add(hash)
}
chunksHash.add(hash)
} else {
// for non-pages we can use the module hash directly
const hash = stats.chunkGraph.getModuleHash(
mod,
chunk.runtime
)
if (
mod.layer === WEBPACK_LAYERS.server &&
mod?.buildInfo?.rsc?.type !== 'client'
) {
chunksHashServerLayer.add(hash)
}
chunksHash.add(hash)
// Both CSS import changes from server and client
// components are tracked.
if (
key.startsWith('app/') &&
mod.resource?.endsWith('.css')
) {
const prevHash = prevCSSImportModuleHashes.get(
mod.resource
)
if (prevHash && prevHash !== hash) {
hasCSSModuleChanges = true
}
prevCSSImportModuleHashes.set(mod.resource, hash)
}
}
})
const prevHash = pageHashMap.get(key)
const curHash = chunksHash.toString()
if (prevHash && prevHash !== curHash) {
changedItems.add(key)
}
pageHashMap.set(key, curHash)
if (serverComponentChangedItems) {
const serverKey = WEBPACK_LAYERS.server + ':' + key
const prevServerHash = pageHashMap.get(serverKey)
const curServerHash = chunksHashServerLayer.toString()
if (prevServerHash && prevServerHash !== curServerHash) {
serverComponentChangedItems.add(key)
}
pageHashMap.set(serverKey, curServerHash)
}
if (hasCSSModuleChanges) {
changedCSSImportPages.add(key)
}
}
})
}
})
} catch (err) {
console.error(err)
}
}
this.multiCompiler.compilers[0].hooks.emit.tap(
'NextjsHotReloaderForClient',
trackPageChanges(prevClientPageHashes, changedClientPages)
)
this.multiCompiler.compilers[1].hooks.emit.tap(
'NextjsHotReloaderForServer',
trackPageChanges(
prevServerPageHashes,
changedServerPages,
changedServerComponentPages
)
)
this.multiCompiler.compilers[2].hooks.emit.tap(
'NextjsHotReloaderForServer',
trackPageChanges(
prevEdgeServerPageHashes,
changedEdgeServerPages,
changedServerComponentPages
)
)
// This plugin watches for changes to _document.js and notifies the client side that it should reload the page
this.multiCompiler.compilers[1].hooks.failed.tap(
'NextjsHotReloaderForServer',
(err: Error) => {
this.serverError = err
this.serverStats = null
}
)
this.multiCompiler.compilers[2].hooks.done.tap(
'NextjsHotReloaderForServer',
(stats) => {
this.serverError = null
this.edgeServerStats = stats
}
)