mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-28 03:45:05 +00:00
refactor: simplify implementation slightly
This commit is contained in:
parent
0cdd58157b
commit
c5e7cc737d
@ -125,33 +125,32 @@ async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve plugins
|
// Re-initialise plugins
|
||||||
app.plugins = [
|
app.plugins = nuxt.options.plugins.map(normalizePlugin)
|
||||||
...nuxt.options.plugins.map(normalizePlugin)
|
|
||||||
]
|
// Track scanned plugins separately so we can apply a sorting mechanism to them
|
||||||
const plugins: NuxtPlugin[] = []
|
const scannedPlugins: [string, NuxtPlugin][] = []
|
||||||
for (const config of nuxt.options._layers.map(layer => layer.config)) {
|
for (const config of nuxt.options._layers.map(layer => layer.config)) {
|
||||||
plugins.push(...[
|
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 order and name
|
// Sort scanned plugins by their names so a scanned `01.plugin.ts` will always run before finished `02.plugin.ts`
|
||||||
const pluginNameRegex = /\/index\.[cm]?[jt]s$/
|
// and append to the plugin array
|
||||||
app.plugins.push(...plugins.sort((a, b) => {
|
for (const [_name, plugin] of scannedPlugins.sort(([a], [b]) => a.localeCompare(b))) {
|
||||||
const sortMapResult = (a.order ?? orderMap.default) - (b.order ?? orderMap.default)
|
app.plugins.push(plugin)
|
||||||
if (sortMapResult !== 0) { return sortMapResult }
|
}
|
||||||
|
|
||||||
const aName = pluginNameRegex.test(a.src) ? basename(dirname(a.src)) : basename(a.src)
|
|
||||||
const bName = pluginNameRegex.test(b.src) ? basename(dirname(b.src)) : basename(b.src)
|
|
||||||
return aName.localeCompare(bName)
|
|
||||||
}))
|
|
||||||
|
|
||||||
// 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')
|
||||||
|
Loading…
Reference in New Issue
Block a user