mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 21:55:11 +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 { isAbsolute, join, relative, resolve } from 'pathe'
|
||||||
import type { Nuxt, TSReference } from '@nuxt/schema'
|
import type { Nuxt, TSReference } from '@nuxt/schema'
|
||||||
import { defu } from 'defu'
|
import { defu } from 'defu'
|
||||||
@ -22,7 +22,6 @@ export const writeTypes = async (nuxt: Nuxt) => {
|
|||||||
resolveJsonModule: true,
|
resolveJsonModule: true,
|
||||||
allowSyntheticDefaultImports: true,
|
allowSyntheticDefaultImports: true,
|
||||||
types: ['node'],
|
types: ['node'],
|
||||||
baseUrl: relative(nuxt.options.buildDir, nuxt.options.rootDir),
|
|
||||||
paths: {}
|
paths: {}
|
||||||
},
|
},
|
||||||
include: [
|
include: [
|
||||||
@ -45,15 +44,17 @@ export const writeTypes = async (nuxt: Nuxt) => {
|
|||||||
// Exclude bridge alias types to support Volar
|
// Exclude bridge alias types to support Volar
|
||||||
const excludedAlias = [/^@vue\/.*$/]
|
const excludedAlias = [/^@vue\/.*$/]
|
||||||
|
|
||||||
|
const basePath = tsConfig.compilerOptions!.baseUrl ? resolve(nuxt.options.buildDir, tsConfig.compilerOptions!.baseUrl) : nuxt.options.buildDir
|
||||||
|
|
||||||
for (const alias in aliases) {
|
for (const alias in aliases) {
|
||||||
if (excludedAlias.some(re => re.test(alias))) {
|
if (excludedAlias.some(re => re.test(alias))) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
const relativePath = isAbsolute(aliases[alias])
|
const relativePath = isAbsolute(aliases[alias])
|
||||||
? relative(nuxt.options.rootDir, aliases[alias]) || '.'
|
? relativeTo(basePath, aliases[alias])
|
||||||
: 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 || {}
|
tsConfig.compilerOptions = tsConfig.compilerOptions || {}
|
||||||
if (stats?.isDirectory()) {
|
if (stats?.isDirectory()) {
|
||||||
tsConfig.compilerOptions.paths[alias] = [relativePath]
|
tsConfig.compilerOptions.paths[alias] = [relativePath]
|
||||||
@ -78,6 +79,18 @@ export const writeTypes = async (nuxt: Nuxt) => {
|
|||||||
|
|
||||||
await nuxt.callHook('prepare:types', { references, declarations, tsConfig })
|
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 = [
|
const declaration = [
|
||||||
...references.map((ref) => {
|
...references.map((ref) => {
|
||||||
if ('path' in ref && isAbsolute(ref.path)) {
|
if ('path' in ref && isAbsolute(ref.path)) {
|
||||||
@ -110,6 +123,18 @@ export const writeTypes = async (nuxt: Nuxt) => {
|
|||||||
await writeFile()
|
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>) {
|
function renderAttrs (obj: Record<string, string>) {
|
||||||
return Object.entries(obj).map(e => renderAttr(e[0], e[1])).join(' ')
|
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 }) => {
|
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') })
|
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