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 { export interface ResolvedPluginMeta {
name?: string name?: string
order: number order: number
parallel?: boolean
} }
export interface Plugin<Injections extends Record<string, unknown> = Record<string, unknown>> { 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 { export interface ObjectPluginInput<Injections extends Record<string, unknown> = Record<string, unknown>> extends PluginMeta {
hooks?: Partial<RuntimeNuxtHooks> hooks?: Partial<RuntimeNuxtHooks>
setup?: Plugin<Injections> setup?: Plugin<Injections>
/**
* Execute plugin in parallel with other parallel plugins.
*
* @default false
*/
parallel?: boolean
} }
export interface CreateOptions { export interface CreateOptions {
@ -300,9 +307,18 @@ export async function applyPlugin (nuxtApp: NuxtApp, plugin: Plugin) {
} }
export async function applyPlugins (nuxtApp: NuxtApp, plugins: Plugin[]) { export async function applyPlugins (nuxtApp: NuxtApp, plugins: Plugin[]) {
const parallels: Promise<any>[] = []
const errors: Error[] = []
for (const plugin of plugins) { 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[]) { export function normalizePlugins (_plugins: Plugin[]) {
@ -382,6 +398,7 @@ export function defineNuxtPlugin<T extends Record<string, unknown>> (plugin: Plu
wrapper.meta = { wrapper.meta = {
name: meta?.name || plugin.name || plugin.setup?.name, name: meta?.name || plugin.name || plugin.setup?.name,
parallel: plugin.parallel,
order: order:
meta?.order || meta?.order ||
plugin.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 () => { it('default client bundle size', async () => {
stats.client = await analyzeSizes('**/*.js', publicDir) 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(` expect(stats.client.files.map(f => f.replace(/\..*\.js/, '.js'))).toMatchInlineSnapshot(`
[ [
"_nuxt/entry.js", "_nuxt/entry.js",
@ -45,7 +45,7 @@ describe.skipIf(isWindows || process.env.TEST_BUILDER === 'webpack' || process.e
it('default server bundle size', async () => { it('default server bundle size', async () => {
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir) 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) const modules = await analyzeSizes('node_modules/**/*', serverDir)
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2283k"') expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2283k"')