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
6 changes: 6 additions & 0 deletions packages/vite/rolldown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ImportType, init, parse } from 'es-module-lexer'
import MagicString from 'magic-string'
import type { Plugin } from 'rolldown'
import { defineConfig } from 'rolldown'
import rolldownPkg from 'rolldown/package.json' with { type: 'json' }
import pkg from './package.json' with { type: 'json' }
import licensePlugin from './rollupLicensePlugin'

Expand Down Expand Up @@ -83,6 +84,11 @@ const nodeConfig = defineConfig({
cli: path.resolve(dirname, 'src/node/cli.ts'),
internal: path.resolve(dirname, 'src/node/internalIndex.ts'),
},
transform: {
define: {
__VITE_BUNDLED_ROLLDOWN_VERSION__: JSON.stringify(rolldownPkg.version),
},
},
external: [
/^vite\//,
'fsevents',
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/client/bundledDevClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
updateStyle,
} from './client'

// Replaced with the rolldown version this client was built against when the
// file is served, see `getHmrImplementation` in `node/plugins/clientInjections.ts`.
declare const __VITE_BUNDLED_ROLLDOWN_VERSION__: string

// keep the same public exports as `client.ts`, which this entry replaces when inlined
export {
createHotContext,
Expand Down Expand Up @@ -51,6 +55,8 @@ if (typeof DevRuntime !== 'undefined') {
const runtime = ((globalThis as any).__rolldown_runtime__ ??=
new ViteDevRuntime(clientId))

;(runtime as any).bundledRolldownVersion = __VITE_BUNDLED_ROLLDOWN_VERSION__

const bundledDevHmrClient = new BundledDevHMRClient(
{
error: (err) => console.error('[vite]', err),
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export const ROLLUP_HOOKS: RollupPluginHooks[] = [

export const VERSION = version as string

declare const __VITE_BUNDLED_ROLLDOWN_VERSION__: string
export const BUNDLED_ROLLDOWN_VERSION: string | undefined =
typeof __VITE_BUNDLED_ROLLDOWN_VERSION__ === 'undefined'
? undefined
: __VITE_BUNDLED_ROLLDOWN_VERSION__

const DEFAULT_MAIN_FIELDS = [
'browser',
'module',
Expand Down
14 changes: 13 additions & 1 deletion packages/vite/src/node/plugins/clientInjections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import fs from 'node:fs'
import path from 'node:path'
import { cleanUrl } from '../../shared/utils'
import type { ResolvedConfig } from '../config'
import { CLIENT_ENTRY, ENV_ENTRY, BUNDLED_DEV_CLIENT_ENTRY } from '../constants'
import {
CLIENT_ENTRY,
ENV_ENTRY,
BUNDLED_DEV_CLIENT_ENTRY,
BUNDLED_ROLLDOWN_VERSION,
} from '../constants'
import { perEnvironmentState } from '../environment'
import type { Plugin } from '../plugin'
import { isObject, normalizePath, resolveHostname } from '../utils'
Expand Down Expand Up @@ -120,6 +125,9 @@ async function createClientConfigValueReplacer(
const serverForwardConsoleReplacement = escapeReplacement(
config.server.forwardConsole as any,
)
const bundledRolldownVersionReplacement = escapeReplacement(
BUNDLED_ROLLDOWN_VERSION ?? 'unknown',
)

return (code) =>
code
Expand All @@ -136,6 +144,10 @@ async function createClientConfigValueReplacer(
.replace(`__HMR_CONFIG_NAME__`, hmrConfigNameReplacement)
.replace(`__WS_TOKEN__`, wsTokenReplacement)
.replace(`__SERVER_FORWARD_CONSOLE__`, serverForwardConsoleReplacement)
.replace(
/__VITE_BUNDLED_ROLLDOWN_VERSION__/g,
bundledRolldownVersionReplacement,
)
}

export async function getHmrImplementation(
Expand Down
26 changes: 25 additions & 1 deletion packages/vite/src/node/server/bundledDev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import {
type DevEngine,
dev,
} from 'rolldown/experimental'
import rolldownPkg from 'rolldown/package.json' with { type: 'json' }
import { ChunkMetadataMap, resolveRolldownOptions } from '../build'
import { BUNDLED_DEV_CLIENT_FILENAME } from '../constants'
import {
BUNDLED_DEV_CLIENT_FILENAME,
BUNDLED_ROLLDOWN_VERSION,
} from '../constants'
import { getHmrImplementation } from '../plugins/clientInjections'
import { createDebugger, formatAndTruncateFileList } from '../utils'
import { convertToDevWatchOptions } from '../watch'
Expand Down Expand Up @@ -116,6 +120,7 @@ export class BundledDev {

async listen(): Promise<void> {
this._closed = false
checkBundledRolldownVersion(this.environment)
debug?.('INITIAL: setup bundle options')
const rolldownOptions = await this.getRolldownOptions()
// NOTE: only single outputOptions is supported here
Expand Down Expand Up @@ -561,3 +566,22 @@ function debounce(time: number, cb: () => void) {
function payloadDeliveredAck(filename: string): string {
return `\n;__rolldown_runtime__.payloadDelivered(${JSON.stringify(filename)});`
}

function checkBundledRolldownVersion(environment: DevEnvironment): void {
// undefined when running from source, where nothing is pre-bundled
if (BUNDLED_ROLLDOWN_VERSION === undefined) return
const installed = rolldownPkg.version
if (installed === BUNDLED_ROLLDOWN_VERSION) return
environment.logger.warn(
colors.yellow(
`experimental.bundledDev: this Vite release bundles the rolldown dev runtime ` +
`of rolldown@${BUNDLED_ROLLDOWN_VERSION}, but the installed rolldown@${installed} ` +
`generates the code that runtime executes. The mismatch can break dynamic ` +
`imports or HMR at runtime with errors like ` +
`"__rolldown_runtime__.requestLazy is not a function".\n` +
`To fix this, upgrade Vite to a release built against rolldown@${installed}, ` +
`or pin rolldown to ${BUNDLED_ROLLDOWN_VERSION} (e.g. via the "overrides" ` +
`field in package.json).`,
),
)
}
Loading