Skip to content

Commit

Permalink
feat: support define function config
Browse files Browse the repository at this point in the history
  • Loading branch information
ntnyq committed Mar 13, 2024
1 parent b5219be commit 46c899f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
21 changes: 20 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import type { Awaitable } from './utility-types'
import type { TaroConfig } from './config'

export interface TaroConfigEnv {
/**
* 构建模式
*/
mode: string

/**
* 构建命令
*/
command: string
}

export type WebpackMerge = (...configs: Array<object | null | undefined>) => object

export type TaroConfigFn = (merge: WebpackMerge, env: TaroConfigEnv) => Awaitable<TaroConfig>

export type TaroConfigExport = Awaitable<TaroConfig> | TaroConfigFn

/**
* Define a Taro config
*
* @param config Taro config
* @returns Taro config
*/
export function defineConfig(config: TaroConfig): TaroConfig {
export function defineConfig(config: TaroConfigExport): TaroConfigExport {
return config
}

Expand Down
22 changes: 15 additions & 7 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectTypeOf, it } from 'vitest'
import { defineConfig } from 'taro-define-config'
import type { TaroConfig } from 'taro-define-config'
import type { TaroConfigExport } from 'taro-define-config'

declare module 'taro-define-config' {
export interface CustomPluginOptionsMap {
Expand All @@ -15,8 +15,16 @@ const getConfigurablePluginConfig = () => ({
config: {},
})

it('define empty config', () => {
expectTypeOf(defineConfig({})).toEqualTypeOf<TaroConfig>()
it('define object config', () => {
expectTypeOf(defineConfig({})).toEqualTypeOf<TaroConfigExport>()
})

it('define function config', () => {
expectTypeOf(defineConfig(() => ({}))).toEqualTypeOf<TaroConfigExport>()
})

it('define async function config', () => {
expectTypeOf(defineConfig(async () => ({}))).toEqualTypeOf<TaroConfigExport>()
})

it('define base config', () => {
Expand Down Expand Up @@ -59,7 +67,7 @@ it('define base config', () => {
rn: {},
mini: {},
}),
).toMatchTypeOf<TaroConfig>()
).toMatchTypeOf<TaroConfigExport>()
})

it('define platform h5 config', () => {
Expand Down Expand Up @@ -100,7 +108,7 @@ it('define platform h5 config', () => {
fontUrlLoaderOption: {},
},
}),
).toMatchTypeOf<TaroConfig>()
).toMatchTypeOf<TaroConfigExport>()
})

it('define platform rn config', () => {
Expand All @@ -127,7 +135,7 @@ it('define platform rn config', () => {
enableSvgTransform: true,
},
}),
).toMatchTypeOf<TaroConfig>()
).toMatchTypeOf<TaroConfigExport>()
})

it('define platform mini config', () => {
Expand Down Expand Up @@ -160,5 +168,5 @@ it('define platform mini config', () => {
fontUrlLoaderOption: {},
},
}),
).toMatchTypeOf<TaroConfig>()
).toMatchTypeOf<TaroConfigExport>()
})

0 comments on commit 46c899f

Please sign in to comment.