From 89c4436ae1f9679ba3f23ee275531d4b7ff225c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Tue, 5 Sep 2023 00:41:51 +0200 Subject: [PATCH] fix(nuxt): load layer plugins before project plugins (#22889) --- packages/nuxt/src/core/app.ts | 4 ++-- test/basic.test.ts | 5 +++++ .../basic/extends/bar/plugins/09.layer-plugin-pre.ts | 3 +++ .../basic/extends/bar/plugins/11.layer-plugin-post.ts | 3 +++ test/fixtures/basic/plugins/10.layer-ordering.ts | 10 ++++++++++ 5 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/basic/extends/bar/plugins/09.layer-plugin-pre.ts create mode 100644 test/fixtures/basic/extends/bar/plugins/11.layer-plugin-post.ts create mode 100644 test/fixtures/basic/plugins/10.layer-ordering.ts 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') + } + } +})