Skip to content

Commit 4a1bc4c

Browse files
committed
feat: change filter to params
1 parent 75745db commit 4a1bc4c

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

src/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ export interface IOptions {
2626
/* 过滤器,调用 reload 时过滤需要 reload 的模块 */
2727
filter?: (file: string) => boolean;
2828

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

@@ -43,7 +40,6 @@ export default class Reloader {
4340
fileMap: IFileMap = {};
4441
filter: (file: string) => boolean;
4542
commonRootPath: string;
46-
filterAll: (file: string) => boolean;
4743

4844
files: string[] = [];
4945

@@ -56,18 +52,14 @@ export default class Reloader {
5652
if (options.filter) {
5753
this.filter = options.filter;
5854
}
59-
this.filterAll = (() => false);
60-
if (options.filterAll) {
61-
this.filterAll = options.filterAll;
62-
}
6355
this.commonRootPath = options.commonRootPath || '';
6456
this.updateFiles();
6557
}
6658

67-
reloadAll() {
59+
reloadAll(filter: (file: string) => boolean) {
6860
const reloadModules = new Set<string>();
6961
for (const moduleId of Object.keys(require.cache)) {
70-
if (this.filterAll(moduleId)) {
62+
if (filter(moduleId)) {
7163
reloadModules.add(moduleId);
7264
}
7365
}

test/index.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ describe('Reloader test', () => {
6767

6868
const reloader = new Reloader({
6969
context: resolve(__dirname, './fixtures'),
70-
commonRootPath: resolve(__dirname, './fixtures/mainModule.js'),
71-
filterAll: (id) => id.endsWith('fixtures/mod1.js')
70+
commonRootPath: resolve(__dirname, './fixtures/mainModule.js')
7271
});
7372

7473
require('./fixtures/mod1').num++;
@@ -77,7 +76,9 @@ describe('Reloader test', () => {
7776
expect(require('./fixtures/mod1').num).to.be.equal(2);
7877
expect(require('./fixtures/mod2').num).to.be.equal(3);
7978

80-
let {errors, reloadModules} = reloader.reloadAll();
79+
let {errors, reloadModules} = reloader.reloadAll(
80+
id => id.endsWith('fixtures/mod1.js')
81+
);
8182

8283
expect(errors.length).to.be.equal(0);
8384
expect(reloadModules.length).to.be.equal(1);

0 commit comments

Comments
 (0)