Skip to content

Commit 95927d9

Browse files
authored
feat: implement removePluginHookSsrArgument future deprecation (vitejs#20433)
1 parent f20addc commit 95927d9

File tree

3 files changed

+141
-22
lines changed

3 files changed

+141
-22
lines changed

packages/vite/src/node/build.ts

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ import {
7575
BasicMinimalPluginContext,
7676
basePluginContextMeta,
7777
} from './server/pluginContainer'
78+
import {
79+
isFutureDeprecationEnabled,
80+
warnFutureDeprecation,
81+
} from './deprecations'
7882
import { prepareOutDirPlugin } from './plugins/prepareOutDir'
7983
import type { Environment } from './environment'
8084

@@ -1086,13 +1090,21 @@ export function injectEnvironmentToHooks(
10861090
for (const hook of Object.keys(clone) as RollupPluginHooks[]) {
10871091
switch (hook) {
10881092
case 'resolveId':
1089-
clone[hook] = wrapEnvironmentResolveId(environment, resolveId)
1093+
clone[hook] = wrapEnvironmentResolveId(
1094+
environment,
1095+
resolveId,
1096+
plugin.name,
1097+
)
10901098
break
10911099
case 'load':
1092-
clone[hook] = wrapEnvironmentLoad(environment, load)
1100+
clone[hook] = wrapEnvironmentLoad(environment, load, plugin.name)
10931101
break
10941102
case 'transform':
1095-
clone[hook] = wrapEnvironmentTransform(environment, transform)
1103+
clone[hook] = wrapEnvironmentTransform(
1104+
environment,
1105+
transform,
1106+
plugin.name,
1107+
)
10961108
break
10971109
default:
10981110
if (ROLLUP_HOOKS.includes(hook)) {
@@ -1107,7 +1119,8 @@ export function injectEnvironmentToHooks(
11071119

11081120
function wrapEnvironmentResolveId(
11091121
environment: Environment,
1110-
hook?: Plugin['resolveId'],
1122+
hook: Plugin['resolveId'] | undefined,
1123+
pluginName: string,
11111124
): Plugin['resolveId'] {
11121125
if (!hook) return
11131126

@@ -1117,7 +1130,7 @@ function wrapEnvironmentResolveId(
11171130
injectEnvironmentInContext(this, environment),
11181131
id,
11191132
importer,
1120-
injectSsrFlag(options, environment),
1133+
injectSsrFlag(options, environment, pluginName),
11211134
)
11221135
}
11231136

@@ -1133,7 +1146,8 @@ function wrapEnvironmentResolveId(
11331146

11341147
function wrapEnvironmentLoad(
11351148
environment: Environment,
1136-
hook?: Plugin['load'],
1149+
hook: Plugin['load'] | undefined,
1150+
pluginName: string,
11371151
): Plugin['load'] {
11381152
if (!hook) return
11391153

@@ -1142,7 +1156,7 @@ function wrapEnvironmentLoad(
11421156
return fn.call(
11431157
injectEnvironmentInContext(this, environment),
11441158
id,
1145-
injectSsrFlag(args[0], environment),
1159+
injectSsrFlag(args[0], environment, pluginName),
11461160
)
11471161
}
11481162

@@ -1158,7 +1172,8 @@ function wrapEnvironmentLoad(
11581172

11591173
function wrapEnvironmentTransform(
11601174
environment: Environment,
1161-
hook?: Plugin['transform'],
1175+
hook: Plugin['transform'] | undefined,
1176+
pluginName: string,
11621177
): Plugin['transform'] {
11631178
if (!hook) return
11641179

@@ -1168,7 +1183,7 @@ function wrapEnvironmentTransform(
11681183
injectEnvironmentInContext(this, environment),
11691184
code,
11701185
importer,
1171-
injectSsrFlag(args[0], environment),
1186+
injectSsrFlag(args[0], environment, pluginName),
11721187
)
11731188
}
11741189

@@ -1218,13 +1233,37 @@ function injectEnvironmentInContext<Context extends MinimalPluginContext>(
12181233
}
12191234

12201235
function injectSsrFlag<T extends Record<string, any>>(
1221-
options?: T,
1222-
environment?: Environment,
1236+
options: T | undefined,
1237+
environment: Environment,
1238+
pluginName: string,
12231239
): T & { ssr?: boolean } {
1224-
const ssr = environment ? environment.config.consumer === 'server' : true
1225-
return { ...(options ?? {}), ssr } as T & {
1240+
let ssr = environment.config.consumer === 'server'
1241+
const newOptions = { ...(options ?? {}), ssr } as T & {
12261242
ssr?: boolean
12271243
}
1244+
1245+
if (
1246+
isFutureDeprecationEnabled(
1247+
environment?.getTopLevelConfig(),
1248+
'removePluginHookSsrArgument',
1249+
)
1250+
) {
1251+
Object.defineProperty(newOptions, 'ssr', {
1252+
get() {
1253+
warnFutureDeprecation(
1254+
environment?.getTopLevelConfig(),
1255+
'removePluginHookSsrArgument',
1256+
`Used in plugin "${pluginName}".`,
1257+
)
1258+
return ssr
1259+
},
1260+
set(v) {
1261+
ssr = v
1262+
},
1263+
})
1264+
}
1265+
1266+
return newOptions
12281267
}
12291268

12301269
/*

packages/vite/src/node/deprecations.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ const deprecationMessages = {
4141

4242
let _ignoreDeprecationWarnings = false
4343

44+
export function isFutureDeprecationEnabled(
45+
config: ResolvedConfig,
46+
type: keyof FutureOptions,
47+
): boolean {
48+
return !!config.future?.[type]
49+
}
50+
4451
// Later we could have a `warnDeprecation` utils when the deprecation is landed
4552
/**
4653
* Warn about future deprecations.

packages/vite/src/node/server/pluginContainer.ts

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ import { cleanUrl, unwrapId } from '../../shared/utils'
8989
import type { PluginHookUtils } from '../config'
9090
import type { Environment } from '../environment'
9191
import type { Logger } from '../logger'
92+
import {
93+
isFutureDeprecationEnabled,
94+
warnFutureDeprecation,
95+
} from '../deprecations'
9296
import type { DevEnvironment } from './environment'
9397
import { buildErrorMessage } from './middlewares/error'
9498
import type {
@@ -369,6 +373,7 @@ class EnvironmentPluginContainer<Env extends Environment = Environment> {
369373
const scan = !!options?.scan
370374
const ssr = this.environment.config.consumer === 'server'
371375
const ctx = new ResolveIdContext(this, skip, skipCalls, scan)
376+
const topLevelConfig = this.environment.getTopLevelConfig()
372377

373378
const mergedSkip = new Set<Plugin>(skip)
374379
for (const call of skipCalls ?? []) {
@@ -390,16 +395,39 @@ class EnvironmentPluginContainer<Env extends Environment = Environment> {
390395

391396
ctx._plugin = plugin
392397

398+
const normalizedOptions = {
399+
attributes: options?.attributes ?? {},
400+
custom: options?.custom,
401+
isEntry: !!options?.isEntry,
402+
ssr,
403+
scan,
404+
}
405+
if (
406+
isFutureDeprecationEnabled(
407+
topLevelConfig,
408+
'removePluginHookSsrArgument',
409+
)
410+
) {
411+
let ssrTemp = ssr
412+
Object.defineProperty(normalizedOptions, 'ssr', {
413+
get() {
414+
warnFutureDeprecation(
415+
topLevelConfig,
416+
'removePluginHookSsrArgument',
417+
`Used in plugin "${plugin.name}".`,
418+
)
419+
return ssrTemp
420+
},
421+
set(v) {
422+
ssrTemp = v
423+
},
424+
})
425+
}
426+
393427
const pluginResolveStart = debugPluginResolve ? performance.now() : 0
394428
const handler = getHookHandler(plugin.resolveId)
395429
const result = await this.handleHookPromise(
396-
handler.call(ctx as any, rawId, importer, {
397-
attributes: options?.attributes ?? {},
398-
custom: options?.custom,
399-
isEntry: !!options?.isEntry,
400-
ssr,
401-
scan,
402-
}),
430+
handler.call(ctx as any, rawId, importer, normalizedOptions),
403431
)
404432
if (!result) continue
405433

@@ -442,7 +470,8 @@ class EnvironmentPluginContainer<Env extends Environment = Environment> {
442470
}
443471

444472
async load(id: string): Promise<LoadResult | null> {
445-
const ssr = this.environment.config.consumer === 'server'
473+
let ssr = this.environment.config.consumer === 'server'
474+
const topLevelConfig = this.environment.getTopLevelConfig()
446475
const options = { ssr }
447476
const ctx = new LoadPluginContext(this)
448477
for (const plugin of this.getSortedPlugins('load')) {
@@ -453,6 +482,28 @@ class EnvironmentPluginContainer<Env extends Environment = Environment> {
453482
if (filter && !filter(id)) continue
454483

455484
ctx._plugin = plugin
485+
486+
if (
487+
isFutureDeprecationEnabled(
488+
topLevelConfig,
489+
'removePluginHookSsrArgument',
490+
)
491+
) {
492+
Object.defineProperty(options, 'ssr', {
493+
get() {
494+
warnFutureDeprecation(
495+
topLevelConfig,
496+
'removePluginHookSsrArgument',
497+
`Used in plugin "${plugin.name}".`,
498+
)
499+
return ssr
500+
},
501+
set(v) {
502+
ssr = v
503+
},
504+
})
505+
}
506+
456507
const handler = getHookHandler(plugin.load)
457508
const result = await this.handleHookPromise(
458509
handler.call(ctx as any, id, options),
@@ -476,7 +527,8 @@ class EnvironmentPluginContainer<Env extends Environment = Environment> {
476527
inMap?: SourceDescription['map']
477528
},
478529
): Promise<{ code: string; map: SourceMap | { mappings: '' } | null }> {
479-
const ssr = this.environment.config.consumer === 'server'
530+
let ssr = this.environment.config.consumer === 'server'
531+
const topLevelConfig = this.environment.getTopLevelConfig()
480532
const optionsWithSSR = options ? { ...options, ssr } : { ssr }
481533
const inMap = options?.inMap
482534

@@ -490,6 +542,27 @@ class EnvironmentPluginContainer<Env extends Environment = Environment> {
490542
const filter = getCachedFilterForPlugin(plugin, 'transform')
491543
if (filter && !filter(id, code)) continue
492544

545+
if (
546+
isFutureDeprecationEnabled(
547+
topLevelConfig,
548+
'removePluginHookSsrArgument',
549+
)
550+
) {
551+
Object.defineProperty(optionsWithSSR, 'ssr', {
552+
get() {
553+
warnFutureDeprecation(
554+
topLevelConfig,
555+
'removePluginHookSsrArgument',
556+
`Used in plugin "${plugin.name}".`,
557+
)
558+
return ssr
559+
},
560+
set(v) {
561+
ssr = v
562+
},
563+
})
564+
}
565+
493566
ctx._updateActiveInfo(plugin, id, code)
494567
const start = debugPluginTransform ? performance.now() : 0
495568
let result: TransformResult | string | undefined

0 commit comments

Comments
 (0)