Skip to content

Commit

Permalink
fix(types): support required options
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 15, 2022
1 parent 9f5c8a6 commit 53954df
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { UnpluginInstance, UnpluginFactory } from './types'
import { getVitePlugin } from './vite'
import { getWebpackPlugin } from './webpack'

export function createUnplugin<UserOptions = {}> (
export function createUnplugin<UserOptions> (
factory: UnpluginFactory<UserOptions>
): UnpluginInstance<UserOptions> {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function getEsbuildPlugin <UserOptions = {}> (
const meta: UnpluginContextMeta = {
framework: 'esbuild'
}
const plugin = factory(userOptions, meta)
const plugin = factory(userOptions!, meta)

return {
name: plugin.name,
Expand Down
2 changes: 1 addition & 1 deletion src/rollup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function getRollupPlugin <UserOptions = {}> (
const meta: UnpluginContextMeta = {
framework: 'rollup'
}
const rawPlugin = factory(userOptions, meta)
const rawPlugin = factory(userOptions!, meta)
return toRollupPlugin(rawPlugin)
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ export interface ResolvedUnpluginOptions extends UnpluginOptions {
__virtualModulePrefix: string
}

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

export interface UnpluginInstance<UserOptions> {
rollup: (options?: UserOptions) => RollupPlugin;
webpack: (options?: UserOptions) => WebpackPluginInstance;
vite: (options?: UserOptions) => VitePlugin;
esbuild: (options?: UserOptions) => EsbuildPlugin;
rollup: UnpluginFactoryOutput<UserOptions, RollupPlugin>
webpack: UnpluginFactoryOutput<UserOptions, WebpackPluginInstance>
vite: UnpluginFactoryOutput<UserOptions, VitePlugin>
esbuild: UnpluginFactoryOutput<UserOptions, EsbuildPlugin>
raw: UnpluginFactory<UserOptions>
}

Expand Down
2 changes: 1 addition & 1 deletion src/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function getVitePlugin <UserOptions = {}> (
const meta: UnpluginContextMeta = {
framework: 'vite'
}
const rawPlugin = factory(userOptions, meta)
const rawPlugin = factory(userOptions!, meta)

const plugin = toRollupPlugin(rawPlugin, false) as VitePlugin

Expand Down
2 changes: 1 addition & 1 deletion src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function getWebpackPlugin<UserOptions = {}> (
}
}

const rawPlugin = factory(userOptions, meta)
const rawPlugin = factory(userOptions!, meta)
const plugin = Object.assign(
rawPlugin,
{
Expand Down

0 comments on commit 53954df

Please sign in to comment.