fix(nuxt): load layer plugins before project plugins (#22889)

This commit is contained in:
Joaquín Sánchez 2023-09-05 00:41:51 +02:00 committed by GitHub
parent 4e98ac1746
commit 89c4436ae1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 2 deletions

View File

@ -125,11 +125,11 @@ async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
})) }))
} }
// Resolve plugins // Resolve plugins, first extended layers and then base
app.plugins = [ app.plugins = [
...nuxt.options.plugins.map(normalizePlugin) ...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(...[ app.plugins.push(...[
...(config.plugins || []), ...(config.plugins || []),
...config.srcDir ...config.srcDir

View File

@ -983,6 +983,11 @@ describe('extends support', () => {
const html = await $fetch('/foo') const html = await $fetch('/foo')
expect(html).toContain('Plugin | foo: String generated from foo plugin!') 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', () => { describe('server', () => {

View File

@ -0,0 +1,3 @@
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.provide('layerPluginPre', 'layer-plugin')
})

View File

@ -0,0 +1,3 @@
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.provide('layerPluginPost', 'layer-plugin')
})

View File

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