Skip to content
Merged
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
2 changes: 1 addition & 1 deletion packages/vite/rolldown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const bundledDevClientConfig = defineConfig({
transform: {
target: 'es2020',
},
// the runtime is served from the installed rolldown at dev time (`getRolldownDevRuntimeFiles`)
// the runtime is served from the installed rolldown at dev time (`getRolldownDevRuntime`)
external: ['@vite/env', 'rolldown/experimental/runtime'],
output: {
dir: path.resolve(dirname, 'dist'),
Expand Down
22 changes: 7 additions & 15 deletions packages/vite/src/node/__tests__/plugins/clientInjections.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import path from 'node:path'
import { expect, test } from 'vitest'
import { getRolldownDevRuntimeFiles } from '../../plugins/clientInjections'
import { getRolldownDevRuntime } from '../../plugins/clientInjections'

// Catch a rolldown layout change here, instead of as a 404 in the browser. The runtime may
// be one file with no imports, or an entry that imports helper files with relative paths.
test('serves every file the rolldown dev runtime imports', () => {
const files = getRolldownDevRuntimeFiles()
expect(files.get('@rolldown/experimental-runtime.mjs')).toContain(
'DevRuntime',
)
for (const [urlPath, source] of files) {
for (const match of source.matchAll(/\bfrom\s*['"](\.\.?\/[^'"]+)['"]/g)) {
const imported = path.posix.join(path.posix.dirname(urlPath), match[1])
expect(files.has(imported), `${urlPath} imports ${match[1]}`).toBe(true)
}
}
// Only this one file is served, so an import in it would 404 in the browser.
test('the rolldown dev runtime imports nothing', () => {
const source = getRolldownDevRuntime()
expect(source).toContain('DevRuntime')
expect(source).not.toMatch(/^\s*import\b/m)
expect(source).not.toMatch(/\bfrom\s*['"]/)
})
5 changes: 3 additions & 2 deletions packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ export const BUNDLED_DEV_CLIENT_ENTRY: string = resolve(
)
/** URL filename the bundled-dev server serves the vite client under */
export const BUNDLED_DEV_CLIENT_FILENAME: string = 'bundledDevClient.mjs'
/** URL directory the bundled-dev server serves rolldown's dev runtime files under */
export const BUNDLED_DEV_ROLLDOWN_RUNTIME_DIR: string = '@rolldown'
/** URL path the bundled-dev server serves rolldown's dev runtime under */
export const BUNDLED_DEV_ROLLDOWN_RUNTIME_FILENAME: string =
'@rolldown/experimental-runtime.mjs'
export const ENV_ENTRY: string = resolve(
VITE_PACKAGE_DIR,
'dist/client/env.mjs',
Expand Down
38 changes: 8 additions & 30 deletions packages/vite/src/node/plugins/clientInjections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { ResolvedConfig } from '../config'
import {
BUNDLED_DEV_CLIENT_ENTRY,
BUNDLED_DEV_CLIENT_FILENAME,
BUNDLED_DEV_ROLLDOWN_RUNTIME_DIR,
BUNDLED_DEV_ROLLDOWN_RUNTIME_FILENAME,
CLIENT_ENTRY,
CLIENT_PUBLIC_PATH,
ENV_ENTRY,
Expand Down Expand Up @@ -167,42 +167,20 @@ export async function getHmrImplementation(
.replace(
new RegExp(`(from\\s*['"])${ROLLDOWN_DEV_RUNTIME_ENTRY}(['"])`),
(_, before, after) =>
`${before}${path.posix.join(
config.base,
BUNDLED_DEV_ROLLDOWN_RUNTIME_DIR,
path.basename(rolldownDevRuntimeEntryPath()),
)}${after}`,
`${before}${path.posix.join(config.base, BUNDLED_DEV_ROLLDOWN_RUNTIME_FILENAME)}${after}`,
)
)
}

function rolldownDevRuntimeEntryPath(): string {
return fileURLToPath(import.meta.resolve(ROLLDOWN_DEV_RUNTIME_ENTRY))
}

/**
* The dev runtime must match the rolldown that generates the bundle, so it is read from the
* installed package at serve time instead of being bundled into the client. The entry imports
* its helper file with a relative path, so both are served under the same directory.
* installed package at serve time instead of being bundled into the client.
*/
export function getRolldownDevRuntimeFiles(): Map<string, string> {
const entry = rolldownDevRuntimeEntryPath()
const dir = path.dirname(entry)
const files = new Map<string, string>()
for (const name of fs.readdirSync(dir)) {
if (name.startsWith('experimental-runtime') && name.endsWith('.mjs')) {
files.set(
`${BUNDLED_DEV_ROLLDOWN_RUNTIME_DIR}/${name}`,
fs.readFileSync(path.join(dir, name), 'utf-8'),
)
}
}
if (
!files.has(`${BUNDLED_DEV_ROLLDOWN_RUNTIME_DIR}/${path.basename(entry)}`)
) {
throw new Error(`rolldown dev runtime entry ${entry} was not found`)
}
return files
export function getRolldownDevRuntime(): string {
return fs.readFileSync(
fileURLToPath(import.meta.resolve(ROLLDOWN_DEV_RUNTIME_ENTRY)),
'utf-8',
)
}

export function getBundledDevClientAliases(
Expand Down
9 changes: 6 additions & 3 deletions packages/vite/src/node/server/bundledDev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import {
dev,
} from 'rolldown/experimental'
import { ChunkMetadataMap, resolveRolldownOptions } from '../build'
import { BUNDLED_DEV_CLIENT_FILENAME } from '../constants'
import {
BUNDLED_DEV_CLIENT_FILENAME,
BUNDLED_DEV_ROLLDOWN_RUNTIME_FILENAME,
} from '../constants'
import {
getBundledDevClientAliases,
getHmrImplementation,
getRolldownDevRuntimeFiles,
getRolldownDevRuntime,
} from '../plugins/clientInjections'
import {
createDebugger,
Expand Down Expand Up @@ -391,7 +394,7 @@ export class BundledDev {
BUNDLED_DEV_CLIENT_FILENAME,
await getHmrImplementation(this.environment.getTopLevelConfig()),
],
...getRolldownDevRuntimeFiles(),
[BUNDLED_DEV_ROLLDOWN_RUNTIME_FILENAME, getRolldownDevRuntime()],
])
const aliases = getBundledDevClientAliases(
this.environment.getTopLevelConfig(),
Expand Down
Loading