Skip to content

feat: add this.meta.viteVersion #20088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
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
106 changes: 80 additions & 26 deletions packages/vite/src/node/__tests__/plugins/hooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,27 @@ const resolveConfigWithPlugin = (
)
}

const ENTRY_ID = 'entry.js'
const RESOLVED_ENTRY_ID = `\0${ENTRY_ID}`
const resolveEntryPlugin: Plugin = {
name: 'resolve-entry.js',
resolveId(id) {
if (id === ENTRY_ID) {
return RESOLVED_ENTRY_ID
}
},
load(id) {
if (id === RESOLVED_ENTRY_ID) {
return 'export default {}'
}
},
}

const createServerWithPlugin = async (plugin: Plugin) => {
const server = await createServer({
configFile: false,
root: import.meta.dirname,
plugins: [plugin],
plugins: [plugin, resolveEntryPlugin],
logLevel: 'error',
server: {
middlewareMode: true,
Expand Down Expand Up @@ -62,28 +78,13 @@ const buildWithPlugin = async (plugin: Plugin) => {
build: {
write: false,
},
plugins: [
{
name: 'resolve-entry.js',
resolveId(id) {
if (id === 'entry.js') {
return '\0' + id
}
},
load(id) {
if (id === '\0entry.js') {
return 'export default {}'
}
},
},
plugin,
],
plugins: [plugin, resolveEntryPlugin],
})
}

describe('supports plugin context', () => {
test('config hook', async () => {
expect.assertions(3)
expect.assertions(4)

await resolveConfigWithPlugin({
name: 'test',
Expand All @@ -96,14 +97,15 @@ describe('supports plugin context', () => {
meta: expect.any(Object),
})
expect(this.meta.rollupVersion).toBeTypeOf('string')
expect(this.meta.viteVersion).toBeTypeOf('string')
// @ts-expect-error watchMode should not exist in types
expect(this.meta.watchMode).toBeUndefined()
},
})
})

test('configEnvironment hook', async () => {
expect.assertions(3)
expect.assertions(4)

await resolveConfigWithPlugin({
name: 'test',
Expand All @@ -118,14 +120,15 @@ describe('supports plugin context', () => {
meta: expect.any(Object),
})
expect(this.meta.rollupVersion).toBeTypeOf('string')
expect(this.meta.viteVersion).toBeTypeOf('string')
// @ts-expect-error watchMode should not exist in types
expect(this.meta.watchMode).toBeUndefined()
},
})
})

test('configResolved hook', async () => {
expect.assertions(3)
expect.assertions(4)

await resolveConfigWithPlugin({
name: 'test',
Expand All @@ -138,13 +141,14 @@ describe('supports plugin context', () => {
meta: expect.any(Object),
})
expect(this.meta.rollupVersion).toBeTypeOf('string')
expect(this.meta.viteVersion).toBeTypeOf('string')
expect(this.meta.watchMode).toBe(true)
},
})
})

test('configureServer hook', async () => {
expect.assertions(3)
expect.assertions(4)

await createServerWithPlugin({
name: 'test',
Expand All @@ -157,13 +161,14 @@ describe('supports plugin context', () => {
meta: expect.any(Object),
})
expect(this.meta.rollupVersion).toBeTypeOf('string')
expect(this.meta.viteVersion).toBeTypeOf('string')
expect(this.meta.watchMode).toBe(true)
},
})
})

test('configurePreviewServer hook', async () => {
expect.assertions(3)
expect.assertions(4)

await createPreviewServerWithPlugin({
name: 'test',
Expand All @@ -176,13 +181,14 @@ describe('supports plugin context', () => {
meta: expect.any(Object),
})
expect(this.meta.rollupVersion).toBeTypeOf('string')
expect(this.meta.viteVersion).toBeTypeOf('string')
expect(this.meta.watchMode).toBe(false)
},
})
})

test('transformIndexHtml hook in dev', async () => {
expect.assertions(3)
expect.assertions(4)

const server = await createServerWithPlugin({
name: 'test',
Expand All @@ -195,14 +201,15 @@ describe('supports plugin context', () => {
meta: expect.any(Object),
})
expect(this.meta.rollupVersion).toBeTypeOf('string')
expect(this.meta.viteVersion).toBeTypeOf('string')
expect(this.meta.watchMode).toBe(true)
},
})
await server.transformIndexHtml('/index.html', '<html></html>')
})

