Skip to content

Commit d05fd7b

Browse files
committed
wip: update
1 parent 70fb459 commit d05fd7b

File tree

1 file changed

+52
-23
lines changed

1 file changed

+52
-23
lines changed

packages/vite/src/node/server/environments/fullBundleEnvironment.ts

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ export class MemoryFiles {
5959
export class FullBundleDevEnvironment extends DevEnvironment {
6060
private devEngine!: DevEngine
6161
private invalidateCalledModules = new Set<string>()
62+
private debouncedFullReload = debounce(20, () => {
63+
this.hot.send({ type: 'full-reload', path: '*' })
64+
this.logger.info(colors.green(`page reload`), { timestamp: true })
65+
})
6266

6367
memoryFiles = new MemoryFiles()
6468

@@ -96,12 +100,15 @@ export class FullBundleDevEnvironment extends DevEnvironment {
96100

97101
this.devEngine = await dev(rollupOptions, outputOptions, {
98102
onHmrUpdates: (updates, files) => {
99-
this.invalidateCalledModules.clear()
103+
if (files.length === 0) {
104+
return
105+
}
100106
// TODO: how to handle errors?
101107
if (updates.every((update) => update.type === 'Noop')) {
102108
debug?.(`ignored file change for ${files.join(', ')}`)
103109
return
104110
}
111+
this.invalidateCalledModules.clear()
105112
for (const update of updates) {
106113
this.handleHmrOutput(files, update)
107114
}
@@ -126,9 +133,10 @@ export class FullBundleDevEnvironment extends DevEnvironment {
126133
}
127134

128135
private async waitForInitialBuildFinish(): Promise<void> {
136+
await this.devEngine.ensureCurrentBuildFinish()
129137
while (this.memoryFiles.size === 0) {
130-
await this.devEngine.ensureCurrentBuildFinish()
131138
await new Promise((resolve) => setTimeout(resolve, 10))
139+
await this.devEngine.ensureCurrentBuildFinish()
132140
}
133141
}
134142

@@ -144,12 +152,14 @@ export class FullBundleDevEnvironment extends DevEnvironment {
144152
;(async () => {
145153
if (this.invalidateCalledModules.has(m.path)) {
146154
debug?.(
147-
'INVALIDATE: invalidate received, but ignored because it was already invalidated',
155+
`INVALIDATE: invalidate received from ${m.path}, but ignored because it was already invalidated`,
148156
)
149157
return
150158
}
151159

152-
debug?.('INVALIDATE: invalidate received, re-triggering HMR')
160+
debug?.(
161+
`INVALIDATE: invalidate received from ${m.path}, re-triggering HMR`,
162+
)
153163
this.invalidateCalledModules.add(m.path)
154164

155165
// TODO: how to handle errors?
@@ -168,16 +178,18 @@ export class FullBundleDevEnvironment extends DevEnvironment {
168178
}
169179

170180
// TODO: need to check if this is enough
171-
this.handleHmrOutput([m.path], update, m.firstInvalidatedBy)
181+
this.handleHmrOutput([m.path], update, {
182+
firstInvalidatedBy: m.firstInvalidatedBy,
183+
reason: m.message,
184+
})
172185
})()
173186
}
174187

175188
async triggerBundleRegenerationIfStale(): Promise<boolean> {
176189
const scheduled = await this.devEngine.scheduleBuildIfStale()
177190
if (scheduled === 'scheduled') {
178191
this.devEngine.ensureCurrentBuildFinish().then(() => {
179-
this.hot.send({ type: 'full-reload', path: '*' })
180-
this.logger.info(colors.green(`page reload`), { timestamp: true })
192+
this.debouncedFullReload()
181193
})
182194
debug?.(`TRIGGER: access to stale bundle, triggered bundle re-generation`)
183195
}
@@ -207,13 +219,18 @@ export class FullBundleDevEnvironment extends DevEnvironment {
207219
rolldownOptions.plugins,
208220
{
209221
name: 'vite:full-bundle-mode:save-output',
210-
generateBundle: (_, bundle) => {
211-
// NOTE: don't clear memoryFiles here as incremental build re-uses the files
212-
for (const outputFile of Object.values(bundle)) {
213-
this.memoryFiles.set(outputFile.fileName, () =>
214-
outputFile.type === 'chunk' ? outputFile.code : outputFile.source,
215-
)
216-
}
222+
generateBundle: {
223+
order: 'post',
224+
handler: (_, bundle) => {
225+
// NOTE: don't clear memoryFiles here as incremental build re-uses the files
226+
for (const outputFile of Object.values(bundle)) {
227+
this.memoryFiles.set(outputFile.fileName, () =>
228+
outputFile.type === 'chunk'
229+
? outputFile.code
230+
: outputFile.source,
231+
)
232+
}
233+
},
217234
},
218235
},
219236
]
@@ -240,24 +257,25 @@ export class FullBundleDevEnvironment extends DevEnvironment {
240257
private handleHmrOutput(
241258
files: string[],
242259
hmrOutput: HmrOutput,
243-
firstInvalidatedBy?: string,
260+
invalidateInformation?: { firstInvalidatedBy: string; reason?: string },
244261
) {
245262
if (hmrOutput.type === 'Noop') return
246263

247264
const shortFile = files
248265
.map((file) => getShortName(file, this.config.root))
249266
.join(', ')
250267
if (hmrOutput.type === 'FullReload') {
251-
const reason = hmrOutput.reason
252-
? colors.dim(` (${hmrOutput.reason})`)
253-
: ''
268+
const reason =
269+
(hmrOutput.reason ? colors.dim(` (${hmrOutput.reason})`) : '') +
270+
(invalidateInformation?.reason
271+
? colors.dim(` (${invalidateInformation.reason})`)
272+
: '')
254273
this.logger.info(
255274
colors.green(`trigger page reload `) + colors.dim(shortFile) + reason,
256-
{ clear: !firstInvalidatedBy, timestamp: true },
275+
{ clear: !invalidateInformation, timestamp: true },
257276
)
258277
this.devEngine.ensureLatestBuild().then(() => {
259-
this.hot.send({ type: 'full-reload', path: '*' })
260-
this.logger.info(colors.green(`page reload`), { timestamp: true })
278+
this.debouncedFullReload()
261279
})
262280
return
263281
}
@@ -277,7 +295,7 @@ export class FullBundleDevEnvironment extends DevEnvironment {
277295
url: hmrOutput.filename,
278296
path: boundary.boundary,
279297
acceptedPath: boundary.acceptedVia,
280-
firstInvalidatedBy,
298+
firstInvalidatedBy: invalidateInformation?.firstInvalidatedBy,
281299
timestamp: Date.now(),
282300
}
283301
})
@@ -288,7 +306,18 @@ export class FullBundleDevEnvironment extends DevEnvironment {
288306
this.logger.info(
289307
colors.green(`hmr update `) +
290308
colors.dim([...new Set(updates.map((u) => u.path))].join(', ')),
291-
{ clear: !firstInvalidatedBy, timestamp: true },
309+
{ clear: !invalidateInformation, timestamp: true },
292310
)
293311
}
294312
}
313+
314+
function debounce(time: number, cb: () => void) {
315+
let timer: ReturnType<typeof setTimeout> | null
316+
return () => {
317+
if (timer) {
318+
clearTimeout(timer)
319+
timer = null
320+
}
321+
timer = setTimeout(cb, time)
322+
}
323+
}

0 commit comments

Comments
 (0)