24
24
* SOFTWARE.
25
25
*/
26
26
27
+ /// <reference types="webpack/module.d.ts" />
28
+
27
29
// This file is a modified version of the Create React App HMR dev client that
28
30
// can be found here:
29
31
// https://github.com/facebook/create-react-app/blob/v3.4.1/packages/react-dev-utils/webpackHotDevClient.js
@@ -55,10 +57,8 @@ import {
55
57
} from '../shared'
56
58
import { RuntimeErrorHandler } from '../../errors/runtime-error-handler'
57
59
import reportHmrLatency from '../utils/report-hmr-latency'
58
- import {
59
- extractModulesFromTurbopackMessage ,
60
- TurbopackHmr ,
61
- } from '../utils/turbopack-hot-reloader-common'
60
+ import { TurbopackHmr } from '../utils/turbopack-hot-reloader-common'
61
+
62
62
// This alternative WebpackDevServer combines the functionality of:
63
63
// https://github.com/webpack/webpack-dev-server/blob/webpack-1/client/index.js
64
64
// https://github.com/webpack/webpack/blob/webpack-1/hot/dev-server.js
@@ -149,7 +149,7 @@ function handleSuccess() {
149
149
150
150
// Attempt to apply hot updates or reload.
151
151
if ( isHotUpdate ) {
152
- tryApplyUpdates ( onBeforeFastRefresh , onFastRefresh )
152
+ tryApplyUpdatesWebpack ( )
153
153
}
154
154
}
155
155
@@ -189,7 +189,7 @@ function handleWarnings(warnings: any) {
189
189
190
190
// Attempt to apply hot updates or reload.
191
191
if ( isHotUpdate ) {
192
- tryApplyUpdates ( onBeforeFastRefresh , onFastRefresh )
192
+ tryApplyUpdatesWebpack ( )
193
193
}
194
194
}
195
195
@@ -233,30 +233,6 @@ const turbopackHmr: TurbopackHmr | null = process.env.TURBOPACK
233
233
: null
234
234
let isrManifest : Record < string , boolean > = { }
235
235
236
- function onBeforeFastRefresh ( updatedModules : string [ ] ) {
237
- if ( updatedModules . length > 0 ) {
238
- // Only trigger a pending state if we have updates to apply
239
- // (cf. onFastRefresh)
240
- onBeforeRefresh ( )
241
- }
242
- }
243
-
244
- function onFastRefresh ( updatedModules : ReadonlyArray < string > = [ ] ) {
245
- onBuildOk ( )
246
- if ( updatedModules . length === 0 ) {
247
- return
248
- }
249
-
250
- onRefresh ( )
251
-
252
- reportHmrLatency (
253
- sendMessage ,
254
- updatedModules ,
255
- webpackStartMsSinceEpoch ! ,
256
- Date . now ( )
257
- )
258
- }
259
-
260
236
// There is a newer version of the code available.
261
237
function handleAvailableHash ( hash : string ) {
262
238
// Update last known compilation hash.
@@ -282,7 +258,7 @@ export function handleStaticIndicator() {
282
258
}
283
259
}
284
260
285
- /** Handles messages from the sevrer for the Pages Router. */
261
+ /** Handles messages from the server for the Pages Router. */
286
262
function processMessage ( obj : HMR_ACTION_TYPES ) {
287
263
if ( ! ( 'action' in obj ) ) {
288
264
return
@@ -325,6 +301,7 @@ function processMessage(obj: HMR_ACTION_TYPES) {
325
301
return handleErrors ( errors )
326
302
}
327
303
304
+ // NOTE: Turbopack does not currently send warnings
328
305
const hasWarnings = Boolean ( warnings && warnings . length )
329
306
if ( hasWarnings ) {
330
307
sendMessage (
@@ -371,8 +348,7 @@ function processMessage(obj: HMR_ACTION_TYPES) {
371
348
break
372
349
}
373
350
case HMR_ACTIONS_SENT_TO_BROWSER . TURBOPACK_MESSAGE : {
374
- const updatedModules = extractModulesFromTurbopackMessage ( obj . data )
375
- onBeforeFastRefresh ( [ ...updatedModules ] )
351
+ onBeforeRefresh ( )
376
352
for ( const listener of turbopackMessageListeners ) {
377
353
listener ( {
378
354
type : HMR_ACTIONS_SENT_TO_BROWSER . TURBOPACK_MESSAGE ,
@@ -424,10 +400,7 @@ function afterApplyUpdates(fn: () => void) {
424
400
}
425
401
426
402
// Attempt to update code on the fly, fall back to a hard reload.
427
- function tryApplyUpdates (
428
- onBeforeHotUpdate : ( ( updatedModules : string [ ] ) => unknown ) | undefined ,
429
- onHotUpdateSuccess : ( updatedModules : string [ ] ) => unknown
430
- ) {
403
+ function tryApplyUpdatesWebpack ( ) {
431
404
if ( ! module . hot ) {
432
405
// HotModuleReplacementPlugin is not in Webpack configuration.
433
406
console . error ( 'HotModuleReplacementPlugin is not in Webpack configuration.' )
@@ -440,8 +413,11 @@ function tryApplyUpdates(
440
413
return
441
414
}
442
415
443
- function handleApplyUpdates ( err : any , updatedModules : string [ ] | null ) {
444
- if ( err || RuntimeErrorHandler . hadRuntimeError || ! updatedModules ) {
416
+ function handleApplyUpdates (
417
+ err : any ,
418
+ updatedModules : ( string | number ) [ ] | null
419
+ ) {
420
+ if ( err || RuntimeErrorHandler . hadRuntimeError || updatedModules == null ) {
445
421
if ( err ) {
446
422
console . warn ( REACT_REFRESH_FULL_RELOAD )
447
423
} else if ( RuntimeErrorHandler . hadRuntimeError ) {
@@ -451,46 +427,49 @@ function tryApplyUpdates(
451
427
return
452
428
}
453
429
454
- if ( typeof onHotUpdateSuccess === 'function' ) {
455
- // Maybe we want to do something.
456
- onHotUpdateSuccess ( updatedModules )
457
- }
430
+ onBuildOk ( )
458
431
459
432
if ( isUpdateAvailable ( ) ) {
460
433
// While we were updating, there was a new update! Do it again.
461
- // However, this time, don't trigger a pending refresh state.
462
- tryApplyUpdates (
463
- updatedModules . length > 0 ? undefined : onBeforeHotUpdate ,
464
- updatedModules . length > 0 ? onBuildOk : onHotUpdateSuccess
465
- )
466
- } else {
467
- onBuildOk ( )
468
- if ( process . env . __NEXT_TEST_MODE ) {
469
- afterApplyUpdates ( ( ) => {
470
- if ( self . __NEXT_HMR_CB ) {
471
- self . __NEXT_HMR_CB ( )
472
- self . __NEXT_HMR_CB = null
473
- }
474
- } )
475
- }
434
+ tryApplyUpdatesWebpack ( )
435
+ return
436
+ }
437
+
438
+ onRefresh ( )
439
+ reportHmrLatency (
440
+ sendMessage ,
441
+ updatedModules ,
442
+ webpackStartMsSinceEpoch ! ,
443
+ Date . now ( )
444
+ )
445
+
446
+ if ( process . env . __NEXT_TEST_MODE ) {
447
+ afterApplyUpdates ( ( ) => {
448
+ if ( self . __NEXT_HMR_CB ) {
449
+ self . __NEXT_HMR_CB ( )
450
+ self . __NEXT_HMR_CB = null
451
+ }
452
+ } )
476
453
}
477
454
}
478
455
479
456
// https://webpack.js.org/api/hot-module-replacement/#check
480
457
module . hot
481
458
. check ( /* autoApply */ false )
482
- . then ( ( updatedModules : any ) => {
483
- if ( ! updatedModules ) {
459
+ . then ( ( updatedModules : ( string | number ) [ ] | null ) => {
460
+ if ( updatedModules == null ) {
484
461
return null
485
462
}
486
463
487
- if ( typeof onBeforeHotUpdate === 'function' ) {
488
- onBeforeHotUpdate ( updatedModules )
489
- }
464
+ // We should always handle an update, even if updatedModules is empty (but
465
+ // non-null) for any reason. That's what webpack would normally do:
466
+ // https://github.com/webpack/webpack/blob/3aa6b6bc3a64/lib/hmr/HotModuleReplacement.runtime.js#L296-L298
467
+ onBeforeRefresh ( )
468
+ // https://webpack.js.org/api/hot-module-replacement/#apply
490
469
return module . hot . apply ( )
491
470
} )
492
471
. then (
493
- ( updatedModules : any ) => {
472
+ ( updatedModules : ( string | number ) [ ] | null ) => {
494
473
handleApplyUpdates ( null , updatedModules )
495
474
} ,
496
475
( err : any ) => {
0 commit comments