@@ -17,19 +17,37 @@ export function isVirtualModuleId(encoded: string, plugin: ResolvedUnpluginOptio
1717
1818export class FakeVirtualModulesPlugin {
1919 name = 'FakeVirtualModulesPlugin'
20- static counter = 0
20+ static counters : Map < string , number > = new Map < string , number > ( )
21+
22+ static {
23+ [ 'SIGINT' , 'SIGTERM' , 'SIGQUIT' , 'exit' ] . forEach ( ( event ) => {
24+ process . once ( event , ( ) => {
25+ this . counters . forEach ( ( _ , dir ) => {
26+ fs . rmSync ( dir , { recursive : true , force : true } )
27+ } )
28+ } )
29+ } )
30+ }
31+
2132 constructor ( private plugin : ResolvedUnpluginOptions ) { }
2233
2334 apply ( compiler : Compiler ) : void {
24- FakeVirtualModulesPlugin . counter ++
2535 const dir = this . plugin . __virtualModulePrefix
2636 if ( ! fs . existsSync ( dir ) ) {
2737 fs . mkdirSync ( dir , { recursive : true } )
2838 }
39+ const counter = FakeVirtualModulesPlugin . counters . get ( dir ) ?? 0
40+ FakeVirtualModulesPlugin . counters . set ( dir , counter + 1 )
41+
2942 compiler . hooks . shutdown . tap ( this . name , ( ) => {
30- if ( -- FakeVirtualModulesPlugin . counter === 0 ) {
43+ const counter = ( FakeVirtualModulesPlugin . counters . get ( dir ) ?? 1 ) - 1
44+ if ( counter === 0 ) {
45+ FakeVirtualModulesPlugin . counters . delete ( dir )
3146 fs . rmSync ( dir , { recursive : true , force : true } )
3247 }
48+ else {
49+ FakeVirtualModulesPlugin . counters . set ( dir , counter )
50+ }
3351 } )
3452 }
3553
0 commit comments