@@ -7,9 +7,14 @@ import {join} from 'path';
77import batchdelcache from 'batchdelcache' ;
88
99interface IFileMap {
10- [ fileName : string ] : string ;
10+ [ fileName : string ] : IFileMapItem ;
1111}
1212
13+ type IFileMapItem = string | {
14+ key : string ;
15+ parents ?: string [ ] ;
16+ } ;
17+
1318export interface IOptions {
1419
1520 /* 项目所在根目录 */
@@ -52,18 +57,33 @@ export default class Reloader {
5257 }
5358
5459 reload ( newFileMap : IFileMap ) {
60+
5561 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+ }
5974 }
6075 }
76+
77+ // 删除缓存
6178 batchdelcache (
6279 Array . from ( reloadModules )
6380 ) ;
81+
82+ /* istanbul ignore next */
6483 if ( typeof global . gc === 'function' ) {
6584 global . gc ( ) ;
6685 }
86+
6787 const errors : IError [ ] = [ ] ;
6888 for ( const mod of reloadModules ) {
6989 try {
@@ -77,7 +97,9 @@ export default class Reloader {
7797 } ) ;
7898 }
7999 }
100+
80101 this . updateFileMap ( Object . assign ( this . fileMap , newFileMap ) ) ;
102+
81103 return {
82104 reloadModules : Array . from ( reloadModules ) ,
83105 errors,
@@ -89,6 +111,23 @@ export default class Reloader {
89111 this . updateFiles ( ) ;
90112 }
91113
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+
92131 private updateFiles ( ) {
93132 this . files = Object . keys ( this . fileMap ) ;
94133 }
0 commit comments