mirror of
https://github.com/unjs/unplugin.git
synced 2025-12-08 20:26:33 +00:00
fix: pass all params for load & transform hooks
This commit is contained in:
parent
93b2af2adc
commit
0b4731208c
@ -12,10 +12,7 @@ export function getRolldownPlugin<UserOptions = Record<string, never>, Nested ex
|
||||
const rawPlugins = toArray(factory(userOptions!, meta))
|
||||
|
||||
const plugins = rawPlugins.map((rawPlugin) => {
|
||||
const plugin = toRollupPlugin(rawPlugin, false) as RolldownPlugin
|
||||
if (rawPlugin.rolldown)
|
||||
Object.assign(plugin, rawPlugin.rolldown)
|
||||
|
||||
const plugin = toRollupPlugin(rawPlugin, 'rolldown') as RolldownPlugin
|
||||
return plugin
|
||||
})
|
||||
|
||||
|
||||
@ -9,34 +9,34 @@ export function getRollupPlugin<UserOptions = Record<string, never>, Nested exte
|
||||
framework: 'rollup',
|
||||
}
|
||||
const rawPlugins = toArray(factory(userOptions!, meta))
|
||||
const plugins = rawPlugins.map(plugin => toRollupPlugin(plugin))
|
||||
const plugins = rawPlugins.map(plugin => toRollupPlugin(plugin, 'rollup'))
|
||||
return plugins.length === 1 ? plugins[0] : plugins
|
||||
}) as UnpluginInstance<UserOptions, Nested>['rollup']
|
||||
}
|
||||
|
||||
export function toRollupPlugin(plugin: UnpluginOptions, containRollupOptions = true): RollupPlugin {
|
||||
export function toRollupPlugin(plugin: UnpluginOptions, key: 'rollup' | 'rolldown' | 'vite' | 'unloader'): RollupPlugin {
|
||||
if (plugin.transform && plugin.transformInclude) {
|
||||
const _transform = plugin.transform
|
||||
plugin.transform = function (code, id) {
|
||||
plugin.transform = function (code, id, ...args) {
|
||||
if (plugin.transformInclude && !plugin.transformInclude(id))
|
||||
return null
|
||||
|
||||
return _transform.call(this, code, id)
|
||||
return _transform.call(this, code, id, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
if (plugin.load && plugin.loadInclude) {
|
||||
const _load = plugin.load
|
||||
plugin.load = function (id) {
|
||||
plugin.load = function (id, ...args) {
|
||||
if (plugin.loadInclude && !plugin.loadInclude(id))
|
||||
return null
|
||||
|
||||
return _load.call(this, id)
|
||||
return _load.call(this, id, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
if (plugin.rollup && containRollupOptions)
|
||||
Object.assign(plugin, plugin.rollup)
|
||||
if (plugin[key])
|
||||
Object.assign(plugin, plugin[key])
|
||||
|
||||
return plugin as RollupPlugin
|
||||
}
|
||||
|
||||
@ -12,10 +12,7 @@ export function getUnloaderPlugin<UserOptions = Record<string, never>, Nested ex
|
||||
const rawPlugins = toArray(factory(userOptions!, meta))
|
||||
|
||||
const plugins = rawPlugins.map((rawPlugin) => {
|
||||
const plugin = toRollupPlugin(rawPlugin, false) as UnloaderPlugin
|
||||
if (rawPlugin.unloader)
|
||||
Object.assign(plugin, rawPlugin.unloader)
|
||||
|
||||
const plugin = toRollupPlugin(rawPlugin, 'unloader') as UnloaderPlugin
|
||||
return plugin
|
||||
})
|
||||
|
||||
|
||||
@ -12,10 +12,7 @@ export function getVitePlugin<UserOptions = Record<string, never>, Nested extend
|
||||
const rawPlugins = toArray(factory(userOptions!, meta))
|
||||
|
||||
const plugins = rawPlugins.map((rawPlugin) => {
|
||||
const plugin = toRollupPlugin(rawPlugin, false) as VitePlugin
|
||||
if (rawPlugin.vite)
|
||||
Object.assign(plugin, rawPlugin.vite)
|
||||
|
||||
const plugin = toRollupPlugin(rawPlugin, 'vite') as VitePlugin
|
||||
return plugin
|
||||
})
|
||||
|
||||
|
||||
@ -54,7 +54,12 @@ function checkHookCalls(
|
||||
const ids = transformIncludeCallback.mock.calls.map(call => call[0])
|
||||
ids.forEach((id) => {
|
||||
expect(path.isAbsolute(id)).toBe(true)
|
||||
expect(transformCallback).toHaveBeenCalledWith(expect.anything(), id)
|
||||
if (name === 'vite') {
|
||||
expect(transformCallback).toHaveBeenCalledWith(expect.anything(), id, expect.anything())
|
||||
}
|
||||
else {
|
||||
expect(transformCallback).toHaveBeenCalledWith(expect.anything(), id)
|
||||
}
|
||||
const isVite = expect.getState().currentTestName?.includes('vite')
|
||||
expect(loadCallback).toHaveBeenCalledWith(
|
||||
id,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user