Skip to content

Commit e56103f

Browse files
authored
refactor: use rolldown/utils (vitejs#21577)
1 parent 1ee5c7f commit e56103f

File tree

7 files changed

+38
-318
lines changed

7 files changed

+38
-318
lines changed

packages/vite/LICENSE.md

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,60 +1846,6 @@ Repository: https://github.com/micromatch/to-regex-range
18461846
18471847
---------------------------------------
18481848

1849-
## tsconfck
1850-
License: MIT
1851-
By: dominikg
1852-
Repository: https://github.com/dominikg/tsconfck
1853-
1854-
> MIT License
1855-
>
1856-
> Copyright (c) 2021-present dominikg and [contributors](https://github.com/dominikg/tsconfck/graphs/contributors)
1857-
>
1858-
> Permission is hereby granted, free of charge, to any person obtaining a copy
1859-
> of this software and associated documentation files (the "Software"), to deal
1860-
> in the Software without restriction, including without limitation the rights
1861-
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1862-
> copies of the Software, and to permit persons to whom the Software is
1863-
> furnished to do so, subject to the following conditions:
1864-
>
1865-
> The above copyright notice and this permission notice shall be included in all
1866-
> copies or substantial portions of the Software.
1867-
>
1868-
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1869-
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1870-
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1871-
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1872-
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1873-
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1874-
> SOFTWARE.
1875-
>
1876-
> -- Licenses for 3rd-party code included in tsconfck --
1877-
>
1878-
> # strip-bom and strip-json-comments
1879-
> MIT License
1880-
>
1881-
> Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
1882-
>
1883-
> Permission is hereby granted, free of charge, to any person obtaining a copy
1884-
> of this software and associated documentation files (the "Software"), to deal
1885-
> in the Software without restriction, including without limitation the rights
1886-
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1887-
> copies of the Software, and to permit persons to whom the Software is
1888-
> furnished to do so, subject to the following conditions:
1889-
>
1890-
> The above copyright notice and this permission notice shall be included in all
1891-
> copies or substantial portions of the Software.
1892-
>
1893-
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1894-
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1895-
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1896-
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1897-
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1898-
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1899-
> SOFTWARE.
1900-
1901-
---------------------------------------
1902-
19031849
## unpipe
19041850
License: MIT
19051851
By: Douglas Christopher Wilson

packages/vite/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@
137137
"sirv": "^3.0.2",
138138
"strip-literal": "^3.1.0",
139139
"terser": "^5.46.0",
140-
"tsconfck": "^3.1.6",
141140
"ufo": "^1.6.3",
142141
"ws": "^8.19.0"
143142
},

packages/vite/src/node/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export {
1616
minifySync,
1717
type MinifyOptions,
1818
type MinifyResult,
19-
} from 'rolldown/experimental'
19+
} from 'rolldown/utils'
2020

