Skip to content

Commit

Permalink
feat: merge parents
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Jul 29, 2020
1 parent af08cd6 commit 8c25f39
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
31 changes: 18 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author cxtom([email protected])
*/

import {join} from 'path';
import {join, sep} from 'path';
import batchdelcache from 'batchdelcache';

interface IFileMap {
Expand Down Expand Up @@ -65,26 +65,31 @@ export default class Reloader {
const md5 = this.getKey(item);
if (hasKey && this.getKey(this.fileMap[name]) !== md5 && this.filter.call(this, name)) {
const parents = this.getParents(item);
if (parents.length > 0) {
parents.forEach(filename => reloadModules.add(join(this.context, filename)));
const prevParents = this.getParents(this.fileMap[name]);
if (parents.length > 0 || prevParents.length > 0) {
[...parents, ...prevParents].forEach(filename => reloadModules.add(join(this.context, filename)));
}
reloadModules.add(join(this.context, name));
}
}

// 删除缓存
batchdelcache(
Array.from(reloadModules),
true, this.commonRootPath
);
const modulesToReload = Array.from(reloadModules);

/* istanbul ignore next */
if (typeof global.gc === 'function') {
global.gc();
if (modulesToReload.length > 0) {
// 删除缓存
batchdelcache(
modulesToReload,
true, this.commonRootPath
);

/* istanbul ignore next */
if (typeof global.gc === 'function') {
global.gc();
}
}

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

return {
reloadModules: Array.from(reloadModules),
reloadModules: modulesToReload.map(a => a.replace(this.context + sep, '')),
errors,
};
}
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('Reloader test', () => {

expect(errors.length).to.be.equal(0);
expect(reloadModules.length).to.be.equal(2);
expect(reloadModules.includes(resolve(__dirname, './fixtures/mod2.js'))).to.be.equal(true);
expect(reloadModules.includes('mod2.js')).to.be.equal(true);
expect(require('./fixtures/mod2').num).to.be.equal(2);
expect(require('./fixtures/mod3').num).to.be.equal(3);

Expand Down

0 comments on commit 8c25f39

Please sign in to comment.