1
1
import type { Compiler } from '@farmfe/core' ;
2
2
import EventEmitter from 'node:events' ;
3
- import { proxyCompilerFn } from './util' ;
3
+ import { proxyCompilerFn , defineProperty } from './util' ;
4
4
import type { FnContext , OmitFnReturn } from './interface' ;
5
5
6
6
export class ProxyCompiler {
@@ -10,6 +10,7 @@ export class ProxyCompiler {
10
10
11
11
private _preProxyFnList : ( keyof Compiler ) [ ] = [ ] ;
12
12
private alreadyProxyFnList : Set < keyof Compiler > = new Set ( ) ;
13
+ isDisableEmit = false ;
13
14
14
15
start ( compiler : Compiler ) {
15
16
const isRestart = ! ! this . compiler ;
@@ -32,13 +33,13 @@ export class ProxyCompiler {
32
33
}
33
34
}
34
35
35
- private proxyCompiler < K extends keyof Compiler > ( fnName : K ) {
36
+ private proxyCompiler < K extends keyof Compiler > ( fnName : K , force = false ) {
36
37
if ( ! this . compiler ) {
37
38
this . _preProxyFnList . push ( fnName ) ;
38
39
return ;
39
40
}
40
41
41
- if ( this . alreadyProxyFnList . has ( fnName ) ) {
42
+ if ( ! force && this . alreadyProxyFnList . has ( fnName ) ) {
42
43
return ;
43
44
}
44
45
@@ -48,6 +49,12 @@ export class ProxyCompiler {
48
49
proxyCompilerFn ( this . compiler , fnName , ( ...args : any [ ] ) => this . event . emit ( fnName , ...args ) ) ;
49
50
}
50
51
52
+ private replaceCompiler < K extends keyof Compiler > ( fnName : K , fn : Compiler [ K ] ) {
53
+ defineProperty ( this . compiler , fnName , fn ) ;
54
+ // re proxy the compiler
55
+ this . proxyCompiler ( fnName , true ) ;
56
+ }
57
+
51
58
private on <
52
59
T extends Compiler ,
53
60
K extends keyof OFT ,
@@ -67,4 +74,15 @@ export class ProxyCompiler {
67
74
onWriteResourcesToDisk ( handler : OmitFnReturn < Compiler [ 'writeResourcesToDisk' ] > ) {
68
75
return this . on ( 'writeResourcesToDisk' , ( r ) => handler ( ...r . args ) ) ;
69
76
}
77
+
78
+ disableEmit ( ) {
79
+ if ( this . isDisableEmit ) return ;
80
+ const handler = ( ) => Promise . resolve ( ) ;
81
+ this . replaceCompiler ( 'writeResourcesToDisk' , handler ) ;
82
+ this . isDisableEmit = true ;
83
+ }
84
+
85
+ resources ( ) {
86
+ return this . compiler . resources ( ) ;
87
+ }
70
88
}
0 commit comments