Skip to content

Commit b08dfc9

Browse files
committed
fix(kit): do not use module: preserve unelss ts v5.4 is installed
1 parent 0d84300 commit b08dfc9

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

packages/kit/src/template.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Nuxt, NuxtTemplate, NuxtTypeTemplate, ResolvedNuxtTemplate, TSRefe
55
import { withTrailingSlash } from 'ufo'
66
import { defu } from 'defu'
77
import type { TSConfig } from 'pkg-types'
8+
import { gte } from 'semver'
89
import { readPackageJSON } from 'pkg-types'
910

1011
import { tryResolveModule } from './internal/esm'
@@ -122,6 +123,12 @@ export async function _generateTypes (nuxt: Nuxt) {
122123
.map(m => getDirectory(m.entryPath)),
123124
)
124125

126+
const isV4 = nuxt.options.future?.compatibilityVersion === 4
127+
128+
const hasTypescriptVersionWithModulePreserve = await readPackageJSON('typescript', { url: nuxt.options.modulesDir })
129+
.then(r => r?.version && gte(r.version, '5.4.0'))
130+
.catch(() => isV4)
131+
125132
// https://www.totaltypescript.com/tsconfig-cheat-sheet
126133
const tsConfig: TSConfig = defu(nuxt.options.typescript?.tsConfig, {
127134
compilerOptions: {
@@ -136,11 +143,11 @@ export async function _generateTypes (nuxt: Nuxt) {
136143
verbatimModuleSyntax: true,
137144
/* Strictness */
138145
strict: nuxt.options.typescript?.strict ?? true,
139-
noUncheckedIndexedAccess: nuxt.options.future?.compatibilityVersion === 4,
146+
noUncheckedIndexedAccess: isV4,
140147
forceConsistentCasingInFileNames: true,
141148
noImplicitOverride: true,
142149
/* If NOT transpiling with TypeScript: */
143-
module: 'preserve',
150+
module: hasTypescriptVersionWithModulePreserve ? 'preserve' : 'es2022',
144151
noEmit: true,
145152
/* If your code runs in the DOM: */
146153
lib: [

0 commit comments

Comments
 (0)