mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
feat(nuxt): support parallel plugins (#20460)
This commit is contained in:
parent
8a112e149a
commit
433b52930a
@ -158,6 +158,7 @@ export interface PluginMeta {
|
||||
export interface ResolvedPluginMeta {
|
||||
name?: string
|
||||
order: number
|
||||
parallel?: boolean
|
||||
}
|
||||
|
||||
export interface Plugin<Injections extends Record<string, unknown> = Record<string, unknown>> {
|
||||
@ -169,6 +170,12 @@ export interface Plugin<Injections extends Record<string, unknown> = Record<stri
|
||||
export interface ObjectPluginInput<Injections extends Record<string, unknown> = Record<string, unknown>> extends PluginMeta {
|
||||
hooks?: Partial<RuntimeNuxtHooks>
|
||||
setup?: Plugin<Injections>
|
||||
/**
|
||||
* Execute plugin in parallel with other parallel plugins.
|
||||
*
|
||||
* @default false
|
||||
*/
|
||||
parallel?: boolean
|
||||
}
|
||||
|
||||
export interface CreateOptions {
|
||||
@ -300,10 +307,19 @@ export async function applyPlugin (nuxtApp: NuxtApp, plugin: Plugin) {
|
||||
}
|
||||
|
||||
export async function applyPlugins (nuxtApp: NuxtApp, plugins: Plugin[]) {
|
||||
const parallels: Promise<any>[] = []
|
||||
const errors: Error[] = []
|
||||
for (const plugin of plugins) {
|
||||
await applyPlugin(nuxtApp, plugin)
|
||||
const promise = applyPlugin(nuxtApp, plugin)
|
||||
if (plugin.meta?.parallel) {
|
||||
parallels.push(promise.catch(e => errors.push(e)))
|
||||
} else {
|
||||
await promise
|
||||
}
|
||||
}
|
||||
await Promise.all(parallels)
|
||||
if (errors.length) { throw errors[0] }
|
||||
}
|
||||
|
||||
export function normalizePlugins (_plugins: Plugin[]) {
|
||||
const unwrappedPlugins: Plugin[] = []
|
||||
@ -382,6 +398,7 @@ export function defineNuxtPlugin<T extends Record<string, unknown>> (plugin: Plu
|
||||
|
||||
wrapper.meta = {
|
||||
name: meta?.name || plugin.name || plugin.setup?.name,
|
||||
parallel: plugin.parallel,
|
||||
order:
|
||||
meta?.order ||
|
||||
plugin.order ||
|
||||
|
@ -34,7 +34,7 @@ describe.skipIf(isWindows || process.env.TEST_BUILDER === 'webpack' || process.e
|
||||
|
||||
it('default client bundle size', async () => {
|
||||
stats.client = await analyzeSizes('**/*.js', publicDir)
|
||||
expect(roundToKilobytes(stats.client.totalBytes)).toMatchInlineSnapshot('"97.6k"')
|
||||
expect(roundToKilobytes(stats.client.totalBytes)).toMatchInlineSnapshot('"97.7k"')
|
||||
expect(stats.client.files.map(f => f.replace(/\..*\.js/, '.js'))).toMatchInlineSnapshot(`
|
||||
[
|
||||
"_nuxt/entry.js",
|
||||
@ -45,7 +45,7 @@ describe.skipIf(isWindows || process.env.TEST_BUILDER === 'webpack' || process.e
|
||||
|
||||
it('default server bundle size', async () => {
|
||||
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
|
||||
expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"61.8k"')
|
||||
expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"62.2k"')
|
||||
|
||||
const modules = await analyzeSizes('node_modules/**/*', serverDir)
|
||||
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2283k"')
|
||||
|
Loading…
Reference in New Issue
Block a user