Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/vite/src/node/server/bundledDev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ export class BundledDev {
`TRIGGER-LAZY: trigger lazy bundling for module ${moduleId} for client ${clientId}`,
)
const result = await this.devEngine.compileEntry(moduleId, clientId)
result.code = this.withBaseForLazyBundlingUrl(result.code)
this.pendingPayloadFilenames.add(result.filename)
return result
}
Expand Down Expand Up @@ -375,7 +376,9 @@ export class BundledDev {
for (const outputFile of output) {
this.memoryFiles.set(outputFile.fileName, () => {
const source =
outputFile.type === 'chunk' ? outputFile.code : outputFile.source
outputFile.type === 'chunk'
? this.withBaseForLazyBundlingUrl(outputFile.code)
: outputFile.source
return {
source,
etag: getEtag(Buffer.from(source), { weak: true }),
Expand Down Expand Up @@ -465,7 +468,7 @@ export class BundledDev {
// https://green.sapphi.red/blog/local-server-security-best-practices#properly-check-the-request-origin
// we can also use `Cross-Origin Resource Policy` header instead of this
// but we cannot use `Sec-Fetch-*` headers as they are only sent to potentially-trustworthy origins
source: hmrOutput.code + '\n; export {}',
source: this.withBaseForLazyBundlingUrl(hmrOutput.code) + '\n; export {}',
})
if (hmrOutput.sourcemapFilename && hmrOutput.sourcemap) {
this.memoryFiles.set(hmrOutput.sourcemapFilename, {
Expand All @@ -490,6 +493,12 @@ export class BundledDev {
},
)
}

private withBaseForLazyBundlingUrl(code: string): string {
const { base } = this.environment.config
if (base === '/') return code
return code.replace(/([`"'])\/@vite\/lazy\?/g, `$1${base}@vite/lazy?`)
}
}

class Clients {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect, test } from 'vitest'
import { isServe, page } from '~utils'

test.runIf(isServe)('lazy bundling respects configured base', async () => {
await expect.poll(() => page.textContent('h1')).toBe('HMR Full Bundle Mode')

const lazyResponsePromise = page.waitForResponse((response) => {
return new URL(response.url()).pathname.endsWith('/@vite/lazy')
})
await page.click('#load-dynamic')

const lazyResponse = await lazyResponsePromise
expect(new URL(lazyResponse.url()).pathname).toBe('/nested-base/@vite/lazy')
expect(lazyResponse.status()).toBe(200)
await expect.poll(() => page.textContent('.dynamic')).toBe('loaded')
})
8 changes: 8 additions & 0 deletions playground/hmr-full-bundle-mode/vite.config-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig, mergeConfig } from 'vite'
import baseConfig from './vite.config.ts'

export default defineConfig(
mergeConfig(baseConfig, {
base: '/nested-base/',
}),
)
Loading