@@ -17,8 +17,9 @@ import {
1717 createFilter ,
1818 ensureWatchedFile ,
1919 generateCodeFrame ,
20+ normalizePath ,
2021} from '../utils'
21- import type { ViteDevServer } from '../server'
22+
2223import type { ResolvedConfig } from '../config'
2324import type { Plugin } from '../plugin'
2425import { 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