mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
chore: update plugin sorting algorithm + update test
This commit is contained in:
parent
7ff5262be7
commit
3e83c09b97
@ -126,31 +126,21 @@ async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Re-initialise plugins
|
// Re-initialise plugins
|
||||||
app.plugins = nuxt.options.plugins.map(normalizePlugin)
|
app.plugins = []
|
||||||
|
let idx = 0
|
||||||
// Track scanned plugins separately so we can apply a sorting mechanism to them
|
// Sort Plugins: layers plugins first then project plugins
|
||||||
const scannedPlugins: [string, NuxtPlugin][] = []
|
for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) {
|
||||||
for (const config of nuxt.options._layers.map(layer => layer.config)) {
|
app.plugins[idx++ === (nuxt.options._layers.length - 1) ? 'push' : 'unshift'](...[
|
||||||
for (const plugin of config.plugins || []) {
|
...(config.plugins || []),
|
||||||
app.plugins.push(normalizePlugin(plugin as NuxtPlugin | string))
|
...config.srcDir
|
||||||
}
|
? await resolveFiles(config.srcDir, [
|
||||||
|
`${config.dir?.plugins || 'plugins'}/*.{ts,js,mjs,cjs,mts,cts}`,
|
||||||
if (config.srcDir) {
|
`${config.dir?.plugins || 'plugins'}/*/index.*{ts,js,mjs,cjs,mts,cts}` // TODO: remove, only scan top-level plugins #18418
|
||||||
const pluginDir = join(config.srcDir, config.dir?.plugins || 'plugins')
|
])
|
||||||
for (const file of await resolveFiles(pluginDir, [
|
: []
|
||||||
'*.{ts,js,mjs,cjs,mts,cts}',
|
].map(plugin => normalizePlugin(plugin as NuxtPlugin)))
|
||||||
'*/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.unshift(...nuxt.options.plugins.map(normalizePlugin))
|
||||||
|
|
||||||
// Normalize and de-duplicate plugins and middleware
|
// Normalize and de-duplicate plugins and middleware
|
||||||
app.middleware = uniqueBy(await resolvePaths(app.middleware, 'path'), 'name')
|
app.middleware = uniqueBy(await resolvePaths(app.middleware, 'path'), 'name')
|
||||||
|
@ -3,8 +3,8 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
if (!nuxtApp.$layerPluginPre) {
|
if (!nuxtApp.$layerPluginPre) {
|
||||||
throw createError('layer plugin failed to run before end project plugin')
|
throw createError('layer plugin failed to run before end project plugin')
|
||||||
}
|
}
|
||||||
if (nuxtApp.$layerPluginPost) {
|
if (!nuxtApp.$layerPluginPost) {
|
||||||
throw createError('layer plugin failed to run after end project plugin')
|
throw createError('layer plugin failed to run before end project plugin')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user