diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index e0680e271c..459c638a31 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -125,11 +125,11 @@ async function resolveApp (nuxt: Nuxt, app: NuxtApp) { })) } - // Resolve plugins + // Resolve plugins, first extended layers and then base app.plugins = [ ...nuxt.options.plugins.map(normalizePlugin) ] - for (const config of nuxt.options._layers.map(layer => layer.config)) { + for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) { app.plugins.push(...[ ...(config.plugins || []), ...config.srcDir diff --git a/test/basic.test.ts b/test/basic.test.ts index ec0b6ab1be..3e4db35cdc 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -983,6 +983,11 @@ describe('extends support', () => { const html = await $fetch('/foo') expect(html).toContain('Plugin | foo: String generated from foo plugin!') }) + + it('respects plugin ordering within layers', async () => { + const html = await $fetch('/plugins/ordering') + expect(html).toContain('catchall at plugins') + }) }) describe('server', () => { diff --git a/test/fixtures/basic/extends/bar/plugins/09.layer-plugin-pre.ts b/test/fixtures/basic/extends/bar/plugins/09.layer-plugin-pre.ts new file mode 100644 index 0000000000..2b2f5c9f27 --- /dev/null +++ b/test/fixtures/basic/extends/bar/plugins/09.layer-plugin-pre.ts @@ -0,0 +1,3 @@ +export default defineNuxtPlugin((nuxtApp) => { + nuxtApp.provide('layerPluginPre', 'layer-plugin') +}) diff --git a/test/fixtures/basic/extends/bar/plugins/11.layer-plugin-post.ts b/test/fixtures/basic/extends/bar/plugins/11.layer-plugin-post.ts new file mode 100644 index 0000000000..786ea4113f --- /dev/null +++ b/test/fixtures/basic/extends/bar/plugins/11.layer-plugin-post.ts @@ -0,0 +1,3 @@ +export default defineNuxtPlugin((nuxtApp) => { + nuxtApp.provide('layerPluginPost', 'layer-plugin') +}) diff --git a/test/fixtures/basic/plugins/10.layer-ordering.ts b/test/fixtures/basic/plugins/10.layer-ordering.ts new file mode 100644 index 0000000000..04c2e492c6 --- /dev/null +++ b/test/fixtures/basic/plugins/10.layer-ordering.ts @@ -0,0 +1,10 @@ +export default defineNuxtPlugin((nuxtApp) => { + if (useRoute().path === '/plugins/ordering') { + if (!nuxtApp.$layerPluginPre) { + throw createError('layer plugin failed to run before end project plugin') + } + if (!nuxtApp.$layerPluginPost) { + throw createError('layer plugin failed to run before end project plugin') + } + } +})