Skip to content

Commit

Permalink
fix: support parent mod
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Jul 28, 2020
1 parent 9c95653 commit 438b638
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"prebuild": "rimraf dist",
"build": "tsc --module commonjs && rollup -c rollup.config.ts && typedoc --out docs --target es6 --theme minimal --mode file src",
"start": "rollup -c rollup.config.ts -w",
"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",
"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",
"deploy-docs": "ts-node tools/gh-pages-publish",
"report-coverage": "cat ./coverage/lcov.info | coveralls",
"commit": "git-cz",
Expand Down
47 changes: 43 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import {join} from 'path';
import batchdelcache from 'batchdelcache';

interface IFileMap {
[fileName: string]: string;
[fileName: string]: IFileMapItem;
}

type IFileMapItem = string | {
key: string;
parents?: string[];
};

export interface IOptions {

/* 项目所在根目录 */
Expand Down Expand Up @@ -52,18 +57,33 @@ export default class Reloader {
}

reload(newFileMap: IFileMap) {

const reloadModules = new Set<string>();
for (const [name, md5] of Object.entries(newFileMap)) {
if (this.filter(name) && (name in this.fileMap) && this.fileMap[name] !== md5) {
reloadModules.add(require.resolve(join(this.context, name)));

for (const [name, item] of Object.entries(newFileMap)) {
const hasKey = name in this.fileMap;
const md5 = this.getKey(item);
if (hasKey && this.getKey(this.fileMap[name]) !== md5 && this.filter(name)) {
const parents = this.getParents(item);
if (parents.length > 0) {
parents.forEach(filename => reloadModules.add(join(this.context, filename)));
}
else {
reloadModules.add(join(this.context, name));
}
}
}

// 删除缓存
batchdelcache(
Array.from(reloadModules)
);

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

const errors: IError[] = [];
for (const mod of reloadModules) {
try {
Expand All @@ -77,7 +97,9 @@ export default class Reloader {
});
}
}

this.updateFileMap(Object.assign(this.fileMap, newFileMap));

return {
reloadModules: Array.from(reloadModules),
errors,
Expand All @@ -89,6 +111,23 @@ export default class Reloader {
this.updateFiles();
}

private getKey(item: IFileMapItem): string {
if (typeof item === 'string') {
return item;
}
else if (item) {
return item.key;
}
return '';
}

private getParents(item: IFileMapItem): string[] {
if (typeof item === 'object' && item.parents) {
return item.parents;
}
return [];
}

private updateFiles() {
this.files = Object.keys(this.fileMap);
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/mainModule.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require('./mod1');
require('./mod2');
require('./mod2');
1 change: 1 addition & 0 deletions test/fixtures/mod2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('./mod3');
module.exports = {
num: 2,
};
3 changes: 3 additions & 0 deletions test/fixtures/mod3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
num: 3,
};
55 changes: 55 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@
import {resolve} from 'path';
import Reloader from '../src/index';
import {expect} from 'chai';
import batchdelcache from 'batchdelcache';
// import {symlinkSync, existsSync} from 'fs';

/**
* Dummy test
*/
describe('Reloader test', () => {

// before(() => {
// const lnPath = resolve(__dirname, './fixtures/lns.js');
// if (!existsSync(lnPath)) {
// symlinkSync(
// resolve(__dirname, './fixtures/mod3.js'),
// lnPath
// );
// }
// });

afterEach(() => {
batchdelcache(['./fixtures/mainModule.js']);
});

it('reload success', () => {
require('./fixtures/mainModule');

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

it('reload success', () => {
require('./fixtures/mainModule');

const reloader = new Reloader({
fileMap: {
mod3: {
key: '2',
parents: [
'mod2.js'
]
},
},
context: resolve(__dirname, './fixtures'),
commonRootPath: resolve(__dirname, './fixtures/mainModule.js'),
});

require('./fixtures/mod2').num++;
require('./fixtures/mod3').num++;

expect(require('./fixtures/mod2').num).to.be.equal(3);
expect(require('./fixtures/mod3').num).to.be.equal(4);

let {errors, reloadModules} = reloader.reload({
mod3: {
key: '3',
parents: [
'mod2.js'
]
},
});

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

});


});

0 comments on commit 438b638

Please sign in to comment.