Skip to content

Commit 955c445

Browse files
committed
feat: support reloadAll
1 parent a155600 commit 955c445

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

src/index.ts

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ export default class Reloader {
5656
this.updateFiles();
5757
}
5858

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+
5978
reload(newFileMap: IFileMap) {
6079

6180
const reloadModules = new Set<string>();
@@ -75,30 +94,23 @@ export default class Reloader {
7594

7695
const modulesToReload = Array.from(reloadModules);
7796

97+
const errors: IError[] = [];
98+
7899
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-
}
90100

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+
}
102114
}
103115
}
104116

@@ -115,6 +127,19 @@ export default class Reloader {
115127
this.updateFiles();
116128
}
117129

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+
118143
private getKey(item: IFileMapItem): string {
119144
if (typeof item === 'string') {
120145
return item;

0 commit comments

Comments
 (0)