@@ -56,6 +56,25 @@ export default class Reloader {
56
56
this . updateFiles ( ) ;
57
57
}
58
58
59
+ reloadAll ( newFileMap : IFileMap ) {
60
+ const reloadModules = new Set < string > ( ) ;
61
+ for ( const [ name ] of Object . entries ( this . fileMap ) ) {
62
+ const moduleId = require . resolve ( join ( this . context , name ) ) ;
63
+ if ( require . cache [ moduleId ] ) {
64
+ reloadModules . add ( moduleId ) ;
65
+ }
66
+ }
67
+ const modulesToReload = Array . from ( reloadModules ) ;
68
+ if ( modulesToReload . length > 0 ) {
69
+ this . del ( modulesToReload ) ;
70
+ }
71
+ this . updateFileMap ( Object . assign ( this . fileMap , newFileMap ) ) ;
72
+ return {
73
+ reloadModules : modulesToReload . map ( a => a . replace ( this . context + sep , '' ) ) ,
74
+ errors : [ ] ,
75
+ } ;
76
+ }
77
+
59
78
reload ( newFileMap : IFileMap ) {
60
79
61
80
const reloadModules = new Set < string > ( ) ;
@@ -75,30 +94,23 @@ export default class Reloader {
75
94
76
95
const modulesToReload = Array . from ( reloadModules ) ;
77
96
97
+ const errors : IError [ ] = [ ] ;
98
+
78
99
if ( modulesToReload . length > 0 ) {
79
- // 删除缓存
80
- batchdelcache (
81
- modulesToReload ,
82
- true , this . commonRootPath
83
- ) ;
84
-
85
- /* istanbul ignore next */
86
- if ( typeof global . gc === 'function' ) {
87
- global . gc ( ) ;
88
- }
89
- }
90
100
91
- const errors : IError [ ] = [ ] ;
92
- for ( const mod of modulesToReload ) {
93
- try {
94
- // eslint-disable-next-line @typescript-eslint/no-require-imports
95
- require ( mod ) ;
96
- }
97
- catch ( e ) {
98
- errors . push ( {
99
- file : mod ,
100
- code : 1 ,
101
- } ) ;
101
+ this . del ( modulesToReload ) ;
102
+
103
+ for ( const mod of modulesToReload ) {
104
+ try {
105
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
106
+ require ( mod ) ;
107
+ }
108
+ catch ( e ) {
109
+ errors . push ( {
110
+ file : mod ,
111
+ code : 1 ,
112
+ } ) ;
113
+ }
102
114
}
103
115
}
104
116
@@ -115,6 +127,19 @@ export default class Reloader {
115
127
this . updateFiles ( ) ;
116
128
}
117
129
130
+ private del ( modulesToReload : string [ ] ) {
131
+ // 删除缓存
132
+ batchdelcache (
133
+ modulesToReload ,
134
+ true , this . commonRootPath
135
+ ) ;
136
+
137
+ /* istanbul ignore next */
138
+ if ( typeof global . gc === 'function' ) {
139
+ global . gc ( ) ;
140
+ }
141
+ }
142
+
118
143
private getKey ( item : IFileMapItem ) : string {
119
144
if ( typeof item === 'string' ) {
120
145
return item ;
0 commit comments