Skip to content

Commit f55bf41

Browse files
authored
feat: add this.meta.viteVersion (#20088)
1 parent 7063839 commit f55bf41

File tree

9 files changed

+123
-64
lines changed

9 files changed

+123
-64
lines changed

packages/vite/src/node/__tests__/plugins/hooks.spec.ts

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,27 @@ const resolveConfigWithPlugin = (
1717
)
1818
}
1919

20+
const ENTRY_ID = 'entry.js'
21+
const RESOLVED_ENTRY_ID = `\0${ENTRY_ID}`
22+
const resolveEntryPlugin: Plugin = {
23+
name: 'resolve-entry.js',
24+
resolveId(id) {
25+
if (id === ENTRY_ID) {
26+
return RESOLVED_ENTRY_ID
27+
}
28+
},
29+
load(id) {
30+
if (id === RESOLVED_ENTRY_ID) {
31+
return 'export default {}'
32+
}
33+
},
34+
}
35+
2036
const createServerWithPlugin = async (plugin: Plugin) => {
2137
const server = await createServer({
2238
configFile: false,
2339
root: import.meta.dirname,
24-
plugins: [plugin],
40+
plugins: [plugin, resolveEntryPlugin],
2541
logLevel: 'error',
2642
server: {
2743
middlewareMode: true,
@@ -62,28 +78,13 @@ const buildWithPlugin = async (plugin: Plugin) => {
6278
build: {
6379
write: false,
6480
},
65-
plugins: [
66-
{
67-
name: 'resolve-entry.js',
68-
resolveId(id) {
69-
if (id === 'entry.js') {
70-
return '\0' + id
71-
}
72-
},
73-
load(id) {
74-
if (id === '\0entry.js') {
75-
return 'export default {}'
76-
}
77-
},
78-
},
79-
plugin,
80-
],
81+
plugins: [plugin, resolveEntryPlugin],
8182
})
8283
}
8384

8485
describe('supports plugin context', () => {
8586
test('config hook', async () => {
86-
expect.assertions(3)
87+
expect.assertions(4)
8788

8889
await resolveConfigWithPlugin({
8990
name: 'test',
@@ -96,14 +97,15 @@ describe('supports plugin context', () => {
9697
meta: expect.any(Object),
9798
})
9899
expect(this.meta.rollupVersion).toBeTypeOf('string')
100+
expect(this.meta.viteVersion).toBeTypeOf('string')
99101
// @ts-expect-error watchMode should not exist in types
100102
expect(this.meta.watchMode).toBeUndefined()
101103
},
102104
})
103105
})
104106

105107
test('configEnvironment hook', async () => {
106-
expect.assertions(3)
108+
expect.assertions(4)
107109

108110
await resolveConfigWithPlugin({
109111
name: 'test',
@@ -118,14 +120,15 @@ describe('supports plugin context', () => {
118120
meta: expect.any(Object),
119121
})
120122
expect(this.meta.rollupVersion).toBeTypeOf('string')
123+
expect(this.meta.viteVersion).toBeTypeOf('string')
121124
// @ts-expect-error watchMode should not exist in types
122125
expect(this.meta.watchMode).toBeUndefined()
123126
},
124127
})
125128
})
126129

127130
test('configResolved hook', async () => {
128-
expect.assertions(3)
131+
expect.assertions(4)
129132

130133
await resolveConfigWithPlugin({
131134
name: 'test',
@@ -138,13 +141,14 @@ describe('supports plugin context', () => {
138141
meta: expect.any(Object),
139142
})
140143
expect(this.meta.rollupVersion).toBeTypeOf('string')
144+
expect(this.meta.viteVersion).toBeTypeOf('string')
141145
expect(this.meta.watchMode).toBe(true)
142146
},
143147
})
144148
})
145149

146150
test('configureServer hook', async () => {
147-
expect.assertions(3)
151+
expect.assertions(4)
148152

149153
await createServerWithPlugin({
150154
name: 'test',
@@ -157,13 +161,14 @@ describe('supports plugin context', () => {
157161
meta: expect.any(Object),
158162
})
159163
expect(this.meta.rollupVersion).toBeTypeOf('string')
164+
expect(this.meta.viteVersion).toBeTypeOf('string')
160165
expect(this.meta.watchMode).toBe(true)
161166
},
162167
})
163168
})
164169

165170
test('configurePreviewServer hook', async () => {
166-
expect.assertions(3)
171+
expect.assertions(4)
167172

168173
await createPreviewServerWithPlugin({
169174
name: 'test',
@@ -176,13 +181,14 @@ describe('supports plugin context', () => {
176181
meta: expect.any(Object),
177182
})
178183
expect(this.meta.rollupVersion).toBeTypeOf('string')
184+
expect(this.meta.viteVersion).toBeTypeOf('string')
179185
expect(this.meta.watchMode).toBe(false)
180186
},
181187
})
182188
})
183189

