Skip to content

Commit eb7aa9a

Browse files
tbvjaos510claude
andauthored
fix(bundled-dev): serve lazy chunk sourcemaps (#23026)
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 24339f4 commit eb7aa9a

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎packages/vite/src/node/server/bundledDev.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,15 @@ export class BundledDev {
343343
)
344344
const result = await this.devEngine.compileEntry(moduleId, clientId)
345345
this.pendingPayloadFilenames.add(result.filename)
346+
// Serve the chunk's sourcemap, the same way an eager chunk's and an HMR
347+
// patch's maps are served. The chunk's relative `sourceMappingURL` resolves
348+
// against the URL it is imported from, `/@vite/lazy?...`, so register the map
349+
// under that directory for the reference to reach it.
350+
if (result.sourcemapFilename && result.sourcemap) {
351+
this.memoryFiles.set(`@vite/${result.sourcemapFilename}`, {
352+
source: result.sourcemap,
353+
})
354+
}
346355
return {
347356
filename: result.filename,
348357
code: result.code + payloadDeliveredAck(result.filename),

‎playground/hmr-full-bundle-mode/__tests__/hmr-full-bundle-mode.spec.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,25 @@ if (isBuild) {
317317
})
318318

319319
test('lazy bundling', async () => {
320+
const responsePromise = page.waitForResponse(/\/@vite\/lazy\?/)
320321
await page.click('#load-dynamic')
321322
await expect.poll(() => page.textContent('.dynamic')).toBe('loaded')
323+
324+
// the lazy chunk names a `.map` file that only exists in memory; the dev
325+
// server has to register it, otherwise the reference 404s and the module
326+
// cannot be mapped back to its source. The reference is relative and a lazy
327+
// chunk is imported from `/@vite/lazy?...`, so it resolves under `/@vite/`.
328+
const response = await responsePromise
329+
const code = await response.text()
330+
const url = code.match(/\/\/# sourceMappingURL=(.+)$/m)?.[1]
331+
expect(url).toMatch(/^lazy_compile_\d+\.js\.map$/)
332+
333+
const mapResponse = await fetch(new URL(url!, response.url()))
334+
expect(mapResponse.status).toBe(200)
335+
const map = await mapResponse.json()
336+
expect(map.version).toBe(3)
337+
expect(map.sources.some((s: string) => s.endsWith('dynamic.js'))).toBe(true)
338+
expect(map.mappings.length).toBeGreaterThan(0)
322339
})
323340

324341
// placed before `invalidate` on purpose: that test's cleanup restores

0 commit comments

Comments
 (0)