Skip to content

Commit 90cafae

Browse files
whxaxespopomore
authored andcommitted
feat: loader support load file without extname (#194)
<!-- Thank you for your pull request. Please review below requirements. Bug fixes and new features should include tests and possibly benchmarks. Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md 感谢您贡献代码。请确认下列 checklist 的完成情况。 Bug 修复和新功能必须包含测试,必要时请附上性能测试。 Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md --> ##### Checklist <!-- Remove items that do not apply. For completed items, change [ ] to [x]. --> - [x] `npm test` passes - [x] tests and/or benchmarks are included - [ ] documentation is changed or added - [ ] commit message follows commit guidelines ##### Affected core subsystem(s) <!-- Provide affected core subsystem(s). --> ##### Description of change <!-- Provide a description of the change below this comment. -->
1 parent f5b6ad6 commit 90cafae

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

lib/loader/egg_loader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ class EggLoader {
291291
* @since 1.0.0
292292
*/
293293
loadFile(filepath, ...inject) {
294-
if (!filepath || !fs.existsSync(filepath)) {
294+
filepath = filepath && this.resolveModule(filepath);
295+
if (!filepath) {
295296
return null;
296297
}
297298

lib/loader/mixin/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
loadRouter() {
1515
this.timing.start('Load Router');
1616
// 加载 router.js
17-
this.loadFile(this.resolveModule(path.join(this.options.baseDir, 'app/router')));
17+
this.loadFile(path.join(this.options.baseDir, 'app/router'));
1818
this.timing.end('Load Router');
1919
},
2020
};

test/loader/egg_loader.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ describe('test/loader/egg_loader.test.js', () => {
6161
framework: path.join(__dirname, '../fixtures/egg'),
6262
});
6363
});
64+
65+
it('should loadFile auto resolve file', () => {
66+
const loader = new EggLoader({
67+
baseDir: path.join(__dirname, '../fixtures/nothing'),
68+
app: {},
69+
logger: console,
70+
});
71+
72+
let ret = loader.loadFile(path.join(__dirname, '../fixtures/load_file/function.js'), 1, 2);
73+
assert(ret[0] === 1);
74+
assert(ret[1] === 2);
75+
76+
ret = loader.loadFile(path.join(__dirname, '../fixtures/load_file/function'), 1, 2);
77+
assert(ret[0] === 1);
78+
assert(ret[1] === 2);
79+
});
6480
});
6581

6682
it('should be loaded by loadToApp', () => {

0 commit comments

Comments
 (0)