mirror of
https://github.com/nuxt/nuxt.git
synced 2025-03-20 16:25:55 +00:00
fix(nuxt): restore nuxt aliases to nitro compilerOptions.paths (#31278)
This commit is contained in:
parent
48e1138cd2
commit
ce800422dd
@ -450,6 +450,31 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
|
|||||||
// Extend nitro config with hook
|
// Extend nitro config with hook
|
||||||
await nuxt.callHook('nitro:config', nitroConfig)
|
await nuxt.callHook('nitro:config', nitroConfig)
|
||||||
|
|
||||||
|
// TODO: extract to shared utility?
|
||||||
|
const excludedAlias = [/^@vue\/.*$/, 'vue', /vue-router/, 'vite/client', '#imports', 'vue-demi', /^#app/, '~', '@', '~~', '@@']
|
||||||
|
const basePath = nitroConfig.typescript!.tsConfig!.compilerOptions?.baseUrl ? resolve(nuxt.options.buildDir, nitroConfig.typescript!.tsConfig!.compilerOptions?.baseUrl) : nuxt.options.buildDir
|
||||||
|
const aliases = nitroConfig.alias!
|
||||||
|
const tsConfig = nitroConfig.typescript!.tsConfig!
|
||||||
|
tsConfig.compilerOptions ||= {}
|
||||||
|
tsConfig.compilerOptions.paths ||= {}
|
||||||
|
for (const _alias in aliases) {
|
||||||
|
const alias = _alias as keyof typeof aliases
|
||||||
|
if (excludedAlias.some(pattern => typeof pattern === 'string' ? alias === pattern : pattern.test(alias))) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (alias in tsConfig.compilerOptions.paths) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
const absolutePath = resolve(basePath, aliases[alias]!)
|
||||||
|
const stats = await fsp.stat(absolutePath).catch(() => null /* file does not exist */)
|
||||||
|
// note - nitro will check + remove the file extension as required
|
||||||
|
tsConfig.compilerOptions.paths[alias] = [absolutePath]
|
||||||
|
if (stats?.isDirectory()) {
|
||||||
|
tsConfig.compilerOptions.paths[`${alias}/*`] = [`${absolutePath}/*`]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Init nitro
|
// Init nitro
|
||||||
const nitro = await createNitro(nitroConfig, {
|
const nitro = await createNitro(nitroConfig, {
|
||||||
compatibilityDate: nuxt.options.compatibilityDate,
|
compatibilityDate: nuxt.options.compatibilityDate,
|
||||||
|
Loading…
Reference in New Issue
Block a user