184190
test('transformIndexHtml hook in dev', async () => {
185-
expect.assertions(3)
191+
expect.assertions(4)
186192

187193
const server = await createServerWithPlugin({
188194
name: 'test',
@@ -195,14 +201,15 @@ describe('supports plugin context', () => {
195201
meta: expect.any(Object),
196202
})
197203
expect(this.meta.rollupVersion).toBeTypeOf('string')
204+
expect(this.meta.viteVersion).toBeTypeOf('string')
198205
expect(this.meta.watchMode).toBe(true)
199206
},
200207
})
201208
await server.transformIndexHtml('/index.html', '<html></html>')
202209
})
203210

204211
test('transformIndexHtml hook in build', async () => {
205-
expect.assertions(3)
212+
expect.assertions(4)
206213

207214
await buildWithPlugin({
208215
name: 'test',
@@ -215,13 +222,14 @@ describe('supports plugin context', () => {
215222
meta: expect.any(Object),
216223
})
217224
expect(this.meta.rollupVersion).toBeTypeOf('string')
225+
expect(this.meta.viteVersion).toBeTypeOf('string')
218226
expect(this.meta.watchMode).toBe(false)
219227
},
220228
})
221229
})
222230

223231
test('handleHotUpdate hook', async () => {
224-
expect.assertions(3)
232+
expect.assertions(4)
225233

226234
const { promise, resolve } = promiseWithResolvers<void>()
227235
const server = await createServerWithPlugin({
@@ -235,6 +243,7 @@ describe('supports plugin context', () => {
235243
meta: expect.any(Object),
236244
})
237245
expect(this.meta.rollupVersion).toBeTypeOf('string')
246+
expect(this.meta.viteVersion).toBeTypeOf('string')
238247
expect(this.meta.watchMode).toBe(true)
239248
resolve()
240249
},
@@ -248,7 +257,7 @@ describe('supports plugin context', () => {
248257
})
249258

250259
test('hotUpdate hook', async () => {
251-
expect.assertions(3)
260+
expect.assertions(4)
252261

253262
const { promise, resolve } = promiseWithResolvers<void>()
254263
const server = await createServerWithPlugin({
@@ -265,6 +274,7 @@ describe('supports plugin context', () => {
265274
environment: expect.any(Object),
266275
})
267276
expect(this.meta.rollupVersion).toBeTypeOf('string')
277+
expect(this.meta.viteVersion).toBeTypeOf('string')
268278
expect(this.meta.watchMode).toBe(true)
269279
resolve()
270280
},
@@ -276,4 +286,48 @@ describe('supports plugin context', () => {
276286

277287
await promise
278288
})
289+
290+
test('transform hook in dev', async () => {
291+
expect.assertions(4)
292+
293+
const server = await createServerWithPlugin({
294+
name: 'test',
295+
transform(_code, id) {
296+
if (id !== RESOLVED_ENTRY_ID) return
297+
expect(this).toMatchObject({
298+
debug: expect.any(Function),
299+
info: expect.any(Function),
300+
warn: expect.any(Function),
301+
error: expect.any(Function),
302+
meta: expect.any(Object),
303+
})
304+
expect(this.meta.rollupVersion).toBeTypeOf('string')
305+
expect(this.meta.viteVersion).toBeTypeOf('string')
306+
expect(this.meta.watchMode).toBe(true)
307+
},
308+
})
309+
await server.transformRequest(ENTRY_ID)
310+
await server.close()
311+
})
312+
313+
test('transform hook in build', async () => {
314+
expect.assertions(4)
315+
316+
await buildWithPlugin({
317+
name: 'test',
318+
transform(_code, id) {
319+
if (id !== RESOLVED_ENTRY_ID) return
320+
expect(this).toMatchObject({
321+
debug: expect.any(Function),
322+
info: expect.any(Function),
323+
warn: expect.any(Function),
324+
error: expect.any(Function),
325+
meta: expect.any(Object),
326+
})
327+
expect(this.meta.rollupVersion).toBeTypeOf('string')
328+
expect(this.meta.viteVersion).toBeTypeOf('string')
329+
expect(this.meta.watchMode).toBe(false)
330+
},
331+
})
332+
})
279333
})

packages/vite/src/node/build.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import {
5555
mergeWithDefaults,
5656
normalizePath,
5757
partialEncodeURIPath,
58-
rollupVersion,
5958
} from './utils'
6059
import { perEnvironmentPlugin, resolveEnvironmentPlugins } from './plugin'
6160
import { manifestPlugin } from './plugins/manifest'
@@ -80,7 +79,10 @@ import {
8079
} from './baseEnvironment'
8180
import type { MinimalPluginContextWithoutEnvironment, Plugin } from './plugin'
8281
import type { RollupPluginHooks } from './typeUtils'
83-
import { BasicMinimalPluginContext } from './server/pluginContainer'
82+
import {
83+
BasicMinimalPluginContext,
84+
basePluginContextMeta,
85+
} from './server/pluginContainer'
8486

8587
export interface BuildEnvironmentOptions {
8688
/**
@@ -1269,6 +1271,7 @@ function injectEnvironmentInContext<Context extends MinimalPluginContext>(
12691271
context: Context,
12701272
environment: BuildEnvironment,
12711273
) {
1274+
context.meta.viteVersion ??= VERSION
12721275
context.environment ??= environment
12731276
return context
12741277
}
@@ -1576,10 +1579,7 @@ export async function createBuilder(
15761579
config,
15771580
async buildApp() {
15781581
const pluginContext = new BasicMinimalPluginContext(
1579-
{
1580-
rollupVersion,
1581-
watchMode: false,
1582-
},
1582+
{ ...basePluginContextMeta, watchMode: false },
15831583
config.logger,
15841584
)
15851585

packages/vite/src/node/config.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ import {
7676
nodeLikeBuiltins,
7777
normalizeAlias,
7878
normalizePath,
79-
rollupVersion,
8079
} from './utils'
8180
import {
8281
createPluginHookUtils,
@@ -104,7 +103,10 @@ import { PartialEnvironment } from './baseEnvironment'
104103
import { createIdResolver } from './idResolver'
105104
import { runnerImport } from './ssr/runnerImport'
106105
import { getAdditionalAllowedHosts } from './server/middlewares/hostCheck'
107-
import { BasicMinimalPluginContext } from './server/pluginContainer'
106+
import {
107+
BasicMinimalPluginContext,
108+
basePluginContextMeta,
109+
} from './server/pluginContainer'
108110

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

13791381
const resolvedConfigContext = new BasicMinimalPluginContext(
13801382
{
1381-
rollupVersion,
1383+
...basePluginContextMeta,
13821384
watchMode:
13831385
(command === 'serve' && !isPreview) ||
13841386
(command === 'build' && !!resolvedBuildOptions.watch),
@@ -2110,7 +2112,7 @@ async function runConfigHook(
21102112
})
21112113
const context = new BasicMinimalPluginContext<
21122114
Omit<PluginContextMeta, 'watchMode'>
2113-
>({ rollupVersion }, tempLogger)
2115+
>(basePluginContextMeta, tempLogger)
21142116

21152117
for (const p of getSortedPluginsByHook('config', plugins)) {
21162118
const hook = p.config
@@ -2133,7 +2135,7 @@ async function runConfigEnvironmentHook(
21332135
): Promise<void> {
21342136
const context = new BasicMinimalPluginContext<
21352137
Omit<PluginContextMeta, 'watchMode'>
2136-
>({ rollupVersion }, logger)
2138+
>(basePluginContextMeta, logger)
21372139

21382140
const environmentNames = Object.keys(environments)
21392141
for (const p of getSortedPluginsByHook('configEnvironment', plugins)) {

packages/vite/src/node/plugin.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export interface PluginContextExtension {
6363
environment: Environment
6464
}
6565

66+
export interface PluginContextMetaExtension {
67+
viteVersion: string
68+
}
69+
6670
export interface ConfigPluginContext
6771
extends Omit<MinimalPluginContext, 'meta' | 'environment'> {
6872
meta: Omit<PluginContextMeta, 'watchMode'>
@@ -74,6 +78,7 @@ export interface MinimalPluginContextWithoutEnvironment
7478
// Augment Rollup types to have the PluginContextExtension
7579
declare module 'rollup' {
7680
export interface MinimalPluginContext extends PluginContextExtension {}
81+
export interface PluginContextMeta extends PluginContextMetaExtension {}
7782
}
7883

7984
/**

packages/vite/src/node/preview.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
getServerUrlByHost,
2929
resolveHostname,
3030
resolveServerUrls,
31-
rollupVersion,
3231
setupSIGTERMListener,
3332
shouldServeFile,
3433
teardownSIGTERMListener,
@@ -41,7 +40,10 @@ import type { InlineConfig, ResolvedConfig } from './config'
4140
import { DEFAULT_PREVIEW_PORT } from './constants'
4241
import type { RequiredExceptFor } from './typeUtils'
4342
import { hostValidationMiddleware } from './server/middlewares/hostCheck'
44-
import { BasicMinimalPluginContext } from './server/pluginContainer'
43+
import {
44+
BasicMinimalPluginContext,
45+
basePluginContextMeta,
46+
} from './server/pluginContainer'
4547
import type { MinimalPluginContextWithoutEnvironment } from './plugin'
4648

4749
export interface PreviewOptions extends CommonServerOptions {}
@@ -195,10 +197,7 @@ export async function preview(
195197

196198
// apply server hooks from plugins
197199
const configurePreviewServerContext = new BasicMinimalPluginContext(
198-
{
199-
rollupVersion,
200-
watchMode: false,
201-
},
200+
{ ...basePluginContextMeta, watchMode: false },
202201
config.logger,
203202
)
204203
const postHooks: ((() => void) | void)[] = []

0 commit comments

Comments
 (0)