fix(nuxi): add back default baseUrl in tsconfig.json (#21632)

This commit is contained in:
Daniel Roe 2023-06-19 23:29:09 +01:00 committed by GitHub
parent 3fd6c179e4
commit bb4ed5e406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 27 additions and 48 deletions

View File

@ -101,14 +101,16 @@ jobs:
category: "/language:javascript" category: "/language:javascript"
typecheck: typecheck:
runs-on: ubuntu-latest runs-on: ${{ matrix.os }}
timeout-minutes: 10 timeout-minutes: 10
needs: needs:
- build - build
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [ubuntu-latest, windows-latest]
module: ['bundler', 'node'] module: ['bundler', 'node']
base: ['with-base-url', 'without-base-url']
steps: steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
@ -131,6 +133,7 @@ jobs:
run: pnpm test:types run: pnpm test:types
env: env:
MODULE_RESOLUTION: ${{ matrix.module }} MODULE_RESOLUTION: ${{ matrix.module }}
TS_BASE_URL: ${{ matrix.base }}
lint: lint:
# autofix workflow will be triggered instead for PRs # autofix workflow will be triggered instead for PRs

View File

@ -21,9 +21,7 @@
"test:fixtures:payload": "TEST_PAYLOAD=js pnpm test:fixtures", "test:fixtures:payload": "TEST_PAYLOAD=js pnpm test:fixtures",
"test:fixtures:dev": "TEST_ENV=dev pnpm test:fixtures", "test:fixtures:dev": "TEST_ENV=dev pnpm test:fixtures",
"test:fixtures:webpack": "TEST_BUILDER=webpack pnpm test:fixtures", "test:fixtures:webpack": "TEST_BUILDER=webpack pnpm test:fixtures",
"test:types": "pnpm test:types:basic && pnpm test:types:minimal", "test:types": "pnpm --filter './test/fixtures/**' test:types",
"test:types:basic": "nuxi prepare test/fixtures/basic-types && cd test/fixtures/basic-types && npx vue-tsc --noEmit",
"test:types:minimal": "nuxi prepare test/fixtures/minimal-types && cd test/fixtures/minimal-types && npx vue-tsc --noEmit",
"test:unit": "vitest run --dir packages", "test:unit": "vitest run --dir packages",
"typecheck": "tsc --noEmit" "typecheck": "tsc --noEmit"
}, },

View File

@ -1,4 +1,4 @@
import { existsSync, promises as fsp } from 'node:fs' import { 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'
@ -18,6 +18,8 @@ export const writeTypes = async (nuxt: Nuxt) => {
skipLibCheck: true, skipLibCheck: true,
strict: nuxt.options.typescript?.strict ?? false, strict: nuxt.options.typescript?.strict ?? false,
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,
@ -34,7 +36,7 @@ export const writeTypes = async (nuxt: Nuxt) => {
// 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')) relative(nuxt.options.buildDir, resolve(nuxt.options.rootDir, 'dist'))
] ]
}) } satisfies TSConfig)
const aliases: Record<string, string> = { const aliases: Record<string, string> = {
...nuxt.options.alias, ...nuxt.options.alias,
@ -50,17 +52,15 @@ export const writeTypes = async (nuxt: Nuxt) => {
if (excludedAlias.some(re => re.test(alias))) { if (excludedAlias.some(re => re.test(alias))) {
continue continue
} }
const relativePath = isAbsolute(aliases[alias]) const absolutePath = resolve(basePath, aliases[alias])
? relativeTo(basePath, aliases[alias])
: aliases[alias]
const stats = await fsp.stat(resolve(basePath, relativePath)).catch(() => null /* file does not exist */) const stats = await fsp.stat(absolutePath).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] = [absolutePath]
tsConfig.compilerOptions.paths[`${alias}/*`] = [`${relativePath}/*`] tsConfig.compilerOptions.paths[`${alias}/*`] = [`${absolutePath}/*`]
} else { } else {
tsConfig.compilerOptions.paths[alias] = [relativePath.replace(/(?<=\w)\.\w+$/g, '')] /* remove extension */ tsConfig.compilerOptions.paths[alias] = [absolutePath.replace(/(?<=\w)\.\w+$/g, '')] /* remove extension */
} }
} }
@ -79,18 +79,6 @@ 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)) {
@ -123,18 +111,6 @@ 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(' ')
} }

View File

@ -178,7 +178,7 @@ export default defineNuxtModule<ComponentsOptions>({
}) })
nuxt.hook('prepare:types', ({ references, tsConfig }) => { nuxt.hook('prepare:types', ({ references, tsConfig }) => {
tsConfig.compilerOptions!.paths['#components'] = [withLeadingDot(relative(nuxt.options.buildDir, resolve(nuxt.options.buildDir, 'components')))] tsConfig.compilerOptions!.paths['#components'] = [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,11 +255,3 @@ export default defineNuxtModule<ComponentsOptions>({
}) })
} }
}) })
const LEADING_DOT_RE = /^\.{1,2}(\/|$)/
function withLeadingDot (path: string) {
if (LEADING_DOT_RE.test(path)) {
return path
}
return `./${path}`
}

View File

@ -35,6 +35,14 @@ export default defineNuxtConfig({
} }
}, },
modules: [ modules: [
function (_, nuxt) {
// TODO: remove in v3.7
if (process.env.TS_BASE_URL === 'without-base-url') {
nuxt.hook('prepare:types', ({ tsConfig }) => {
delete tsConfig.compilerOptions!.baseUrl
})
}
},
function () { function () {
addTypeTemplate({ addTypeTemplate({
filename: 'test.d.ts', filename: 'test.d.ts',

View File

@ -2,7 +2,8 @@
"private": true, "private": true,
"name": "fixture-basic-types", "name": "fixture-basic-types",
"scripts": { "scripts": {
"build": "nuxi build" "build": "nuxi build",
"test:types": "nuxi prepare && npx vue-tsc --noEmit"
}, },
"dependencies": { "dependencies": {
"nuxt": "workspace:*" "nuxt": "workspace:*"

View File

@ -2,7 +2,8 @@
"private": true, "private": true,
"name": "fixture-minimal-types", "name": "fixture-minimal-types",
"scripts": { "scripts": {
"build": "nuxi build" "build": "nuxi build",
"test:types": "nuxi prepare && npx vue-tsc --noEmit"
}, },
"dependencies": { "dependencies": {
"nuxt": "workspace:*" "nuxt": "workspace:*"