Skip to content

Commit dc05040

Browse files
committedOct 25, 2022
feat(types): improve return types
1 parent 2b11358 commit dc05040

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed
 

‎src/define.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
/* eslint-disable no-redeclare */
12
import { getEsbuildPlugin } from './esbuild'
23
import { getRollupPlugin } from './rollup'
34
import { UnpluginInstance, UnpluginFactory } from './types'
45
import { getVitePlugin } from './vite'
56
import { getWebpackPlugin } from './webpack'
67

78
export function createUnplugin<UserOptions> (
8-
factory: UnpluginFactory<UserOptions>
9-
): UnpluginInstance<UserOptions> {
9+
factory: UnpluginFactory<UserOptions, false>
10+
): UnpluginInstance<UserOptions, false>
11+
export function createUnplugin<UserOptions> (
12+
factory: UnpluginFactory<UserOptions, true>
13+
): UnpluginInstance<UserOptions, true>
14+
export function createUnplugin<UserOptions, Nested extends boolean> (
15+
factory: UnpluginFactory<UserOptions, Nested>
16+
): UnpluginInstance<UserOptions, Nested> {
1017
return {
1118
get esbuild () {
1219
return getEsbuildPlugin(factory)

‎src/types.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { Compiler as WebpackCompiler, WebpackPluginInstance } from 'webpack
33
import type { Plugin as VitePlugin } from 'vite'
44
import type { Plugin as EsbuildPlugin } from 'esbuild'
55
import type VirtualModulesPlugin from 'webpack-virtual-modules'
6-
import type { Arrayable } from './utils'
76

87
export {
98
EsbuildPlugin,
@@ -70,18 +69,20 @@ export interface ResolvedUnpluginOptions extends UnpluginOptions {
7069
__virtualModulePrefix: string
7170
}
7271

73-
export type UnpluginFactory<UserOptions> = (options: UserOptions, meta: UnpluginContextMeta) =>
74-
Arrayable<UnpluginOptions>
72+
export type UnpluginFactory<UserOptions, Nested extends boolean> = (options: UserOptions, meta: UnpluginContextMeta) =>
73+
Nested extends true
74+
? Array<UnpluginOptions>
75+
: UnpluginOptions
7576
export type UnpluginFactoryOutput<UserOptions, Return> = undefined extends UserOptions
7677
? (options?: UserOptions) => Return
7778
: (options: UserOptions) => Return
7879

79-
export interface UnpluginInstance<UserOptions> {
80-
rollup: UnpluginFactoryOutput<UserOptions, Arrayable<RollupPlugin>>
81-
vite: UnpluginFactoryOutput<UserOptions, Arrayable<VitePlugin>>
80+
export interface UnpluginInstance<UserOptions, Nested extends boolean> {
81+
rollup: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<RollupPlugin> : RollupPlugin>
82+
vite: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<VitePlugin> : VitePlugin>
8283
webpack: UnpluginFactoryOutput<UserOptions, WebpackPluginInstance>
8384
esbuild: UnpluginFactoryOutput<UserOptions, EsbuildPlugin>
84-
raw: UnpluginFactory<UserOptions>
85+
raw: UnpluginFactory<UserOptions, Nested>
8586
}
8687

8788
export type UnpluginContextMeta = Partial<RollupContextMeta> & ({

0 commit comments

Comments
 (0)
Please sign in to comment.