From 433b52930afabd6dda1a6506ee12fc9064396c45 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 16 May 2023 10:50:43 +0200 Subject: [PATCH] feat(nuxt): support parallel plugins (#20460) --- packages/nuxt/src/app/nuxt.ts | 19 ++++++++++++++++++- test/bundle.test.ts | 4 ++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/nuxt/src/app/nuxt.ts b/packages/nuxt/src/app/nuxt.ts index 2148e78c7e..65ba6d991f 100644 --- a/packages/nuxt/src/app/nuxt.ts +++ b/packages/nuxt/src/app/nuxt.ts @@ -158,6 +158,7 @@ export interface PluginMeta { export interface ResolvedPluginMeta { name?: string order: number + parallel?: boolean } export interface Plugin = Record> { @@ -169,6 +170,12 @@ export interface Plugin = Record = Record> extends PluginMeta { hooks?: Partial setup?: Plugin + /** + * Execute plugin in parallel with other parallel plugins. + * + * @default false + */ + parallel?: boolean } export interface CreateOptions { @@ -300,9 +307,18 @@ export async function applyPlugin (nuxtApp: NuxtApp, plugin: Plugin) { } export async function applyPlugins (nuxtApp: NuxtApp, plugins: Plugin[]) { + const parallels: Promise[] = [] + 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[]) { @@ -382,6 +398,7 @@ export function defineNuxtPlugin> (plugin: Plu wrapper.meta = { name: meta?.name || plugin.name || plugin.setup?.name, + parallel: plugin.parallel, order: meta?.order || plugin.order || diff --git a/test/bundle.test.ts b/test/bundle.test.ts index 2b2e2c2b68..f64ef20071 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -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"')