feat(nuxt): support parallel plugins (#20460)

This commit is contained in:
Anthony Fu 2023-05-16 10:50:43 +02:00 committed by GitHub
parent 8a112e149a
commit 433b52930a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View File

@ -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,9 +307,18 @@ 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[]) {
@ -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 ||

View File

@ -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"')