Skip to content

Commit d89fb24

Browse files
committed
refactor: update reloadAll
1 parent 955c445 commit d89fb24

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ export interface IOptions {
2121
context: string;
2222

2323
/* 每个模块文件所对应的 md5 */
24-
fileMap: IFileMap;
24+
fileMap?: IFileMap;
2525

26-
/* 过滤器,过滤需要 reload 的模块 */
26+
/* 过滤器,调用 reload 时过滤需要 reload 的模块 */
2727
filter?: (file: string) => boolean;
2828

29+
/* 过滤器,调用 reloadAll 时过滤需要 reload 的模块 */
30+
filterAll?: (file: string) => boolean;
31+
2932
commonRootPath?: string;
3033
}
3134

@@ -40,6 +43,7 @@ export default class Reloader {
4043
fileMap: IFileMap = {};
4144
filter: (file: string) => boolean;
4245
commonRootPath: string;
46+
filterAll: (file: string) => boolean;
4347

4448
files: string[] = [];
4549

@@ -52,25 +56,27 @@ export default class Reloader {
5256
if (options.filter) {
5357
this.filter = options.filter;
5458
}
59+
this.filterAll = (() => false);
60+
if (options.filterAll) {
61+
this.filterAll = options.filterAll;
62+
}
5563
this.commonRootPath = options.commonRootPath || '';
5664
this.updateFiles();
5765
}
5866

59-
reloadAll(newFileMap: IFileMap) {
67+
reloadAll() {
6068
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)) {
6471
reloadModules.add(moduleId);
6572
}
6673
}
6774
const modulesToReload = Array.from(reloadModules);
6875
if (modulesToReload.length > 0) {
6976
this.del(modulesToReload);
7077
}
71-
this.updateFileMap(Object.assign(this.fileMap, newFileMap));
7278
return {
73-
reloadModules: modulesToReload.map(a => a.replace(this.context + sep, '')),
79+
reloadModules: modulesToReload,
7480
errors: [],
7581
};
7682
}

test/index.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@ describe('Reloader test', () => {
6262
expect(require('./fixtures/mod2').num).to.be.equal(2);
6363
});
6464

65+
it('reload all success', () => {
66+
require('./fixtures/mainModule');
67+
68+
const reloader = new Reloader({
69+
context: resolve(__dirname, './fixtures'),
70+
commonRootPath: resolve(__dirname, './fixtures/mainModule.js'),
71+
filterAll: (id) => id.endsWith('fixtures/mod1.js')
72+
});
73+
74+
require('./fixtures/mod1').num++;
75+
require('./fixtures/mod2').num++;
76+
77+
expect(require('./fixtures/mod1').num).to.be.equal(2);
78+
expect(require('./fixtures/mod2').num).to.be.equal(3);
79+
80+
let {errors, reloadModules} = reloader.reloadAll();
81+
82+
expect(errors.length).to.be.equal(0);
83+
expect(reloadModules.length).to.be.equal(1);
84+
expect(require('./fixtures/mod1').num).to.be.equal(1);
85+
expect(require('./fixtures/mod2').num).to.be.equal(3);
86+
});
87+
6588
it('reload success with parents', () => {
6689
require('./fixtures/mainModule');
6790

0 commit comments

Comments
 (0)