File size: 4,716 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { TurbopackMessageAction } from '../../../server/dev/hot-reloader-types'
import type { Update as TurbopackUpdate } from '../../../build/swc/types'

declare global {
  interface Window {
    __NEXT_HMR_TURBOPACK_REPORT_NOISY_NOOP_EVENTS: boolean | undefined
  }
}

// How long to wait before reporting the HMR start, used to suppress irrelevant
// `BUILDING` events. Does not impact reported latency.
const TURBOPACK_HMR_START_DELAY_MS = 100

interface HmrUpdate {
  hasUpdates: boolean
  updatedModules: Set<string>
  startMsSinceEpoch: number
  endMsSinceEpoch: number
}

export class TurbopackHmr {
  #updatedModules: Set<string>
  #startMsSinceEpoch: number | undefined
  #lastUpdateMsSinceEpoch: number | undefined
  #deferredReportHmrStartId: ReturnType<typeof setTimeout> | undefined

  constructor() {
    this.#updatedModules = new Set()
  }

  // HACK: Turbopack tends to generate a lot of irrelevant "BUILDING" actions,
  // as it reports *any* compilation, including fully no-op/cached compilations
  // and those unrelated to HMR. Fixing this would require significant
  // architectural changes.
  //
  // Work around this by deferring any "rebuilding" message by 100ms. If we get
  // a BUILT event within that threshold and nothing has changed, just suppress
  // the message entirely.
  #runDeferredReportHmrStart() {
    if (this.#deferredReportHmrStartId != null) {
      console.log('[Fast Refresh] rebuilding')
      this.#cancelDeferredReportHmrStart()
    }
  }

  #cancelDeferredReportHmrStart() {
    clearTimeout(this.#deferredReportHmrStartId)
    this.#deferredReportHmrStartId = undefined
  }

  onBuilding() {
    this.#lastUpdateMsSinceEpoch = undefined
    this.#cancelDeferredReportHmrStart()
    this.#startMsSinceEpoch = Date.now()

    // report the HMR start after a short delay
    this.#deferredReportHmrStartId = setTimeout(
      () => this.#runDeferredReportHmrStart(),
      // debugging feature: don't defer/suppress noisy no-op HMR update messages
      self.__NEXT_HMR_TURBOPACK_REPORT_NOISY_NOOP_EVENTS
        ? 0
        : TURBOPACK_HMR_START_DELAY_MS
    )
  }

  /** Helper for other `onEvent` methods. */
  #onUpdate() {
    this.#runDeferredReportHmrStart()
    this.#lastUpdateMsSinceEpoch = Date.now()
  }

  onTurbopackMessage(msg: TurbopackMessageAction) {
    this.#onUpdate()
    const updatedModules = extractModulesFromTurbopackMessage(msg.data)
    for (const module of updatedModules) {
      this.#updatedModules.add(module)
    }
  }

  onServerComponentChanges() {
    this.#onUpdate()
  }

  onReloadPage() {
    this.#onUpdate()
  }

  onPageAddRemove() {
    this.#onUpdate()
  }

  /**
   * @returns `null` if the caller should ignore the update entirely. Returns an
   *   object with `hasUpdates: false` if the caller should report the end of
   *   the HMR in the browser console, but the HMR was a no-op.
   */
  onBuilt(): HmrUpdate | null {
    // Check that we got *any* `TurbopackMessageAction`, even if
    // `updatedModules` is empty (not everything gets recorded there).
    //
    // There's also a case where `onBuilt` gets called before `onBuilding`,
    // which can happen during initial page load. Ignore that too!
    const hasUpdates =
      this.#lastUpdateMsSinceEpoch != null && this.#startMsSinceEpoch != null
    if (!hasUpdates && this.#deferredReportHmrStartId != null) {
      // suppress the update entirely
      this.#cancelDeferredReportHmrStart()
      return null
    }
    this.#runDeferredReportHmrStart()

    const result = {
      hasUpdates,
      updatedModules: this.#updatedModules,
      startMsSinceEpoch: this.#startMsSinceEpoch!,
      endMsSinceEpoch: this.#lastUpdateMsSinceEpoch ?? Date.now(),
    }
    this.#updatedModules = new Set()
    return result
  }
}

function extractModulesFromTurbopackMessage(
  data: TurbopackUpdate | TurbopackUpdate[]
): Set<string> {
  const updatedModules: Set<string> = new Set()

  const updates = Array.isArray(data) ? data : [data]
  for (const update of updates) {
    // TODO this won't capture changes to CSS since they don't result in a "merged" update
    if (
      update.type !== 'partial' ||
      update.instruction.type !== 'ChunkListUpdate' ||
      update.instruction.merged === undefined
    ) {
      continue
    }

    for (const mergedUpdate of update.instruction.merged) {
      for (const name of Object.keys(mergedUpdate.entries)) {
        const res = /(.*)\s+\[.*/.exec(name)
        if (res === null) {
          console.error(
            '[Turbopack HMR] Expected module to match pattern: ' + name
          )
          continue
        }

        updatedModules.add(res[1])
      }
    }
  }

  return updatedModules
}