From 3e83c09b97c143de3a8423c08102c7a88dfd2733 Mon Sep 17 00:00:00 2001 From: userquin Date: Thu, 31 Aug 2023 19:36:40 +0200 Subject: [PATCH] chore: update plugin sorting algorithm + update test --- packages/nuxt/src/core/app.ts | 38 +++++++------------ .../basic/plugins/10.layer-ordering.ts | 4 +- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index 0d51542f88..4b7f249e77 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -126,31 +126,21 @@ async function resolveApp (nuxt: Nuxt, app: NuxtApp) { } // Re-initialise plugins - app.plugins = nuxt.options.plugins.map(normalizePlugin) - - // Track scanned plugins separately so we can apply a sorting mechanism to them - const scannedPlugins: [string, NuxtPlugin][] = [] - for (const config of nuxt.options._layers.map(layer => layer.config)) { - for (const plugin of config.plugins || []) { - app.plugins.push(normalizePlugin(plugin as NuxtPlugin | string)) - } - - if (config.srcDir) { - const pluginDir = join(config.srcDir, config.dir?.plugins || 'plugins') - for (const file of await resolveFiles(pluginDir, [ - '*.{ts,js,mjs,cjs,mts,cts}', - '*/index.*{ts,js,mjs,cjs,mts,cts}' // TODO: remove, only scan top-level plugins #18418 - ])) { - scannedPlugins.push([file.replace(pluginDir, ''), normalizePlugin(file)]) - } - } - } - - // Sort scanned plugins by their names so a scanned `01.plugin.ts` will always run before finished `02.plugin.ts` - // and append to the plugin array - for (const [_name, plugin] of scannedPlugins.sort(([a], [b]) => a.localeCompare(b))) { - app.plugins.push(plugin) + app.plugins = [] + let idx = 0 + // Sort Plugins: layers plugins first then project plugins + for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) { + app.plugins[idx++ === (nuxt.options._layers.length - 1) ? 'push' : 'unshift'](...[ + ...(config.plugins || []), + ...config.srcDir + ? await resolveFiles(config.srcDir, [ + `${config.dir?.plugins || 'plugins'}/*.{ts,js,mjs,cjs,mts,cts}`, + `${config.dir?.plugins || 'plugins'}/*/index.*{ts,js,mjs,cjs,mts,cts}` // TODO: remove, only scan top-level plugins #18418 + ]) + : [] + ].map(plugin => normalizePlugin(plugin as NuxtPlugin))) } + app.plugins.unshift(...nuxt.options.plugins.map(normalizePlugin)) // Normalize and de-duplicate plugins and middleware app.middleware = uniqueBy(await resolvePaths(app.middleware, 'path'), 'name') diff --git a/test/fixtures/basic/plugins/10.layer-ordering.ts b/test/fixtures/basic/plugins/10.layer-ordering.ts index fd71d524a4..04c2e492c6 100644 --- a/test/fixtures/basic/plugins/10.layer-ordering.ts +++ b/test/fixtures/basic/plugins/10.layer-ordering.ts @@ -3,8 +3,8 @@ export default defineNuxtPlugin((nuxtApp) => { if (!nuxtApp.$layerPluginPre) { throw createError('layer plugin failed to run before end project plugin') } - if (nuxtApp.$layerPluginPost) { - throw createError('layer plugin failed to run after end project plugin') + if (!nuxtApp.$layerPluginPost) { + throw createError('layer plugin failed to run before end project plugin') } } })