@@ -7,9 +7,14 @@ import {join} from 'path';
7
7
import batchdelcache from 'batchdelcache' ;
8
8
9
9
interface IFileMap {
10
- [ fileName : string ] : string ;
10
+ [ fileName : string ] : IFileMapItem ;
11
11
}
12
12
13
+ type IFileMapItem = string | {
14
+ key : string ;
15
+ parents ?: string [ ] ;
16
+ } ;
17
+
13
18
export interface IOptions {
14
19
15
20
/* 项目所在根目录 */
@@ -52,18 +57,33 @@ export default class Reloader {
52
57
}
53
58
54
59
reload ( newFileMap : IFileMap ) {
60
+
55
61
const reloadModules = new Set < string > ( ) ;
56
- for ( const [ name , md5 ] of Object . entries ( newFileMap ) ) {
57
- if ( this . filter ( name ) && ( name in this . fileMap ) && this . fileMap [ name ] !== md5 ) {
58
- reloadModules . add ( require . resolve ( join ( this . context , name ) ) ) ;
62
+
63
+ for ( const [ name , item ] of Object . entries ( newFileMap ) ) {
64
+ const hasKey = name in this . fileMap ;
65
+ const md5 = this . getKey ( item ) ;
66
+ if ( hasKey && this . getKey ( this . fileMap [ name ] ) !== md5 && this . filter ( name ) ) {
67
+ const parents = this . getParents ( item ) ;
68
+ if ( parents . length > 0 ) {
69
+ parents . forEach ( filename => reloadModules . add ( join ( this . context , filename ) ) ) ;
70
+ }
71
+ else {
72
+ reloadModules . add ( join ( this . context , name ) ) ;
73
+ }
59
74
}
60
75
}
76
+
77
+ // 删除缓存
61
78
batchdelcache (
62
79
Array . from ( reloadModules )
63
80
) ;
81
+
82
+ /* istanbul ignore next */
64
83
if ( typeof global . gc === 'function' ) {
65
84
global . gc ( ) ;
66
85
}
86
+
67
87
const errors : IError [ ] = [ ] ;
68
88
for ( const mod of reloadModules ) {
69
89
try {
@@ -77,7 +97,9 @@ export default class Reloader {
77
97
} ) ;
78
98
}
79
99
}
100
+
80
101
this . updateFileMap ( Object . assign ( this . fileMap , newFileMap ) ) ;
102
+
81
103
return {
82
104
reloadModules : Array . from ( reloadModules ) ,
83
105
errors,
@@ -89,6 +111,23 @@ export default class Reloader {
89
111
this . updateFiles ( ) ;
90
112
}
91
113
114
+ private getKey ( item : IFileMapItem ) : string {
115
+ if ( typeof item === 'string' ) {
116
+ return item ;
117
+ }
118
+ else if ( item ) {
119
+ return item . key ;
120
+ }
121
+ return '' ;
122
+ }
123
+
124
+ private getParents ( item : IFileMapItem ) : string [ ] {
125
+ if ( typeof item === 'object' && item . parents ) {
126
+ return item . parents ;
127
+ }
128
+ return [ ] ;
129
+ }
130
+
92
131
private updateFiles ( ) {
93
132
this . files = Object . keys ( this . fileMap ) ;
94
133
}
0 commit comments