mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
fix(nuxi,nuxt): remove baseUrl + use relative paths in tsconfig (#21081)
This commit is contained in:
parent
c884a95f0f
commit
acdc28a99e
@ -1,4 +1,4 @@
|
||||
import { promises as fsp } from 'node:fs'
|
||||
import { existsSync, promises as fsp } from 'node:fs'
|
||||
import { isAbsolute, join, relative, resolve } from 'pathe'
|
||||
import type { Nuxt, TSReference } from '@nuxt/schema'
|
||||
import { defu } from 'defu'
|
||||
@ -22,7 +22,6 @@ export const writeTypes = async (nuxt: Nuxt) => {
|
||||
resolveJsonModule: true,
|
||||
allowSyntheticDefaultImports: true,
|
||||
types: ['node'],
|
||||
baseUrl: relative(nuxt.options.buildDir, nuxt.options.rootDir),
|
||||
paths: {}
|
||||
},
|
||||
include: [
|
||||
@ -45,15 +44,17 @@ export const writeTypes = async (nuxt: Nuxt) => {
|
||||
// Exclude bridge alias types to support Volar
|
||||
const excludedAlias = [/^@vue\/.*$/]
|
||||
|
||||
const basePath = tsConfig.compilerOptions!.baseUrl ? resolve(nuxt.options.buildDir, tsConfig.compilerOptions!.baseUrl) : nuxt.options.buildDir
|
||||
|
||||
for (const alias in aliases) {
|
||||
if (excludedAlias.some(re => re.test(alias))) {
|
||||
continue
|
||||
}
|
||||
const relativePath = isAbsolute(aliases[alias])
|
||||
? relative(nuxt.options.rootDir, aliases[alias]) || '.'
|
||||
? relativeTo(basePath, aliases[alias])
|
||||
: aliases[alias]
|
||||
|
||||
const stats = await fsp.stat(resolve(nuxt.options.rootDir, relativePath)).catch(() => null /* file does not exist */)
|
||||
const stats = await fsp.stat(resolve(basePath, relativePath)).catch(() => null /* file does not exist */)
|
||||
tsConfig.compilerOptions = tsConfig.compilerOptions || {}
|
||||
if (stats?.isDirectory()) {
|
||||
tsConfig.compilerOptions.paths[alias] = [relativePath]
|
||||
@ -78,6 +79,18 @@ export const writeTypes = async (nuxt: Nuxt) => {
|
||||
|
||||
await nuxt.callHook('prepare:types', { references, declarations, tsConfig })
|
||||
|
||||
// Normalise aliases to be relative to buildDir for backward compatibility
|
||||
for (const alias in tsConfig.compilerOptions!.paths!) {
|
||||
const paths = tsConfig.compilerOptions!.paths![alias] as string[]
|
||||
for (const [index, path] of paths.entries()) {
|
||||
if (isAbsolute(path) || LEADING_DOT_RE.test(path)) { continue }
|
||||
const resolvedPath = join(nuxt.options.rootDir, path) /* previously basePath was set to rootDir */
|
||||
if (existsSync(resolvedPath)) {
|
||||
paths[index] = relativeTo(basePath, resolvedPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const declaration = [
|
||||
...references.map((ref) => {
|
||||
if ('path' in ref && isAbsolute(ref.path)) {
|
||||
@ -110,6 +123,18 @@ export const writeTypes = async (nuxt: Nuxt) => {
|
||||
await writeFile()
|
||||
}
|
||||
|
||||
const LEADING_DOT_RE = /^\.{1,2}(\/|$)/
|
||||
function withLeadingDot (path: string) {
|
||||
if (LEADING_DOT_RE.test(path)) {
|
||||
return path
|
||||
}
|
||||
return `./${path}`
|
||||
}
|
||||
|
||||
function relativeTo (from: string, to: string) {
|
||||
return withLeadingDot(relative(from, to) || '.')
|
||||
}
|
||||
|
||||
function renderAttrs (obj: Record<string, string>) {
|
||||
return Object.entries(obj).map(e => renderAttr(e[0], e[1])).join(' ')
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ export default defineNuxtModule<ComponentsOptions>({
|
||||
})
|
||||
|
||||
nuxt.hook('prepare:types', ({ references, tsConfig }) => {
|
||||
tsConfig.compilerOptions!.paths['#components'] = [relative(nuxt.options.rootDir, resolve(nuxt.options.buildDir, 'components'))]
|
||||
tsConfig.compilerOptions!.paths['#components'] = [withLeadingDot(relative(nuxt.options.buildDir, resolve(nuxt.options.buildDir, 'components')))]
|
||||
references.push({ path: resolve(nuxt.options.buildDir, 'components.d.ts') })
|
||||
})
|
||||
|
||||
@ -255,3 +255,11 @@ export default defineNuxtModule<ComponentsOptions>({
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
const LEADING_DOT_RE = /^\.{1,2}(\/|$)/
|
||||
function withLeadingDot (path: string) {
|
||||
if (LEADING_DOT_RE.test(path)) {
|
||||
return path
|
||||
}
|
||||
return `./${path}`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user