Skip to content

Commit 93164c3

Browse files
authored
feat: add top-level tsconfig option (#23310)
1 parent f40efef commit 93164c3

10 files changed

Lines changed: 118 additions & 4 deletions

File tree

‎docs/config/shared-options.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,20 @@ Ensure that `@vitejs/devtools` is installed as a dependency. This feature is cur
629629

630630
See [Vite DevTools](https://github.com/vitejs/devtools) for more details.
631631

632+
## tsconfig
633+
634+
- **Type:** `string`
635+
636+
Path to the TypeScript configuration file used by Vite. Relative paths are resolved from the project [`root`](#root).
637+
638+
When this option is not set, Vite discovers the closest matching `tsconfig.json` for each file. See [TypeScript Compiler Options](/guide/features#typescript-compiler-options) for more details.
639+
640+
::: warning Prefer automatic discovery
641+
Setting this option is discouraged because it overrides Vite's per-file tsconfig discovery which is aligned with TypeScript language server. Prefer placing a `tsconfig.json` near the files it configures and using TypeScript [`references`](https://www.typescriptlang.org/tsconfig/#references) for multi-project setups.
642+
643+
If the goal is to remap imports, prefer [`resolve.alias`](#resolve-alias) or the `imports` and `exports` fields in `package.json` instead of selecting a tsconfig solely for [`compilerOptions.paths`](https://www.typescriptlang.org/tsconfig/#paths). Use this option only when automatic discovery cannot identify the intended configuration.
644+
:::
645+
632646
## future
633647

634648
- **Type:** `Record<string, 'warn' | undefined>`

‎docs/guide/features.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export type { T }
5353

5454
### TypeScript Compiler Options
5555

56-
Vite respects some of the options in `tsconfig.json` and sets the corresponding Oxc Transformer options. For each file, Vite uses the closest parent `tsconfig.json` that matches the file, or a config referenced by its [`references`](https://www.typescriptlang.org/tsconfig/#references) field that matches the file. Vite treats a config as matching the file when the file satisfies the config's [`files`](https://www.typescriptlang.org/tsconfig/#files), [`include`](https://www.typescriptlang.org/tsconfig/#include), and [`exclude`](https://www.typescriptlang.org/tsconfig/#exclude) fields.
56+
Vite respects some of the options in `tsconfig.json` and sets the corresponding Oxc Transformer options. By default, Vite uses the closest parent `tsconfig.json` that matches each file. A config referenced by that config's [`references`](https://www.typescriptlang.org/tsconfig/#references) field is used when it matches the file. Vite treats a config as matching the file when the file satisfies the config's [`files`](https://www.typescriptlang.org/tsconfig/#files), [`include`](https://www.typescriptlang.org/tsconfig/#include), and [`exclude`](https://www.typescriptlang.org/tsconfig/#exclude) fields.
5757

5858
When the options are set in both the Vite config and the `tsconfig.json`, the value in the Vite config takes precedence.
5959

‎packages/vite/src/node/__tests__/build.spec.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
RollupLog,
1313
} from 'rolldown'
1414
import { afterEach, describe, expect, assert, test, vi } from 'vitest'
15-
import { BuildEnvironment, resolveConfig } from '..'
15+
import { BuildEnvironment, normalizePath, resolveConfig } from '..'
1616
import type { LibraryFormats, LibraryOptions } from '../build'
1717
import {
1818
ChunkMetadataMap,
@@ -488,6 +488,28 @@ describe('resolveBuildOutputs', () => {
488488
expect(options.input).toBe('explicit-entry.js')
489489
})
490490

491+
test('top-level tsconfig applies to Rolldown options', async () => {
492+
const builder = await createBuilder({
493+
root: buildProjectRoot,
494+
logLevel: 'silent',
495+
tsconfig: './custom.tsconfig.json',
496+
build: {
497+
rolldownOptions: {
498+
tsconfig: './other.tsconfig.json',
499+
resolve: { tsconfigFilename: './legacy.tsconfig.json' },
500+
},
501+
},
502+
})
503+
const options = resolveRolldownOptions(
504+
builder.environments.client,
505+
new ChunkMetadataMap(),
506+
)
507+
expect(options.tsconfig).toBe(
508+
normalizePath(resolve(buildProjectRoot, 'custom.tsconfig.json')),
509+
)
510+
expect(options.resolve?.tsconfigFilename).toBeUndefined()
511+
})
512+
491513
test('falls back to index.html when no input is set', async () => {
492514
const builder = await createBuilder({
493515
root: buildProjectRoot,

‎packages/vite/src/node/__tests__/config.spec.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,31 @@ describe('resolveConfig', () => {
11541154
expect(results2.clearScreen).toBe(false)
11551155
})
11561156

1157+
test('resolves the configured tsconfig path', async () => {
1158+
const root = path.resolve(import.meta.dirname, 'fixtures')
1159+
const resolved = await resolveConfig(
1160+
{ root, tsconfig: './custom.tsconfig.json' },
1161+
'build',
1162+
)
1163+
expect(resolved.tsconfig).toBe(
1164+
normalizePath(path.resolve(root, 'custom.tsconfig.json')),
1165+
)
1166+
1167+
const absoluteTsconfig = path.resolve(root, 'absolute.tsconfig.json')
1168+
expect(
1169+
(
1170+
await resolveConfig(
1171+
{ root, tsconfig: absoluteTsconfig, configFile: false },
1172+
'build',
1173+
)
1174+
).tsconfig,
1175+
).toBe(normalizePath(absoluteTsconfig))
1176+
1177+
expect(
1178+
(await resolveConfig({ root, configFile: false }, 'build')).tsconfig,
1179+
).toBeUndefined()
1180+
})
1181+
11571182
test('resolveConfig with root path including "#" and "?" and "*" should warn ', async () => {
11581183
expect.assertions(1)
11591184

‎packages/vite/src/node/__tests__/plugins/oxc.spec.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path'
22
import { describe, expect, test } from 'vitest'
3+
import { resolveConfig } from '../../config'
34
import { transformWithOxc } from '../../plugins/oxc'
45

56
describe('transformWithOxc', () => {
@@ -153,4 +154,37 @@ describe('transformWithOxc', () => {
153154
)
154155
expect(result?.code).toContain('_decorateMetadata("design:type"')
155156
})
157+
158+
test('uses the configured tsconfig instead of automatic discovery', async () => {
159+
const code = `
160+
class Foo {
161+
bar = 'bar'
162+
}
163+
`
164+
const fixtures = path.resolve(
165+
import.meta.dirname,
166+
'./fixtures/oxc-tsconfigs',
167+
)
168+
const explicitTsconfig = path.resolve(
169+
fixtures,
170+
'use-define-false/tsconfig.json',
171+
)
172+
const config = await resolveConfig(
173+
{ root: fixtures, tsconfig: explicitTsconfig, configFile: false },
174+
'serve',
175+
)
176+
const expected = await transformWithOxc(
177+
code,
178+
path.resolve(fixtures, 'use-define-false/bar.ts'),
179+
{ target: 'esnext' },
180+
)
181+
const actual = await transformWithOxc(
182+
code,
183+
path.resolve(fixtures, 'use-define-true/bar.ts'),
184+
{ target: 'esnext' },
185+
undefined,
186+
config,
187+
)
188+
expect(actual.code).toBe(expected.code)
189+
})
156190
})

‎packages/vite/src/node/build.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,13 @@ export function resolveRolldownOptions(
662662
: false,
663663
// cache: options.watch ? undefined : false,
664664
...options.rolldownOptions,
665+
tsconfig: environment.config.tsconfig ?? options.rolldownOptions.tsconfig,
666+
resolve: environment.config.tsconfig
667+
? {
668+
...options.rolldownOptions.resolve,
669+
tsconfigFilename: undefined,
670+
}
671+
: options.rolldownOptions.resolve,
665672
output: options.rolldownOptions.output,
666673
input,
667674
plugins,

‎packages/vite/src/node/config.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ export interface UserConfig extends DefaultEnvironmentOptions {
374374
* @default process.cwd()
375375
*/
376376
root?: string
377+
/**
378+
* Path to the TypeScript configuration file. Relative paths are resolved
379+
* from the project root.
380+
*/
381+
tsconfig?: string
377382
/**
378383
* Base public path when served in development or production.
379384
* @default '/'
@@ -2032,6 +2037,9 @@ export async function resolveConfig(
20322037
),
20332038
inlineConfig,
20342039
root: resolvedRoot,
2040+
tsconfig: config.tsconfig
2041+
? normalizePath(path.resolve(resolvedRoot, config.tsconfig))
2042+
: undefined,
20352043
base,
20362044
decodedBase: decodeBase(base),
20372045
rawBase: resolvedBase,

‎packages/vite/src/node/plugins/esbuild.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ export function getTSConfigResolutionCache(
468468
}
469469
let cache = tsconfigResolutionCacheMap.get(config)
470470
if (!cache) {
471-
cache = new TsconfigCache()
471+
cache = new TsconfigCache(config.tsconfig)
472472
tsconfigResolutionCacheMap.set(config, cache)
473473
}
474474
return cache

‎packages/vite/src/node/plugins/oxc.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ export async function transformWithOxc(
153153
filename,
154154
code,
155155
resolvedOptions,
156-
getTSConfigResolutionCache(config),
156+
options?.tsconfig === undefined || options.tsconfig === config?.tsconfig
157+
? getTSConfigResolutionCache(config)
158+
: undefined,
157159
)
158160
if (
159161
watcher &&
@@ -292,6 +294,7 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
292294

293295
return nativeTransformPlugin({
294296
root: environment.config.root,
297+
tsconfig: environment.config.tsconfig,
295298
include,
296299
exclude,
297300
jsxRefreshInclude,

‎packages/vite/src/node/plugins/resolve.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ export function oxcResolvePlugin(
258258
: [options.noExternal]
259259

260260
const plugin = viteResolvePlugin({
261+
tsconfig: partialEnv.config.tsconfig,
261262
resolveOptions: {
262263
isBuild: options.isBuild,
263264
isProduction: options.isProduction,

0 commit comments

Comments
 (0)