Skip to content

Commit fbe6361

Browse files
authored
feat: environment in hooks, context vs param (#16261)
1 parent b4e46fe commit fbe6361

File tree

8 files changed

+59
-23
lines changed

8 files changed

+59
-23
lines changed

packages/vite/rollup.dts.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const identifierWithTrailingDollarRE = /\b(\w+)\$\d+\b/g
4848
const identifierReplacements: Record<string, Record<string, string>> = {
4949
rollup: {
5050
Plugin$1: 'rollup.Plugin',
51+
PluginContext$1: 'rollup.PluginContext',
52+
TransformPluginContext$1: 'rollup.TransformPluginContext',
5153
TransformResult$2: 'rollup.TransformResult',
5254
},
5355
esbuild: {

packages/vite/src/node/build.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
RollupLog,
1515
RollupOptions,
1616
RollupOutput,
17+
PluginContext as RollupPluginContext,
1718
RollupWatcher,
1819
WatcherOptions,
1920
} from 'rollup'
@@ -1040,10 +1041,10 @@ function wrapEnvironmentResolveId(
10401041
const fn = getHookHandler(hook)
10411042
const handler: Plugin['resolveId'] = function (id, importer, options) {
10421043
return fn.call(
1043-
this,
1044+
injectEnvironmentInContext(this),
10441045
id,
10451046
importer,
1046-
injectEnvironmentFlag(options, environment),
1047+
injectSsrFlag(options, environment),
10471048
)
10481049
}
10491050

@@ -1065,8 +1066,12 @@ function wrapEnvironmentLoad(
10651066

10661067
const fn = getHookHandler(hook)
10671068
const handler: Plugin['load'] = function (id, ...args) {
1068-
// @ts-expect-error: Receiving options param to be future-proof if Rollup adds it
1069-
return fn.call(this, id, injectEnvironmentFlag(args[0], environment))
1069+
return fn.call(
1070+
injectEnvironmentInContext(this, environment),
1071+
id,
1072+
// @ts-expect-error: Receiving options param to be future-proof if Rollup adds it
1073+
injectSsrFlag(args[0], environment),
1074+
)
10701075
}
10711076

10721077
if ('handler' in hook) {
@@ -1088,11 +1093,11 @@ function wrapEnvironmentTransform(
10881093
const fn = getHookHandler(hook)
10891094
const handler: Plugin['transform'] = function (code, importer, ...args) {
10901095
return fn.call(
1091-
this,
1096+
injectEnvironmentInContext(this, environment),
10921097
code,
10931098
importer,
10941099
// @ts-expect-error: Receiving options param to be future-proof if Rollup adds it
1095-
injectEnvironmentFlag(args[0], environment),
1100+
injectSsrFlag(args[0], environment),
10961101
)
10971102
}
10981103

@@ -1106,14 +1111,27 @@ function wrapEnvironmentTransform(
11061111
}
11071112
}
11081113

1109-
function injectEnvironmentFlag<T extends Record<string, any>>(
1114+
function injectEnvironmentInContext(
1115+
context: RollupPluginContext,
1116+
environment?: BuildEnvironment,
1117+
) {
1118+
return new Proxy(context, {
1119+
get(target, prop, receiver) {
1120+
if (prop === 'environment') {
1121+
return environment
1122+
}
1123+
return Reflect.get(target, prop, receiver)
1124+
},
1125+
})
1126+
}
1127+
1128+
function injectSsrFlag<T extends Record<string, any>>(
11101129
options?: T,
11111130
environment?: BuildEnvironment,
1112-
): T & { ssr?: boolean; environment?: BuildEnvironment } {
1131+
): T & { ssr?: boolean } {
11131132
const ssr = environment ? environment.name !== 'client' : true
1114-
return { ...(options ?? {}), ssr, environment } as T & {
1133+
return { ...(options ?? {}), ssr } as T & {
11151134
ssr?: boolean
1116-
environment?: BuildEnvironment
11171135
}
11181136
}
11191137

packages/vite/src/node/plugin.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import type {
22
CustomPluginOptions,
33
LoadResult,
44
ObjectHook,
5-
PluginContext,
65
ResolveIdResult,
76
Plugin as RollupPlugin,
8-
TransformPluginContext,
7+
PluginContext as RollupPluginContext,
8+
TransformPluginContext as RollupTransformPluginContext,
99
TransformResult,
1010
} from 'rollup'
11-
export type { PluginContext } from 'rollup'
1211
import type {
1312
ConfigEnv,
1413
EnvironmentConfig,
@@ -44,7 +43,19 @@ import type { BuildEnvironment } from './build'
4443
*
4544
* If a plugin should be applied only for server or build, a function format
4645
* config file can be used to conditional determine the plugins to use.
46+
*
47+
* The current module environment can be accessed from the context for the
48+
* buildStart, resolveId, transform, load, and buildEnd, hooks
4749
*/
50+
51+
export interface PluginContext extends RollupPluginContext {
52+
environment?: DevEnvironment | BuildEnvironment
53+
}
54+
55+
export interface TransformPluginContext extends RollupTransformPluginContext {
56+
environment?: DevEnvironment | BuildEnvironment
57+
}
58+
4859
export interface Plugin<A = any> extends RollupPlugin<A> {
4960
/**
5061
* Enforce plugin invocation tier similar to webpack loaders.
@@ -198,8 +209,10 @@ export interface Plugin<A = any> extends RollupPlugin<A> {
198209
options: {
199210
attributes: Record<string, string>
200211
custom?: CustomPluginOptions
212+
/**
213+
* @deprecated use this.environment
214+
*/
201215
ssr?: boolean
202-
environment?: DevEnvironment | BuildEnvironment
203216
/**
204217
* @internal
205218
*/
@@ -213,8 +226,10 @@ export interface Plugin<A = any> extends RollupPlugin<A> {
213226
this: PluginContext,
214227
id: string,
215228
options?: {
229+
/**
230+
* @deprecated use this.environment
231+
*/
216232
ssr?: boolean
217-
environment?: DevEnvironment | BuildEnvironment
218233
},
219234
) => Promise<LoadResult> | LoadResult
220235
>
@@ -224,8 +239,10 @@ export interface Plugin<A = any> extends RollupPlugin<A> {
224239
code: string,
225240
id: string,
226241
options?: {
242+
/**
243+
* @deprecated use this.environment
244+
*/
227245
ssr?: boolean
228-
environment?: DevEnvironment | BuildEnvironment
229246
},
230247
) => Promise<TransformResult> | TransformResult
231248
>

packages/vite/src/node/plugins/asset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
200200

201201
// Inherit HMR timestamp if this asset was invalidated
202202
if (server) {
203-
const environment = options?.environment as DevEnvironment | undefined
203+
const environment = this.environment as DevEnvironment | undefined
204204
const mod = environment?.moduleGraph.getModuleById(id)
205205
if (mod && mod.lastHMRTimestamp > 0) {
206206
url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)

packages/vite/src/node/plugins/css.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ export function cssAnalysisPlugin(config: ResolvedConfig): Plugin {
941941
return
942942
}
943943

944-
const environment = options?.environment
944+
const environment = this.environment
945945
const moduleGraph =
946946
environment?.mode === 'dev' ? environment.moduleGraph : undefined
947947
const thisModule = moduleGraph?.getModuleById(id)

packages/vite/src/node/plugins/importAnalysis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
215215
// running src/node/server/__tests__/pluginContainer.spec.ts
216216

217217
const ssr = options?.ssr === true
218-
const environment = (options?.environment as DevEnvironment) || undefined
218+
const environment = this.environment as DevEnvironment | undefined
219219

220220
if (!server || !environment) {
221221
return null

packages/vite/src/node/plugins/worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
258258
} else if (server) {
259259
// dynamic worker type we can't know how import the env
260260
// so we copy /@vite/env code of server transform result into file header
261-
const environment = options?.environment
261+
const environment = this.environment
262262
const moduleGraph =
263263
environment?.mode === 'dev' ? environment.moduleGraph : undefined
264264
const module = moduleGraph?.getModuleById(ENV_ENTRY)

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,6 @@ export async function createPluginContainer(
731731
custom: options?.custom,
732732
isEntry: !!options?.isEntry,
733733
ssr,
734-
environment,
735734
scan,
736735
}),
737736
)
@@ -786,7 +785,7 @@ export async function createPluginContainer(
786785
ctx._activePlugin = plugin
787786
const handler = getHookHandler(plugin.load)
788787
const result = await handleHookPromise(
789-
handler.call(ctx as any, id, { ssr, environment }),
788+
handler.call(ctx as any, id, { ssr }),
790789
)
791790
if (result != null) {
792791
if (isObject(result)) {
@@ -822,7 +821,7 @@ export async function createPluginContainer(
822821
const handler = getHookHandler(plugin.transform)
823822
try {
824823
result = await handleHookPromise(
825-
handler.call(ctx as any, code, id, { ssr, environment }),
824+
handler.call(ctx as any, code, id, { ssr }),
826825
)
827826
} catch (e) {
828827
ctx.error(e)

0 commit comments

Comments
 (0)