fix(kit): remove baseUrl and use relative paths in tsconfig (#22410)

This commit is contained in:
Daniel Roe 2023-08-01 10:17:02 +01:00 committed by GitHub
parent 2dfbb5f04d
commit 2540c879da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 15 deletions

View File

@ -34,6 +34,7 @@
"pkg-types": "^1.0.3", "pkg-types": "^1.0.3",
"scule": "^1.0.0", "scule": "^1.0.0",
"semver": "^7.5.4", "semver": "^7.5.4",
"ufo": "^1.2.0",
"unctx": "^2.3.1", "unctx": "^2.3.1",
"unimport": "^3.1.0", "unimport": "^3.1.0",
"untyped": "^1.4.0" "untyped": "^1.4.0"

View File

@ -7,6 +7,7 @@ import { defu } from 'defu'
import type { TSConfig } from 'pkg-types' import type { TSConfig } from 'pkg-types'
import { readPackageJSON } from 'pkg-types' import { readPackageJSON } from 'pkg-types'
import { tryResolveModule } from './internal/esm'
import { tryUseNuxt, useNuxt } from './context' import { tryUseNuxt, useNuxt } from './context'
import { getModulePaths } from './internal/cjs' import { getModulePaths } from './internal/cjs'
@ -122,8 +123,6 @@ export async function writeTypes (nuxt: Nuxt) {
skipLibCheck: true, skipLibCheck: true,
strict: nuxt.options.typescript?.strict ?? true, strict: nuxt.options.typescript?.strict ?? true,
allowJs: true, allowJs: true,
// TODO: remove by default in 3.7
baseUrl: nuxt.options.srcDir,
noEmit: true, noEmit: true,
resolveJsonModule: true, resolveJsonModule: true,
allowSyntheticDefaultImports: true, allowSyntheticDefaultImports: true,
@ -132,7 +131,7 @@ export async function writeTypes (nuxt: Nuxt) {
}, },
include: [ include: [
'./nuxt.d.ts', './nuxt.d.ts',
join(relative(nuxt.options.buildDir, nuxt.options.rootDir), '**/*'), join(relativeWithDot(nuxt.options.buildDir, nuxt.options.rootDir), '**/*'),
...nuxt.options.srcDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.srcDir), '**/*')] : [], ...nuxt.options.srcDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.srcDir), '**/*')] : [],
...nuxt.options._layers.map(layer => layer.config.srcDir ?? layer.cwd) ...nuxt.options._layers.map(layer => layer.config.srcDir ?? layer.cwd)
.filter(srcOrCwd => !srcOrCwd.startsWith(rootDirWithSlash) || srcOrCwd.includes('node_modules')) .filter(srcOrCwd => !srcOrCwd.startsWith(rootDirWithSlash) || srcOrCwd.includes('node_modules'))
@ -140,9 +139,9 @@ export async function writeTypes (nuxt: Nuxt) {
...nuxt.options.typescript.includeWorkspace && nuxt.options.workspaceDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.workspaceDir), '**/*')] : [] ...nuxt.options.typescript.includeWorkspace && nuxt.options.workspaceDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.workspaceDir), '**/*')] : []
], ],
exclude: [ exclude: [
...nuxt.options.modulesDir.map(m => relative(nuxt.options.buildDir, m)), ...nuxt.options.modulesDir.map(m => relativeWithDot(nuxt.options.buildDir, m)),
// nitro generate output: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/core/nitro.ts#L186 // nitro generate output: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/core/nitro.ts#L186
relative(nuxt.options.buildDir, resolve(nuxt.options.rootDir, 'dist')) relativeWithDot(nuxt.options.buildDir, resolve(nuxt.options.rootDir, 'dist'))
] ]
} satisfies TSConfig) } satisfies TSConfig)
@ -163,21 +162,30 @@ export async function writeTypes (nuxt: Nuxt) {
if (excludedAlias.some(re => re.test(alias))) { if (excludedAlias.some(re => re.test(alias))) {
continue continue
} }
const absolutePath = resolve(basePath, aliases[alias]) let absolutePath = resolve(basePath, aliases[alias])
const relativePath = relative(nuxt.options.buildDir, absolutePath) let stats = await fsp.stat(absolutePath).catch(() => null /* file does not exist */)
if (!stats) {
const resolvedModule = await tryResolveModule(aliases[alias], nuxt.options.modulesDir)
if (resolvedModule) {
absolutePath = resolvedModule
stats = await fsp.stat(resolvedModule).catch(() => null)
}
}
const stats = await fsp.stat(absolutePath).catch(() => null /* file does not exist */) const relativePath = relativeWithDot(nuxt.options.buildDir, absolutePath)
if (stats?.isDirectory()) { if (stats?.isDirectory()) {
tsConfig.compilerOptions.paths[alias] = [absolutePath] tsConfig.compilerOptions.paths[alias] = [relativePath]
tsConfig.compilerOptions.paths[`${alias}/*`] = [`${absolutePath}/*`] tsConfig.compilerOptions.paths[`${alias}/*`] = [`${relativePath}/*`]
if (!absolutePath.startsWith(rootDirWithSlash)) { if (!absolutePath.startsWith(rootDirWithSlash)) {
tsConfig.include.push(relativePath) tsConfig.include.push(relativePath)
} }
} else { } else {
const path = stats?.isFile() const path = stats?.isFile()
? absolutePath.replace(/(?<=\w)\.\w+$/g, '') /* remove extension */ // remove extension
: absolutePath ? relativePath.replace(/(?<=\w)\.\w+$/g, '')
// non-existent file probably shouldn't be resolved
: aliases[alias]
tsConfig.compilerOptions.paths[alias] = [path] tsConfig.compilerOptions.paths[alias] = [path]
@ -200,11 +208,20 @@ export async function writeTypes (nuxt: Nuxt) {
const declarations: string[] = [] const declarations: string[] = []
tsConfig.include = [...new Set(tsConfig.include)]
tsConfig.exclude = [...new Set(tsConfig.exclude)]
await nuxt.callHook('prepare:types', { references, declarations, tsConfig }) await nuxt.callHook('prepare:types', { references, declarations, tsConfig })
for (const alias in tsConfig.compilerOptions!.paths) {
const paths = tsConfig.compilerOptions!.paths[alias]
tsConfig.compilerOptions!.paths[alias] = await Promise.all(paths.map(async (path: string) => {
if (!isAbsolute(path)) { return path }
const stats = await fsp.stat(path).catch(() => null /* file does not exist */)
return relativeWithDot(nuxt.options.buildDir, stats?.isFile() ? path.replace(/(?<=\w)\.\w+$/g, '') /* remove extension */ : path)
}))
}
tsConfig.include = [...new Set(tsConfig.include.map(p => isAbsolute(p) ? relativeWithDot(nuxt.options.buildDir, p) : p))]
tsConfig.exclude = [...new Set(tsConfig.exclude!.map(p => isAbsolute(p) ? relativeWithDot(nuxt.options.buildDir, p) : p))]
const declaration = [ const declaration = [
...references.map((ref) => { ...references.map((ref) => {
if ('path' in ref && isAbsolute(ref.path)) { if ('path' in ref && isAbsolute(ref.path)) {
@ -244,3 +261,7 @@ function renderAttrs (obj: Record<string, string>) {
function renderAttr (key: string, value: string) { function renderAttr (key: string, value: string) {
return value ? `${key}="${value}"` : '' return value ? `${key}="${value}"` : ''
} }
function relativeWithDot (from: string, to: string) {
return relative(from, to).replace(/^([^.])/, './$1') || '.'
}

View File

@ -194,6 +194,9 @@ importers:
semver: semver:
specifier: ^7.5.4 specifier: ^7.5.4
version: 7.5.4 version: 7.5.4
ufo:
specifier: ^1.2.0
version: 1.2.0
unctx: unctx:
specifier: ^2.3.1 specifier: ^2.3.1
version: 2.3.1 version: 2.3.1