fix(kit): do not use module: preserve unelss ts v5.4 is installed

This commit is contained in:
Daniel Roe 2024-06-10 10:48:49 +01:00
parent 0d84300218
commit b08dfc98be
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B

View File

@ -5,6 +5,7 @@ import type { Nuxt, NuxtTemplate, NuxtTypeTemplate, ResolvedNuxtTemplate, TSRefe
import { withTrailingSlash } from 'ufo'
import { defu } from 'defu'
import type { TSConfig } from 'pkg-types'
import { gte } from 'semver'
import { readPackageJSON } from 'pkg-types'
import { tryResolveModule } from './internal/esm'
@ -122,6 +123,12 @@ export async function _generateTypes (nuxt: Nuxt) {
.map(m => getDirectory(m.entryPath)),
)
const isV4 = nuxt.options.future?.compatibilityVersion === 4
const hasTypescriptVersionWithModulePreserve = await readPackageJSON('typescript', { url: nuxt.options.modulesDir })
.then(r => r?.version && gte(r.version, '5.4.0'))
.catch(() => isV4)
// https://www.totaltypescript.com/tsconfig-cheat-sheet
const tsConfig: TSConfig = defu(nuxt.options.typescript?.tsConfig, {
compilerOptions: {
@ -136,11 +143,11 @@ export async function _generateTypes (nuxt: Nuxt) {
verbatimModuleSyntax: true,
/* Strictness */
strict: nuxt.options.typescript?.strict ?? true,
noUncheckedIndexedAccess: nuxt.options.future?.compatibilityVersion === 4,
noUncheckedIndexedAccess: isV4,
forceConsistentCasingInFileNames: true,
noImplicitOverride: true,
/* If NOT transpiling with TypeScript: */
module: 'preserve',
module: hasTypescriptVersionWithModulePreserve ? 'preserve' : 'es2022',
noEmit: true,
/* If your code runs in the DOM: */
lib: [