-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathhmr.ts
More file actions
309 lines (275 loc) · 9.16 KB
/
Copy pathhmr.ts
File metadata and controls
309 lines (275 loc) · 9.16 KB
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
import type { HotPayload, Update } from '#types/hmrPayload'
import type { ModuleNamespace, ViteHotContext } from '#types/hot'
import type { InferCustomEventPayload } from '#types/customEvent'
import type { NormalizedModuleRunnerTransport } from './moduleRunnerTransport'
type CustomListenersMap = Map<string, ((data: any) => void)[]>
interface HotModule {
id: string
callbacks: HotCallback[]
}
interface HotCallback {
// the dependencies must be fetchable paths
deps: string[]
fn: (modules: Array<ModuleNamespace | undefined>) => void
}
export interface HMRLogger {
error(msg: string | Error): void
debug(...msg: unknown[]): void
}
export class HMRContext implements ViteHotContext {
private newListeners: CustomListenersMap
constructor(
private hmrClient: HMRClient,
private ownerPath: string,
) {
if (!hmrClient.dataMap.has(ownerPath)) {
hmrClient.dataMap.set(ownerPath, {})
}
// when a file is hot updated, a new context is created
// clear its stale callbacks
const mod = hmrClient.hotModulesMap.get(ownerPath)
if (mod) {
mod.callbacks = []
}
// clear stale custom event listeners
const staleListeners = hmrClient.ctxToListenersMap.get(ownerPath)
if (staleListeners) {
for (const [event, staleFns] of staleListeners) {
const listeners = hmrClient.customListenersMap.get(event)
if (listeners) {
hmrClient.customListenersMap.set(
event,
listeners.filter((l) => !staleFns.includes(l)),
)
}
}
}
this.newListeners = new Map()
hmrClient.ctxToListenersMap.set(ownerPath, this.newListeners)
}
get data(): any {
return this.hmrClient.dataMap.get(this.ownerPath)
}
accept(deps?: any, callback?: any): void {
if (typeof deps === 'function' || !deps) {
// self-accept: hot.accept(() => {})
this.acceptDeps([this.ownerPath], ([mod]) => deps?.(mod))
} else if (typeof deps === 'string') {
// explicit deps
this.acceptDeps([deps], ([mod]) => callback?.(mod))
} else if (Array.isArray(deps)) {
this.acceptDeps(deps, callback)
} else {
throw new Error(`invalid hot.accept() usage.`)
}
}
// export names (first arg) are irrelevant on the client side, they're
// extracted in the server for propagation
acceptExports(
_: string | readonly string[],
callback?: (data: any) => void,
): void {
this.acceptDeps([this.ownerPath], ([mod]) => callback?.(mod))
}
dispose(cb: (data: any) => void): void {
this.hmrClient.disposeMap.set(this.ownerPath, cb)
}
prune(cb: (data: any) => void): void {
this.hmrClient.pruneMap.set(this.ownerPath, cb)
}
// Kept for backward compatibility (#11036)
// eslint-disable-next-line @typescript-eslint/no-empty-function
decline(): void {}
invalidate(message: string): void {
const firstInvalidatedBy =
this.hmrClient.currentFirstInvalidatedBy ?? this.ownerPath
this.hmrClient.notifyListeners('vite:invalidate', {
path: this.ownerPath,
message,
firstInvalidatedBy,
})
this.send('vite:invalidate', {
path: this.ownerPath,
message,
firstInvalidatedBy,
})
this.hmrClient.logger.debug(
`invalidate ${this.ownerPath}${message ? `: ${message}` : ''}`,
)
}
on<T extends string>(
event: T,
cb: (payload: InferCustomEventPayload<T>) => void,
): void {
const addToMap = (map: Map<string, any[]>) => {
const existing = map.get(event) || []
existing.push(cb)
map.set(event, existing)
}
addToMap(this.hmrClient.customListenersMap)
addToMap(this.newListeners)
}
off<T extends string>(
event: T,
cb: (payload: InferCustomEventPayload<T>) => void,
): void {
const removeFromMap = (map: Map<string, any[]>) => {
const existing = map.get(event)
if (existing === undefined) {
return
}
const pruned = existing.filter((l) => l !== cb)
if (pruned.length === 0) {
map.delete(event)
return
}
map.set(event, pruned)
}
removeFromMap(this.hmrClient.customListenersMap)
removeFromMap(this.newListeners)
}
send<T extends string>(event: T, data?: InferCustomEventPayload<T>): void {
this.hmrClient.send({ type: 'custom', event, data })
}
private acceptDeps(
deps: string[],
callback: HotCallback['fn'] = () => {},
): void {
const mod: HotModule = this.hmrClient.hotModulesMap.get(this.ownerPath) || {
id: this.ownerPath,
callbacks: [],
}
mod.callbacks.push({
deps,
fn: callback,
})
this.hmrClient.hotModulesMap.set(this.ownerPath, mod)
}
}
export class HMRClient {
public hotModulesMap: Map<string, HotModule> = new Map()
public disposeMap: Map<string, (data: any) => void | Promise<void>> =
new Map()
public pruneMap: Map<string, (data: any) => void | Promise<void>> = new Map()
public dataMap: Map<string, any> = new Map()
public customListenersMap: CustomListenersMap = new Map()
public ctxToListenersMap: Map<string, CustomListenersMap> = new Map()
public currentFirstInvalidatedBy: string | undefined
constructor(
public logger: HMRLogger,
private transport: NormalizedModuleRunnerTransport,
// This allows implementing reloading via different methods depending on the environment
private importUpdatedModule: (update: Update) => Promise<ModuleNamespace>,
) {}
public async notifyListeners<T extends string>(
event: T,
data: InferCustomEventPayload<T>,
): Promise<void>
public async notifyListeners(event: string, data: any): Promise<void> {
const cbs = this.customListenersMap.get(event)
if (cbs) {
await Promise.allSettled(cbs.map((cb) => cb(data)))
}
}
public send(payload: HotPayload): void {
this.transport.send(payload).catch((err) => {
this.logger.error(err)
})
}
public clear(): void {
this.hotModulesMap.clear()
this.disposeMap.clear()
this.pruneMap.clear()
this.dataMap.clear()
this.customListenersMap.clear()
this.ctxToListenersMap.clear()
}
// After an HMR update, some modules are no longer imported on the page
// but they may have left behind side effects that need to be cleaned up
// (e.g. style injections)
public async prunePaths(paths: string[]): Promise<void> {
await Promise.all(
paths.map((path) => {
const disposer = this.disposeMap.get(path)
if (disposer) return disposer(this.dataMap.get(path))
}),
)
await Promise.all(
paths.map((path) => {
const fn = this.pruneMap.get(path)
if (fn) {
return fn(this.dataMap.get(path))
}
}),
)
}
protected warnFailedUpdate(err: Error, path: string | string[]): void {
if (!(err instanceof Error) || !err.message.includes('fetch')) {
this.logger.error(err)
}
this.logger.error(
`Failed to reload ${path}. ` +
`This could be due to syntax errors or importing non-existent ` +
`modules. (see errors above)`,
)
}
private updateQueue: Promise<(() => void) | undefined>[] = []
private pendingUpdateQueue = false
/**
* buffer multiple hot updates triggered by the same src change
* so that they are invoked in the same order they were sent.
* (otherwise the order may be inconsistent because of the http request round trip)
*/
public async queueUpdate(payload: Update): Promise<void> {
this.updateQueue.push(this.fetchUpdate(payload))
if (!this.pendingUpdateQueue) {
this.pendingUpdateQueue = true
await Promise.resolve()
this.pendingUpdateQueue = false
const loading = [...this.updateQueue]
this.updateQueue = []
;(await Promise.all(loading)).forEach((fn) => fn && fn())
}
}
private async fetchUpdate(update: Update): Promise<(() => void) | undefined> {
const { path, acceptedPath, firstInvalidatedBy } = update
const mod = this.hotModulesMap.get(path)
if (!mod) {
// In a code-splitting project,
// it is common that the hot-updating module is not loaded yet.
// https://github.com/vitejs/vite/issues/721
return
}
let fetchedModule: ModuleNamespace | undefined
const isSelfUpdate = path === acceptedPath
// determine the qualified callbacks before we re-import the modules
const qualifiedCallbacks = mod.callbacks.filter(({ deps }) =>
deps.includes(acceptedPath),
)
if (isSelfUpdate || qualifiedCallbacks.length > 0) {
const disposer = this.disposeMap.get(acceptedPath)
if (disposer) await disposer(this.dataMap.get(acceptedPath))
try {
fetchedModule = await this.importUpdatedModule(update)
} catch (e) {
this.warnFailedUpdate(e, acceptedPath)
}
}
return () => {
try {
this.currentFirstInvalidatedBy = firstInvalidatedBy
for (const { deps, fn } of qualifiedCallbacks) {
fn(
deps.map((dep) =>
dep === acceptedPath ? fetchedModule : undefined,
),
)
}
const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}`
this.logger.debug(`hot updated: ${loggedPath}`)
} finally {
this.currentFirstInvalidatedBy = undefined
}
}
}
}