mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 00:23:53 +00:00
perf(vite): avoid extra resolve call for resolveId
in layers (#27971)
This commit is contained in:
parent
a238a83e9d
commit
ebe6c7122c
@ -123,6 +123,7 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => {
|
||||
ctx.config.build!.watch = undefined
|
||||
}
|
||||
|
||||
// TODO: this may no longer be needed with most recent vite version
|
||||
if (nuxt.options.dev) {
|
||||
// Identify which layers will need to have an extra resolve step.
|
||||
const layerDirs: string[] = []
|
||||
@ -133,18 +134,25 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => {
|
||||
}
|
||||
}
|
||||
if (layerDirs.length > 0) {
|
||||
ctx.config.plugins!.push({
|
||||
// Reverse so longest/most specific directories are searched first
|
||||
layerDirs.sort().reverse()
|
||||
ctx.nuxt.hook('vite:extendConfig', (config) => {
|
||||
const dirs = [...layerDirs]
|
||||
config.plugins!.push({
|
||||
name: 'nuxt:optimize-layer-deps',
|
||||
enforce: 'pre',
|
||||
async resolveId (source, _importer) {
|
||||
if (!_importer) { return }
|
||||
if (!_importer || !dirs.length) { return }
|
||||
const importer = normalize(_importer)
|
||||
if (layerDirs.some(dir => importer.startsWith(dir))) {
|
||||
const layerIndex = dirs.findIndex(dir => importer.startsWith(dir))
|
||||
// Trigger vite to optimize dependencies imported within a layer, just as if they were imported in final project
|
||||
if (layerIndex !== -1) {
|
||||
dirs.splice(layerIndex, 1)
|
||||
await this.resolve(source, join(nuxt.options.srcDir, 'index.html'), { skipSelf: true }).catch(() => null)
|
||||
}
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user