@@ -21,11 +21,14 @@ export interface IOptions {
21
21
context : string ;
22
22
23
23
/* 每个模块文件所对应的 md5 */
24
- fileMap : IFileMap ;
24
+ fileMap ? : IFileMap ;
25
25
26
- /* 过滤器,过滤需要 reload 的模块 */
26
+ /* 过滤器,调用 reload 时过滤需要 reload 的模块 */
27
27
filter ?: ( file : string ) => boolean ;
28
28
29
+ /* 过滤器,调用 reloadAll 时过滤需要 reload 的模块 */
30
+ filterAll ?: ( file : string ) => boolean ;
31
+
29
32
commonRootPath ?: string ;
30
33
}
31
34
@@ -40,6 +43,7 @@ export default class Reloader {
40
43
fileMap : IFileMap = { } ;
41
44
filter : ( file : string ) => boolean ;
42
45
commonRootPath : string ;
46
+ filterAll : ( file : string ) => boolean ;
43
47
44
48
files : string [ ] = [ ] ;
45
49
@@ -52,25 +56,27 @@ export default class Reloader {
52
56
if ( options . filter ) {
53
57
this . filter = options . filter ;
54
58
}
59
+ this . filterAll = ( ( ) => false ) ;
60
+ if ( options . filterAll ) {
61
+ this . filterAll = options . filterAll ;
62
+ }
55
63
this . commonRootPath = options . commonRootPath || '' ;
56
64
this . updateFiles ( ) ;
57
65
}
58
66
59
- reloadAll ( newFileMap : IFileMap ) {
67
+ reloadAll ( ) {
60
68
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 ] ) {
69
+ for ( const moduleId of Object . keys ( require . cache ) ) {
70
+ if ( this . filterAll ( moduleId ) ) {
64
71
reloadModules . add ( moduleId ) ;
65
72
}
66
73
}
67
74
const modulesToReload = Array . from ( reloadModules ) ;
68
75
if ( modulesToReload . length > 0 ) {
69
76
this . del ( modulesToReload ) ;
70
77
}
71
- this . updateFileMap ( Object . assign ( this . fileMap , newFileMap ) ) ;
72
78
return {
73
- reloadModules : modulesToReload . map ( a => a . replace ( this . context + sep , '' ) ) ,
79
+ reloadModules : modulesToReload ,
74
80
errors : [ ] ,
75
81
} ;
76
82
}
0 commit comments