Skip to content

Commit 8c25f39

Browse files
committed
feat: merge parents
1 parent af08cd6 commit 8c25f39

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/index.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author cxtom([email protected])
44
*/
55

6-
import {join} from 'path';
6+
import {join, sep} from 'path';
77
import batchdelcache from 'batchdelcache';
88

99
interface IFileMap {
@@ -65,26 +65,31 @@ export default class Reloader {
6565
const md5 = this.getKey(item);
6666
if (hasKey && this.getKey(this.fileMap[name]) !== md5 && this.filter.call(this, name)) {
6767
const parents = this.getParents(item);
68-
if (parents.length > 0) {
69-
parents.forEach(filename => reloadModules.add(join(this.context, filename)));
68+
const prevParents = this.getParents(this.fileMap[name]);
69+
if (parents.length > 0 || prevParents.length > 0) {
70+
[...parents, ...prevParents].forEach(filename => reloadModules.add(join(this.context, filename)));
7071
}
7172
reloadModules.add(join(this.context, name));
7273
}
7374
}
7475

75-
// 删除缓存
76-
batchdelcache(
77-
Array.from(reloadModules),
78-
true, this.commonRootPath
79-
);
76+
const modulesToReload = Array.from(reloadModules);
8077

81-
/* istanbul ignore next */
82-
if (typeof global.gc === 'function') {
83-
global.gc();
78+
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+
}
8489
}
8590

8691
const errors: IError[] = [];
87-
for (const mod of reloadModules) {
92+
for (const mod of modulesToReload) {
8893
try {
8994
// eslint-disable-next-line @typescript-eslint/no-require-imports
9095
require(mod);
@@ -100,7 +105,7 @@ export default class Reloader {
100105
this.updateFileMap(Object.assign(this.fileMap, newFileMap));
101106

102107
return {
103-
reloadModules: Array.from(reloadModules),
108+
reloadModules: modulesToReload.map(a => a.replace(this.context + sep, '')),
104109
errors,
105110
};
106111
}

test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('Reloader test', () => {
9595

9696
expect(errors.length).to.be.equal(0);
9797
expect(reloadModules.length).to.be.equal(2);
98-
expect(reloadModules.includes(resolve(__dirname, './fixtures/mod2.js'))).to.be.equal(true);
98+
expect(reloadModules.includes('mod2.js')).to.be.equal(true);
9999
expect(require('./fixtures/mod2').num).to.be.equal(2);
100100
expect(require('./fixtures/mod3').num).to.be.equal(3);
101101

0 commit comments

Comments
 (0)