Skip to content

Commit 83b27a5

Browse files
dominikgsapphi-red
authored andcommitted
fix: handle tsconfig watching without watchChange as tsconfig files are not modules that pass through it
1 parent 8b012c7 commit 83b27a5

File tree

1 file changed

+37
-44
lines changed

1 file changed

+37
-44
lines changed

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

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import {
1717
createFilter,
1818
ensureWatchedFile,
1919
generateCodeFrame,
20+
normalizePath,
2021
} from '../utils'
21-
import type { ViteDevServer } from '../server'
22+
2223
import type { ResolvedConfig } from '../config'
2324
import type { Plugin } from '../plugin'
2425
import { cleanUrl } from '../../shared/utils'
@@ -251,52 +252,44 @@ export function esbuildPlugin(config: ResolvedConfig): Plugin {
251252
},
252253
}
253254

254-
let server: ViteDevServer
255-
256255
return {
257256
name: 'vite:esbuild',
258-
configureServer(_server) {
259-
server = _server
260-
},
261-
watchChange(id) {
262-
// any tsconfig.json that's added in the workspace could be closer to a code file than a previously cached one
263-
// any json file in the tsconfig cache could have been used to compile ts
264-
if (
265-
path.basename(id) === 'tsconfig.json' ||
266-
id.endsWith('.json') /*
267-
TODO: the tsconfckCache?.clear() line will make this fail if there are several servers
268-
we may need a cache per server if we don't want all servers to share the reset
269-
leaving it commented for now because it should still work
270-
&& tsconfckCache?.hasParseResult(changedFile)
271-
*/
272-
) {
273-
server.config.logger.info(
274-
`changed tsconfig file detected: ${id} - Clearing cache and forcing full-reload to ensure TypeScript is compiled with updated config values.`,
275-
{ clear: server.config.clearScreen, timestamp: true },
276-
)
277-
278-
// TODO: more finegrained invalidation than the nuclear option below
279-
280-
// clear module graph to remove code compiled with outdated config
281-
for (const environment of Object.values(server.environments)) {
282-
environment.moduleGraph.invalidateAll()
283-
}
284-
285-
// reset tsconfck cache so that recompile works with up2date configs
286-
tsconfckCache?.clear()
287-
288-
// reload environments
289-
for (const environment of Object.values(server.environments)) {
290-
environment.hot.send({
291-
type: 'full-reload',
292-
path: '*',
293-
})
257+
configureServer(server) {
258+
server.watcher.on('all', (event, file) => {
259+
if (
260+
(event === 'add' || event === 'change' || event === 'unlink') &&
261+
file.slice(-5) === '.json'
262+
) {
263+
const filename = normalizePath(file)
264+
if (
265+
filename.slice(-14) === '/tsconfig.json' ||
266+
tsconfckCache?.hasParseResult(filename)
267+
) {
268+
server.config.logger.info(
269+
`changed tsconfig file detected: ${filename} - Clearing cache and forcing full-reload to ensure TypeScript is compiled with updated config values.`,
270+
{ clear: server.config.clearScreen, timestamp: true },
271+
)
272+
273+
// TODO: more finegrained invalidation than the nuclear option below
274+
275+
// clear module graph to remove code compiled with outdated config
276+
for (const environment of Object.values(server.environments)) {
277+
environment.moduleGraph.invalidateAll()
278+
}
279+
280+
// reset tsconfck cache so that recompile works with up2date configs
281+
tsconfckCache?.clear()
282+
283+
// reload environments
284+
for (const environment of Object.values(server.environments)) {
285+
environment.hot.send({
286+
type: 'full-reload',
287+
path: '*',
288+
})
289+
}
290+
}
294291
}
295-
}
296-
},
297-
buildEnd() {
298-
// recycle serve to avoid preventing Node self-exit (#6815)
299-
server = null as any
292+
})
300293
},
301294
async transform(code, id) {
302295
if (filter(id) || filter(cleanUrl(id))) {

0 commit comments

Comments
 (0)