Skip to content

Commit

Permalink
fix: internal TS type errors (#187)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <[email protected]>
  • Loading branch information
sxzz and antfu authored Oct 31, 2022
1 parent 1b93bec commit 20987e9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/rollup/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { RollupPlugin, UnpluginContextMeta, UnpluginFactory, UnpluginInstance, UnpluginOptions } from '../types'
import { toArray } from '../utils'

export function getRollupPlugin <UserOptions = {}>(
factory: UnpluginFactory<UserOptions>,
): UnpluginInstance<UserOptions>['rollup'] {
return (userOptions?: UserOptions) => {
export function getRollupPlugin <UserOptions = {}, Nested extends boolean = boolean>(
factory: UnpluginFactory<UserOptions, Nested>,
) {
return ((userOptions?: UserOptions) => {
const meta: UnpluginContextMeta = {
framework: 'rollup',
}
const rawPlugins = toArray(factory(userOptions!, meta))
const plugins = rawPlugins.map(plugin => toRollupPlugin(plugin))
return plugins.length === 1 ? plugins[0] : plugins
}
}) as UnpluginInstance<UserOptions, Nested>['rollup']
}

export function toRollupPlugin(plugin: UnpluginOptions, containRollupOptions = true): RollupPlugin {
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ export interface ResolvedUnpluginOptions extends UnpluginOptions {
__virtualModulePrefix: string
}

export type UnpluginFactory<UserOptions, Nested extends boolean> = (options: UserOptions, meta: UnpluginContextMeta) =>
export type UnpluginFactory<UserOptions, Nested extends boolean = boolean> = (options: UserOptions, meta: UnpluginContextMeta) =>
Nested extends true
? Array<UnpluginOptions>
: UnpluginOptions
export type UnpluginFactoryOutput<UserOptions, Return> = undefined extends UserOptions
? (options?: UserOptions) => Return
: (options: UserOptions) => Return

export interface UnpluginInstance<UserOptions, Nested extends boolean> {
export interface UnpluginInstance<UserOptions, Nested extends boolean = boolean> {
rollup: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<RollupPlugin> : RollupPlugin>
vite: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<VitePlugin> : VitePlugin>
webpack: UnpluginFactoryOutput<UserOptions, WebpackPluginInstance>
Expand Down
10 changes: 5 additions & 5 deletions src/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { toRollupPlugin } from '../rollup'
import type { UnpluginContextMeta, UnpluginFactory, UnpluginInstance, VitePlugin } from '../types'
import { toArray } from '../utils'

export function getVitePlugin <UserOptions = {}>(
factory: UnpluginFactory<UserOptions>,
): UnpluginInstance<UserOptions>['vite'] {
return (userOptions?: UserOptions) => {
export function getVitePlugin <UserOptions = {}, Nested extends boolean = boolean>(
factory: UnpluginFactory<UserOptions, Nested>,
) {
return ((userOptions?: UserOptions) => {
const meta: UnpluginContextMeta = {
framework: 'vite',
}
Expand All @@ -20,5 +20,5 @@ export function getVitePlugin <UserOptions = {}>(
})

return plugins.length === 1 ? plugins[0] : plugins
}
}) as UnpluginInstance<UserOptions, Nested>['vite']
}

0 comments on commit 20987e9

Please sign in to comment.