test('transformIndexHtml hook in build', async () => {
expect.assertions(3)
expect.assertions(4)

await buildWithPlugin({
name: 'test',
Expand All @@ -215,13 +222,14 @@ describe('supports plugin context', () => {
meta: expect.any(Object),
})
expect(this.meta.rollupVersion).toBeTypeOf('string')
expect(this.meta.viteVersion).toBeTypeOf('string')
expect(this.meta.watchMode).toBe(false)
},
})
})

test('handleHotUpdate hook', async () => {
expect.assertions(3)
expect.assertions(4)

const { promise, resolve } = promiseWithResolvers<void>()
const server = await createServerWithPlugin({
Expand All @@ -235,6 +243,7 @@ describe('supports plugin context', () => {
meta: expect.any(Object),
})
expect(this.meta.rollupVersion).toBeTypeOf('string')
expect(this.meta.viteVersion).toBeTypeOf('string')
expect(this.meta.watchMode).toBe(true)
resolve()
},
Expand All @@ -248,7 +257,7 @@ describe('supports plugin context', () => {
})

test('hotUpdate hook', async () => {
expect.assertions(3)
expect.assertions(4)

const { promise, resolve } = promiseWithResolvers<void>()
const server = await createServerWithPlugin({
Expand All @@ -265,6 +274,7 @@ describe('supports plugin context', () => {
environment: expect.any(Object),
})
expect(this.meta.rollupVersion).toBeTypeOf('string')
expect(this.meta.viteVersion).toBeTypeOf('string')
expect(this.meta.watchMode).toBe(true)
resolve()
},
Expand All @@ -276,4 +286,48 @@ describe('supports plugin context', () => {

await promise
})

test('transform hook in dev', async () => {
expect.assertions(4)

const server = await createServerWithPlugin({
name: 'test',
transform(_code, id) {
if (id !== RESOLVED_ENTRY_ID) return
expect(this).toMatchObject({
debug: expect.any(Function),
info: expect.any(Function),
warn: expect.any(Function),
error: expect.any(Function),
meta: expect.any(Object),
})
expect(this.meta.rollupVersion).toBeTypeOf('string')
expect(this.meta.viteVersion).toBeTypeOf('string')
expect(this.meta.watchMode).toBe(true)
},
})
await server.transformRequest(ENTRY_ID)
await server.close()
})

test('transform hook in build', async () => {
expect.assertions(4)

await buildWithPlugin({
name: 'test',
transform(_code, id) {
if (id !== RESOLVED_ENTRY_ID) return
expect(this).toMatchObject({
debug: expect.any(Function),
info: expect.any(Function),
warn: expect.any(Function),
error: expect.any(Function),
meta: expect.any(Object),
})
expect(this.meta.rollupVersion).toBeTypeOf('string')
expect(this.meta.viteVersion).toBeTypeOf('string')
expect(this.meta.watchMode).toBe(false)
},
})
})
})
12 changes: 6 additions & 6 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import {
mergeWithDefaults,
normalizePath,
partialEncodeURIPath,
rollupVersion,
} from './utils'
import { perEnvironmentPlugin, resolveEnvironmentPlugins } from './plugin'
import { manifestPlugin } from './plugins/manifest'
Expand All @@ -80,7 +79,10 @@ import {
} from './baseEnvironment'
import type { MinimalPluginContextWithoutEnvironment, Plugin } from './plugin'
import type { RollupPluginHooks } from './typeUtils'
import { BasicMinimalPluginContext } from './server/pluginContainer'
import {
BasicMinimalPluginContext,
basePluginContextMeta,
} from './server/pluginContainer'

export interface BuildEnvironmentOptions {
/**
Expand Down Expand Up @@ -1269,6 +1271,7 @@ function injectEnvironmentInContext<Context extends MinimalPluginContext>(
context: Context,
environment: BuildEnvironment,
) {
context.meta.viteVersion ??= VERSION
context.environment ??= environment
return context
}
Expand Down Expand Up @@ -1576,10 +1579,7 @@ export async function createBuilder(
config,
async buildApp() {
const pluginContext = new BasicMinimalPluginContext(
{
rollupVersion,
watchMode: false,
},
{ ...basePluginContextMeta, watchMode: false },
config.logger,
)

Expand Down
12 changes: 7 additions & 5 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ import {
nodeLikeBuiltins,
normalizeAlias,
normalizePath,
rollupVersion,
} from './utils'
import {
createPluginHookUtils,
Expand Down Expand Up @@ -104,7 +103,10 @@ import { PartialEnvironment } from './baseEnvironment'
import { createIdResolver } from './idResolver'
import { runnerImport } from './ssr/runnerImport'
import { getAdditionalAllowedHosts } from './server/middlewares/hostCheck'
import { BasicMinimalPluginContext } from './server/pluginContainer'
import {
BasicMinimalPluginContext,
basePluginContextMeta,
} from './server/pluginContainer'

const debug = createDebugger('vite:config', { depth: 10 })
const promisifiedRealpath = promisify(fs.realpath)
Expand Down Expand Up @@ -1378,7 +1380,7 @@ export async function resolveConfig(

const resolvedConfigContext = new BasicMinimalPluginContext(
{
rollupVersion,
...basePluginContextMeta,
watchMode:
(command === 'serve' && !isPreview) ||
(command === 'build' && !!resolvedBuildOptions.watch),
Expand Down Expand Up @@ -2110,7 +2112,7 @@ async function runConfigHook(
})
const context = new BasicMinimalPluginContext<
Omit<PluginContextMeta, 'watchMode'>
>({ rollupVersion }, tempLogger)
>(basePluginContextMeta, tempLogger)

for (const p of getSortedPluginsByHook('config', plugins)) {
const hook = p.config
Expand All @@ -2133,7 +2135,7 @@ async function runConfigEnvironmentHook(
): Promise<void> {
const context = new BasicMinimalPluginContext<
Omit<PluginContextMeta, 'watchMode'>
>({ rollupVersion }, logger)
>(basePluginContextMeta, logger)

const environmentNames = Object.keys(environments)
for (const p of getSortedPluginsByHook('configEnvironment', plugins)) {
Expand Down
5 changes: 5 additions & 0 deletions packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export interface PluginContextExtension {
environment: Environment
}

export interface PluginContextMetaExtension {
viteVersion: string
}

export interface ConfigPluginContext
extends Omit<MinimalPluginContext, 'meta' | 'environment'> {
meta: Omit<PluginContextMeta, 'watchMode'>
Expand All @@ -74,6 +78,7 @@ export interface MinimalPluginContextWithoutEnvironment
// Augment Rollup types to have the PluginContextExtension
declare module 'rollup' {
export interface MinimalPluginContext extends PluginContextExtension {}
export interface PluginContextMeta extends PluginContextMetaExtension {}
}

/**
Expand Down
11 changes: 5 additions & 6 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
getServerUrlByHost,
resolveHostname,
resolveServerUrls,
rollupVersion,
setupSIGTERMListener,
shouldServeFile,
teardownSIGTERMListener,
Expand All @@ -41,7 +40,10 @@ import type { InlineConfig, ResolvedConfig } from './config'
import { DEFAULT_PREVIEW_PORT } from './constants'
import type { RequiredExceptFor } from './typeUtils'
import { hostValidationMiddleware } from './server/middlewares/hostCheck'
import { BasicMinimalPluginContext } from './server/pluginContainer'
import {
BasicMinimalPluginContext,
basePluginContextMeta,
} from './server/pluginContainer'
import type { MinimalPluginContextWithoutEnvironment } from './plugin'

export interface PreviewOptions extends CommonServerOptions {}
Expand Down Expand Up @@ -195,10 +197,7 @@ export async function preview(

// apply server hooks from plugins
const configurePreviewServerContext = new BasicMinimalPluginContext(
{
rollupVersion,
watchMode: false,
},
{ ...basePluginContextMeta, watchMode: false },
config.logger,
)
const postHooks: ((() => void) | void)[] = []
Expand Down
Loading