Skip to content

Commit

Permalink
feat: support async plugins (#8574)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jun 18, 2022
1 parent 14db473 commit caa8a58
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/config/shared-options.md
Expand Up @@ -77,9 +77,9 @@ const obj = {

## plugins

- **Type:** `(Plugin | Plugin[])[]`
- **Type:** `(Plugin | Plugin[] | Promise<Plugin | Plugin[]>)[]`

Array of plugins to use. Falsy plugins are ignored and arrays of plugins are flattened. See [Plugin API](/guide/api-plugin) for more details on Vite plugins.
Array of plugins to use. Falsy plugins are ignored and arrays of plugins are flattened. If a promise is returned, it would be resolved before running. See [Plugin API](/guide/api-plugin) for more details on Vite plugins.

## publicDir

Expand Down
43 changes: 43 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
@@ -1,5 +1,6 @@
import { describe, expect, test } from 'vitest'
import {
asyncFlatten,
getHash,
getPotentialTsSrcPaths,
injectQuery,
Expand Down Expand Up @@ -127,3 +128,45 @@ describe('getHash', () => {
expect(hash).toMatch(/^[\da-f]{8}$/)
})
})

describe('asyncFlatten', () => {
test('plain array', async () => {
const arr = await asyncFlatten([1, 2, 3])
expect(arr).toEqual([1, 2, 3])
})

test('nested array', async () => {
const arr = await asyncFlatten([1, 2, 3, [4, 5, 6]])
expect(arr).toEqual([1, 2, 3, 4, 5, 6])
})

test('nested falsy array', async () => {
const arr = await asyncFlatten([1, 2, false, [4, null, undefined]])
expect(arr).toEqual([1, 2, false, 4, null, undefined])
})

test('plain promise array', async () => {
const arr = await asyncFlatten([1, 2, Promise.resolve(3)])
expect(arr).toEqual([1, 2, 3])
})

test('nested promise array', async () => {
const arr = await asyncFlatten([
1,
2,
Promise.resolve(3),
Promise.resolve([4, 5, 6])
])
expect(arr).toEqual([1, 2, 3, 4, 5, 6])
})

test('2x nested promise array', async () => {
const arr = await asyncFlatten([
1,
2,
Promise.resolve(3),
Promise.resolve([4, 5, Promise.resolve(6), Promise.resolve([7, 8, 9])])
])
expect(arr).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9])
})
})
15 changes: 12 additions & 3 deletions packages/vite/src/node/config.ts
Expand Up @@ -17,6 +17,7 @@ import type { PreviewOptions, ResolvedPreviewOptions } from './preview'
import { resolvePreviewOptions } from './preview'
import type { CSSOptions } from './plugins/css'
import {
asyncFlatten,
createDebugger,
createFilter,
dynamicImport,
Expand Down Expand Up @@ -75,7 +76,13 @@ export function defineConfig(config: UserConfigExport): UserConfigExport {
return config
}

export type PluginOption = Plugin | false | null | undefined | PluginOption[]
export type PluginOption =
| Plugin
| false
| null
| undefined
| PluginOption[]
| Promise<Plugin | false | null | undefined | PluginOption[]>

export interface UserConfig {
/**
Expand Down Expand Up @@ -370,7 +377,9 @@ export async function resolveConfig(
configEnv.mode = mode

// resolve plugins
const rawUserPlugins = (config.plugins || []).flat(Infinity).filter((p) => {
const rawUserPlugins = (
(await asyncFlatten(config.plugins || [])) as Plugin[]
).filter((p) => {
if (!p) {
return false
} else if (!p.apply) {
Expand All @@ -380,7 +389,7 @@ export async function resolveConfig(
} else {
return p.apply === command
}
}) as Plugin[]
})
const [prePlugins, normalPlugins, postPlugins] =
sortUserPlugins(rawUserPlugins)

Expand Down
7 changes: 7 additions & 0 deletions packages/vite/src/node/utils.ts
Expand Up @@ -1015,6 +1015,13 @@ export function transformResult(
}
}

export async function asyncFlatten<T>(arr: T[]): Promise<T[]> {
do {
arr = (await Promise.all(arr)).flat(Infinity) as any
} while (arr.some((v: any) => v?.then))
return arr
}

// strip UTF-8 BOM
export function stripBomTag(content: string): string {
if (content.charCodeAt(0) === 0xfeff) {
Expand Down

0 comments on commit caa8a58

Please sign in to comment.