Skip to content

Commit

Permalink
feat: support this.environment in options and onLog hook (#18142)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Sep 19, 2024
1 parent b529b6f commit 7722c06
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import { mergeConfig } from './publicUtils'
import { webWorkerPostPlugin } from './plugins/worker'
import { getHookHandler } from './plugins'
import { BaseEnvironment } from './baseEnvironment'
import type { Plugin, PluginContext } from './plugin'
import type { MinimalPluginContext, Plugin, PluginContext } from './plugin'
import type { RollupPluginHooks } from './typeUtils'

export interface BuildEnvironmentOptions {
Expand Down Expand Up @@ -1253,7 +1253,7 @@ function wrapEnvironmentHook<HookName extends keyof Plugin>(
}
}

function injectEnvironmentInContext<Context extends PluginContext>(
function injectEnvironmentInContext<Context extends MinimalPluginContext>(
context: Context,
environment: BuildEnvironment,
) {
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { version } = JSON.parse(
)

export const ROLLUP_HOOKS = [
'options',
'buildStart',
'buildEnd',
'renderStart',
Expand All @@ -33,6 +34,7 @@ export const ROLLUP_HOOKS = [
'resolveId',
'shouldTransformCachedModule',
'transform',
'onLog',
] satisfies RollupPluginHooks[]

export const VERSION = version as string
Expand Down
7 changes: 6 additions & 1 deletion packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
LoadResult,
ObjectHook,
ResolveIdResult,
MinimalPluginContext as RollupMinimalPluginContext,
Plugin as RollupPlugin,
PluginContext as RollupPluginContext,
TransformPluginContext as RollupTransformPluginContext,
Expand Down Expand Up @@ -61,6 +62,10 @@ export interface HotUpdatePluginContext {
environment: DevEnvironment
}

export interface MinimalPluginContext
extends RollupMinimalPluginContext,
PluginContextExtension {}

export interface PluginContext
extends RollupPluginContext,
PluginContextExtension {}
Expand All @@ -75,7 +80,7 @@ export interface TransformPluginContext

// Argument Rollup types to have the PluginContextExtension
declare module 'rollup' {
export interface PluginContext extends PluginContextExtension {}
export interface MinimalPluginContext extends PluginContextExtension {}
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class EnvironmentPluginContainer {
warn: noop,
// @ts-expect-error noop
error: noop,
environment,
}
const utils = createPluginHookUtils(plugins)
this.getSortedPlugins = utils.getSortedPlugins
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/typeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
ObjectHook,
MinimalPluginContext as RollupMinimalPluginContext,
Plugin as RollupPlugin,
PluginContext as RollupPluginContext,
} from 'rollup'

export type NonNeverKeys<T> = {
Expand All @@ -11,7 +11,7 @@ export type NonNeverKeys<T> = {
export type GetHookContextMap<Plugin> = {
[K in keyof Plugin]-?: Plugin[K] extends ObjectHook<infer T, unknown>
? T extends (this: infer This, ...args: any[]) => any
? This extends RollupPluginContext
? This extends RollupMinimalPluginContext
? This
: never
: never
Expand Down

0 comments on commit 7722c06

Please sign in to comment.