2121
/** @deprecated - use `parse` instead */
2222
export const parseAst: typeof _parseAst = _parseAst

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

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import path from 'node:path'
22
import colors from 'picocolors'
33
import type { RawSourceMap } from '@jridgewell/remapping'
44
import type { InternalModuleFormat, SourceMap } from 'rolldown'
5-
import type { TSConfckParseResult } from 'tsconfck'
6-
import { TSConfckCache, TSConfckParseError, parse } from 'tsconfck'
5+
import { resolveTsconfig } from 'rolldown/experimental'
6+
import { TsconfigCache } from 'rolldown/utils'
77
import type {
88
EsbuildLoader,
99
EsbuildMessage,
@@ -145,13 +145,19 @@ export async function transformWithEsbuild(
145145
]
146146
const compilerOptionsForFile: TSCompilerOptions = {}
147147
if (loader === 'ts' || loader === 'tsx') {
148-
try {
149-
const { tsconfig: loadedTsconfig, tsconfigFile } =
150-
await loadTsconfigJsonForFile(filename, config)
148+
const result = resolveTsconfig(
149+
filename,
150+
getTSConfigResolutionCache(config),
151+
)
152+
if (result) {
153+
const { tsconfig: loadedTsconfig, tsconfigFilePaths } = result
151154
// tsconfig could be out of root, make sure it is watched on dev
152-
if (watcher && tsconfigFile && config) {
153-
ensureWatchedFile(watcher, tsconfigFile, config.root)
155+
if (watcher && config) {
156+
for (const tsconfigFile of tsconfigFilePaths) {
157+
ensureWatchedFile(watcher, tsconfigFile, config.root)
158+
}
154159
}
160+
155161
const loadedCompilerOptions = loadedTsconfig.compilerOptions ?? {}
156162

157163
for (const field of meaningfulFields) {
@@ -160,14 +166,6 @@ export async function transformWithEsbuild(
160166
compilerOptionsForFile[field] = loadedCompilerOptions[field]
161167
}
162168
}
163-
} catch (e) {
164-
if (e instanceof TSConfckParseError) {
165-
// tsconfig could be out of root, make sure it is watched on dev
166-
if (watcher && e.tsconfigFile && config) {
167-
ensureWatchedFile(watcher, e.tsconfigFile, config.root)
168-
}
169-
}
170-
throw e
171169
}
172170
}
173171

@@ -522,47 +520,30 @@ function prettifyMessage(m: EsbuildMessage, code: string): string {
522520
return res + `\n`
523521
}
524522

525-
let globalTSConfckCache: TSConfckCache<TSConfckParseResult> | undefined
526-
const tsconfckCacheMap = new WeakMap<
527-
ResolvedConfig,
528-
TSConfckCache<TSConfckParseResult>
529-
>()
523+
let globalTSConfigResolutionCache: TsconfigCache | undefined
524+
const tsconfigResolutionCacheMap = new WeakMap<ResolvedConfig, TsconfigCache>()
530525

531-
function getTSConfckCache(config?: ResolvedConfig) {
526+
function getTSConfigResolutionCache(config?: ResolvedConfig) {
532527
if (!config) {
533-
return (globalTSConfckCache ??= new TSConfckCache<TSConfckParseResult>())
528+
return (globalTSConfigResolutionCache ??= new TsconfigCache())
534529
}
535-
let cache = tsconfckCacheMap.get(config)
530+
let cache = tsconfigResolutionCacheMap.get(config)
536531
if (!cache) {
537-
cache = new TSConfckCache<TSConfckParseResult>()
538-
tsconfckCacheMap.set(config, cache)
532+
cache = new TsconfigCache()
533+
tsconfigResolutionCacheMap.set(config, cache)
539534
}
540535
return cache
541536
}
542537

543-
export async function loadTsconfigJsonForFile(
544-
filename: string,
545-
config?: ResolvedConfig,
546-
): Promise<{ tsconfigFile: string; tsconfig: TSConfigJSON }> {
547-
const { tsconfig, tsconfigFile } = await parse(filename, {
548-
cache: getTSConfckCache(config),
549-
ignoreNodeModules: true,
550-
})
551-
return { tsconfigFile, tsconfig }
552-
}
553-
554538
export async function reloadOnTsconfigChange(
555539
server: ViteDevServer,
556540
changedFile: string,
557541
): Promise<void> {
558542
// any tsconfig.json that's added in the workspace could be closer to a code file than a previously cached one
559543
// any json file in the tsconfig cache could have been used to compile ts
560544
if (changedFile.endsWith('.json')) {
561-
const cache = getTSConfckCache(server.config)
562-
if (
563-
changedFile.endsWith('/tsconfig.json') ||
564-
cache.hasParseResult(changedFile)
565-
) {
545+
const cache = getTSConfigResolutionCache(server.config)
546+
if (changedFile.endsWith('/tsconfig.json') || cache.size() > 0) {
566547
server.config.logger.info(
567548
`changed tsconfig file detected: ${changedFile} - Clearing cache and forcing full-reload to ensure TypeScript is compiled with updated config values.`,
568549
{ clear: server.config.clearScreen, timestamp: true },
@@ -575,7 +556,7 @@ export async function reloadOnTsconfigChange(
575556
environment.moduleGraph.invalidateAll()
576557
}
577558

578-
// reset tsconfck cache so that recompile works with up2date configs
559+
// reset the cache so that recompile works with up2date configs
579560
cache.clear()
580561

581562
// reload environments

0 commit comments

Comments
 (0)