mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +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 {
|
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 ||
|
||||||
|
@ -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"')
|
||||||
|
Loading…
Reference in New Issue
Block a user