Skip to content
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
41 changes: 41 additions & 0 deletions packages/vite/src/node/__tests__/plugins/hooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ const transformIndexHtmlWithPlugin = async (plugin: Plugin) => {
await server.transformIndexHtml('/', '<html></html>')
}

const transformRequestWithPlugin = async (plugin: Plugin) => {
const server = await createServerWithPlugin(plugin)
await server.environments.client.transformRequest(ENTRY_ID)
}

describe('supports plugin context', () => {
test('config hook', async () => {
expect.assertions(4)
Expand Down Expand Up @@ -700,6 +705,9 @@ describe('plugin hook errors', () => {
['buildStart', createServerWithPlugin],
['buildApp', buildAppWithPlugin],
['transformIndexHtml', transformIndexHtmlWithPlugin],
['resolveId', transformRequestWithPlugin],
['load', transformRequestWithPlugin],
['transform', transformRequestWithPlugin],
] as const) {
test(`${hook} hook error has the plugin and hook`, async () => {
const plugin = {
Expand Down Expand Up @@ -768,4 +776,37 @@ describe('plugin hook errors', () => {
}),
).rejects.toBe('oops')
})

test('error has the new plugin name after a rename', async () => {
let fail = false
const plugin: Plugin = {
name: 'before',
resolveId() {
if (fail) throw new Error('missing apiKey')
},
}
const server = await createServerWithPlugin(plugin)
const { pluginContainer } = server.environments.client
await pluginContainer.resolveId('id')

plugin.name = 'after'
fail = true

await expect(pluginContainer.resolveId('id')).rejects.toMatchObject({
plugin: 'after',
})
})
})

test('runs the new hook after a plugin replaces it', async () => {
const plugin: Plugin = { name: 'replaces', resolveId: () => '/first' }
const server = await createServerWithPlugin(plugin)
const { pluginContainer } = server.environments.client
await pluginContainer.resolveId('id')

plugin.resolveId = () => '/second'

expect(await pluginContainer.resolveId('id')).toMatchObject({
id: '/second',
})
})
38 changes: 33 additions & 5 deletions packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,43 @@ export function getHookHandler<T extends ObjectHook<Function>>(
return (typeof hook === 'object' ? hook.handler : hook) as HookHandler<T>
}

interface TaggedHandler {
handler: Function
tagged: Function
}

const taggedHandlers = new WeakMap<Plugin, Map<keyof Plugin, TaggedHandler>>()

export function getPluginHookHandler<K extends keyof Plugin>(
plugin: Plugin,
hookName: K,
): NonNullable<HookHandler<Plugin[K]>> {
const handler: ((this: unknown, ...args: unknown[]) => unknown) | undefined =
getHookHandler(plugin[hookName])
if (!handler) return handler as never
const handler = getHookHandler(plugin[hookName])
if (!handler) return handler

@materwelonDhruv materwelonDhruv Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. idk why i didn't try remvoing the annotation from before 🤦🏻‍♂️


let handlers = taggedHandlers.get(plugin)
if (!handlers) {
handlers = new Map()
taggedHandlers.set(plugin, handlers)
}
let cached = handlers.get(hookName)
if (cached?.handler !== handler) {
cached = {
handler,
tagged: tagHandlerErrors(handler, plugin, hookName),
}
handlers.set(hookName, cached)
}
return cached.tagged as NonNullable<HookHandler<Plugin[K]>>
}

function tagHandlerErrors(
handler: (this: unknown, ...args: unknown[]) => unknown,
plugin: Plugin,
hook: string,
) {
const rethrowTagged = (e: unknown): never => {
tagPluginError(e, plugin.name, hookName)
tagPluginError(e, plugin.name, hook)
throw e
}
return function (this: unknown, ...args: unknown[]) {
Expand All @@ -240,7 +268,7 @@ export function getPluginHookHandler<K extends keyof Plugin>(
} catch (e) {
rethrowTagged(e)
}
} as NonNullable<HookHandler<Plugin[K]>>
}
}

function tagPluginError(e: unknown, plugin: string, hook: string): void {
Expand Down
7 changes: 3 additions & 4 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ import type { Plugin } from '../plugin'
import {
createPluginHookUtils,
getCachedFilterForPlugin,
getHookHandler,
getPluginHookHandler,
} from '../plugins'
import {
Expand Down Expand Up @@ -436,7 +435,7 @@ class EnvironmentPluginContainer<Env extends Environment = Environment> {
}

const pluginResolveStart = debugPluginResolve ? performance.now() : 0
const handler = getHookHandler(plugin.resolveId)
const handler = getPluginHookHandler(plugin, 'resolveId')
const result = await this.handleHookPromise(
handler.call(ctx as any, rawId, importer, normalizedOptions),
)
Expand Down Expand Up @@ -523,7 +522,7 @@ class EnvironmentPluginContainer<Env extends Environment = Environment> {
})
}

const handler = getHookHandler(plugin.load)
const handler = getPluginHookHandler(plugin, 'load')
const result = await this.handleHookPromise(
handler.call(ctx as any, id, options),
)
Expand Down Expand Up @@ -597,7 +596,7 @@ class EnvironmentPluginContainer<Env extends Environment = Environment> {
ctx._updateActiveInfo(plugin, id, code)
const start = debugPluginTransform ? performance.now() : 0
let result: TransformResult | string | undefined
const handler = getHookHandler(plugin.transform)
const handler = getPluginHookHandler(plugin, 'transform')
try {
result = await this.handleHookPromise(
handler.call(ctx as any, code, id, optionsWithSSR),
Expand Down
Loading