Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support async plugins #8574

Merged
merged 11 commits into from Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
38 changes: 38 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 @@ -106,3 +107,40 @@ 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('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])
})
})
31 changes: 20 additions & 11 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 @@ -66,7 +67,13 @@ export function defineConfig(config: UserConfigExport): UserConfigExport {
return config
}

export type PluginOption = Plugin | false | null | undefined | PluginOption[]
export type PluginOption =
| Plugin
| Promise<Plugin>
bluwy marked this conversation as resolved.
Show resolved Hide resolved
| false
| null
| undefined
| PluginOption[]

export interface UserConfig {
/**
Expand Down Expand Up @@ -330,17 +337,19 @@ export async function resolveConfig(
configEnv.mode = mode

bluwy marked this conversation as resolved.
Show resolved Hide resolved
// resolve plugins
const rawUserPlugins = (config.plugins || []).flat(Infinity).filter((p) => {
if (!p) {
return false
} else if (!p.apply) {
return true
} else if (typeof p.apply === 'function') {
return p.apply({ ...config, mode }, configEnv)
} else {
return p.apply === command
const rawUserPlugins = (await asyncFlatten(config.plugins || [])).filter(
(p: any) => {
bluwy marked this conversation as resolved.
Show resolved Hide resolved
if (!p) {
return false
} else if (!p.apply) {
bluwy marked this conversation as resolved.
Show resolved Hide resolved
return true
} else if (typeof p.apply === 'function') {
return p.apply({ ...config, mode }, configEnv)
} else {
return p.apply === command
}
}
}) as Plugin[]
) as Plugin[]
const [prePlugins, normalPlugins, postPlugins] =
sortUserPlugins(rawUserPlugins)

Expand Down
7 changes: 7 additions & 0 deletions packages/vite/src/node/utils.ts
Expand Up @@ -1018,3 +1018,10 @@ export function transformResult(
map: needSourceMap ? s.generateMap({ hires: true, source: id }) : null
}
}

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
}