perf(kit,nuxt): don't resolve paths from local layers/modules (#30650)

This commit is contained in:
Daniel Roe 2025-01-17 23:45:42 +00:00 committed by GitHub
parent 00e0b3c136
commit e4a6cdceb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 6 deletions

View File

@ -19,11 +19,11 @@ export async function installModule<
> (moduleToInstall: T, inlineOptions?: [Config] extends [never] ? any : Config[1], nuxt: Nuxt = useNuxt()) { > (moduleToInstall: T, inlineOptions?: [Config] extends [never] ? any : Config[1], nuxt: Nuxt = useNuxt()) {
const { nuxtModule, buildTimeModuleMeta, resolvedModulePath } = await loadNuxtModuleInstance(moduleToInstall, nuxt) const { nuxtModule, buildTimeModuleMeta, resolvedModulePath } = await loadNuxtModuleInstance(moduleToInstall, nuxt)
const localLayerModuleDirs = new Set<string>() const localLayerModuleDirs: string[] = []
for (const l of nuxt.options._layers) { for (const l of nuxt.options._layers) {
const srcDir = l.config.srcDir || l.cwd const srcDir = l.config.srcDir || l.cwd
if (!NODE_MODULES_RE.test(srcDir)) { if (!NODE_MODULES_RE.test(srcDir)) {
localLayerModuleDirs.add(resolve(srcDir, l.config?.dir?.modules || 'modules')) localLayerModuleDirs.push(resolve(srcDir, l.config?.dir?.modules || 'modules').replace(/\/?$/, '/'))
} }
} }
@ -38,8 +38,8 @@ export async function installModule<
const parsed = parseNodeModulePath(modulePath) const parsed = parseNodeModulePath(modulePath)
const moduleRoot = parsed.dir ? parsed.dir + parsed.name : modulePath const moduleRoot = parsed.dir ? parsed.dir + parsed.name : modulePath
nuxt.options.build.transpile.push(normalizeModuleTranspilePath(moduleRoot)) nuxt.options.build.transpile.push(normalizeModuleTranspilePath(moduleRoot))
const directory = parsed.dir ? moduleRoot : getDirectory(modulePath) const directory = (parsed.dir ? moduleRoot : getDirectory(modulePath)).replace(/\/?$/, '/')
if (directory !== moduleToInstall && !localLayerModuleDirs.has(directory)) { if (directory !== moduleToInstall && !localLayerModuleDirs.some(dir => directory.startsWith(dir))) {
nuxt.options.modulesDir.push(resolve(directory, 'node_modules')) nuxt.options.modulesDir.push(resolve(directory, 'node_modules'))
} }
} }

View File

@ -404,8 +404,11 @@ async function initNuxt (nuxt: Nuxt) {
...nuxt.options._layers.filter(i => i.cwd.includes('node_modules')).map(i => i.cwd as string), ...nuxt.options._layers.filter(i => i.cwd.includes('node_modules')).map(i => i.cwd as string),
) )
// Ensure we can resolve dependencies within layers // Ensure we can resolve dependencies within layers - filtering out local `~/layers` directories
nuxt.options.modulesDir.push(...nuxt.options._layers.map(l => resolve(l.cwd, 'node_modules'))) const locallyScannedLayersDirs = nuxt.options._layers.map(l => resolve(l.cwd, 'layers').replace(/\/?$/, '/'))
nuxt.options.modulesDir.push(...nuxt.options._layers
.filter(l => l.cwd !== nuxt.options.rootDir && locallyScannedLayersDirs.every(dir => !l.cwd.startsWith(dir)))
.map(l => resolve(l.cwd, 'node_modules')))
// Init user modules // Init user modules
await nuxt.callHook('modules:before') await nuxt.callHook('modules:before')
@ -571,6 +574,7 @@ async function initNuxt (nuxt: Nuxt) {
nuxt._ignore.add(resolveIgnorePatterns()) nuxt._ignore.add(resolveIgnorePatterns())
await nuxt.callHook('modules:done') await nuxt.callHook('modules:done')
console.log(nuxt.options.modulesDir, nuxt.options.modulesDir.length)
// Add <NuxtIsland> // Add <NuxtIsland>
if (nuxt.options.experimental.componentIslands) { if (nuxt.options.experimental.componentIslands) {