Skip to content

Commit 438b638

Browse files
committed
fix: support parent mod
1 parent 9c95653 commit 438b638

File tree

6 files changed

+104
-6
lines changed

6 files changed

+104
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"prebuild": "rimraf dist",
2424
"build": "tsc --module commonjs && rollup -c rollup.config.ts && typedoc --out docs --target es6 --theme minimal --mode file src",
2525
"start": "rollup -c rollup.config.ts -w",
26-
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' node --expose-gc ./node_modules/.bin/nyc mocha --require ts-node/register --require source-map-support/register --full-trace --bail test/index.test.ts",
26+
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' nyc mocha --require ts-node/register --require source-map-support/register --full-trace --bail test/index.test.ts",
2727
"deploy-docs": "ts-node tools/gh-pages-publish",
2828
"report-coverage": "cat ./coverage/lcov.info | coveralls",
2929
"commit": "git-cz",

src/index.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import {join} from 'path';
77
import batchdelcache from 'batchdelcache';
88

99
interface IFileMap {
10-
[fileName: string]: string;
10+
[fileName: string]: IFileMapItem;
1111
}
1212

13+
type IFileMapItem = string | {
14+
key: string;
15+
parents?: string[];
16+
};
17+
1318
export interface IOptions {
1419

1520
/* 项目所在根目录 */
@@ -52,18 +57,33 @@ export default class Reloader {
5257
}
5358

5459
reload(newFileMap: IFileMap) {
60+
5561
const reloadModules = new Set<string>();
56-
for (const [name, md5] of Object.entries(newFileMap)) {
57-
if (this.filter(name) && (name in this.fileMap) && this.fileMap[name] !== md5) {
58-
reloadModules.add(require.resolve(join(this.context, name)));
62+
63+
for (const [name, item] of Object.entries(newFileMap)) {
64+
const hasKey = name in this.fileMap;
65+
const md5 = this.getKey(item);
66+
if (hasKey && this.getKey(this.fileMap[name]) !== md5 && this.filter(name)) {
67+
const parents = this.getParents(item);
68+
if (parents.length > 0) {
69+
parents.forEach(filename => reloadModules.add(join(this.context, filename)));
70+
}
71+
else {
72+
reloadModules.add(join(this.context, name));
73+
}
5974
}
6075
}
76+
77+
// 删除缓存
6178
batchdelcache(
6279
Array.from(reloadModules)
6380
);
81+
82+
/* istanbul ignore next */
6483
if (typeof global.gc === 'function') {
6584
global.gc();
6685
}
86+
6787
const errors: IError[] = [];
6888
for (const mod of reloadModules) {
6989
try {
@@ -77,7 +97,9 @@ export default class Reloader {
7797
});
7898
}
7999
}
100+
80101
this.updateFileMap(Object.assign(this.fileMap, newFileMap));
102+
81103
return {
82104
reloadModules: Array.from(reloadModules),
83105
errors,
@@ -89,6 +111,23 @@ export default class Reloader {
89111
this.updateFiles();
90112
}
91113

114+
private getKey(item: IFileMapItem): string {
115+
if (typeof item === 'string') {
116+
return item;
117+
}
118+
else if (item) {
119+
return item.key;
120+
}
121+
return '';
122+
}
123+
124+
private getParents(item: IFileMapItem): string[] {
125+
if (typeof item === 'object' && item.parents) {
126+
return item.parents;
127+
}
128+
return [];
129+
}
130+
92131
private updateFiles() {
93132
this.files = Object.keys(this.fileMap);
94133
}

test/fixtures/mainModule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
require('./mod1');
2-
require('./mod2');
2+
require('./mod2');

test/fixtures/mod2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require('./mod3');
12
module.exports = {
23
num: 2,
34
};

test/fixtures/mod3.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
num: 3,
3+
};

test/index.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,28 @@
66
import {resolve} from 'path';
77
import Reloader from '../src/index';
88
import {expect} from 'chai';
9+
import batchdelcache from 'batchdelcache';
10+
// import {symlinkSync, existsSync} from 'fs';
911

1012
/**
1113
* Dummy test
1214
*/
1315
describe('Reloader test', () => {
1416

17+
// before(() => {
18+
// const lnPath = resolve(__dirname, './fixtures/lns.js');
19+
// if (!existsSync(lnPath)) {
20+
// symlinkSync(
21+
// resolve(__dirname, './fixtures/mod3.js'),
22+
// lnPath
23+
// );
24+
// }
25+
// });
26+
27+
afterEach(() => {
28+
batchdelcache(['./fixtures/mainModule.js']);
29+
});
30+
1531
it('reload success', () => {
1632
require('./fixtures/mainModule');
1733

@@ -46,5 +62,44 @@ describe('Reloader test', () => {
4662
expect(require('./fixtures/mod2').num).to.be.equal(2);
4763
});
4864

65+
it('reload success', () => {
66+
require('./fixtures/mainModule');
67+
68+
const reloader = new Reloader({
69+
fileMap: {
70+
mod3: {
71+
key: '2',
72+
parents: [
73+
'mod2.js'
74+
]
75+
},
76+
},
77+
context: resolve(__dirname, './fixtures'),
78+
commonRootPath: resolve(__dirname, './fixtures/mainModule.js'),
79+
});
80+
81+
require('./fixtures/mod2').num++;
82+
require('./fixtures/mod3').num++;
83+
84+
expect(require('./fixtures/mod2').num).to.be.equal(3);
85+
expect(require('./fixtures/mod3').num).to.be.equal(4);
86+
87+
let {errors, reloadModules} = reloader.reload({
88+
mod3: {
89+
key: '3',
90+
parents: [
91+
'mod2.js'
92+
]
93+
},
94+
});
95+
96+
expect(errors.length).to.be.equal(0);
97+
expect(reloadModules.length).to.be.equal(1);
98+
expect(reloadModules.includes(resolve(__dirname, './fixtures/mod2.js'))).to.be.equal(true);
99+
expect(require('./fixtures/mod2').num).to.be.equal(2);
100+
expect(require('./fixtures/mod3').num).to.be.equal(3);
101+
102+
});
103+
49104

50105
});

0 commit comments

Comments
 (0)