-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: scan framework dependencies as optional module (#184)
<!-- 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 - [x] 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. --> <!-- - any feature? - close https://github.com/eggjs/egg/ISSUE_URL -->
- Loading branch information
Showing
27 changed files
with
289 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import assert from 'assert'; | ||
import path from 'path'; | ||
import { AppGraph, ModuleNode } from '../src/model/AppGraph'; | ||
import { RootProto } from './fixtures/modules/app-graph-modules/root/Root'; | ||
import { UsedProto } from './fixtures/modules/app-graph-modules/used/Used'; | ||
import { UnusedProto } from './fixtures/modules/app-graph-modules/unused/Unused'; | ||
|
||
describe('test/LoadUnit/AppGraph.test.ts', () => { | ||
it('optional module dep should work', () => { | ||
const graph = new AppGraph(); | ||
const rootModuleNode = new ModuleNode({ | ||
name: 'foo', | ||
path: path.join(__dirname, './fixtures/modules/app-graph-modules/root'), | ||
}); | ||
rootModuleNode.addClazz(RootProto); | ||
graph.addNode(rootModuleNode); | ||
const usedOptionalModuleNode = new ModuleNode({ | ||
name: 'usedOptionalModuleNode', | ||
path: path.join(__dirname, './fixtures/modules/app-graph-modules/used'), | ||
optional: true, | ||
}); | ||
usedOptionalModuleNode.addClazz(UsedProto); | ||
graph.addNode(usedOptionalModuleNode); | ||
const unusedOptionalModuleNode = new ModuleNode({ | ||
name: 'unusedOptionalModuleNode', | ||
path: path.join(__dirname, './fixtures/modules/app-graph-modules/unused'), | ||
optional: true, | ||
}); | ||
unusedOptionalModuleNode.addClazz(UnusedProto); | ||
graph.addNode(unusedOptionalModuleNode); | ||
graph.build(); | ||
graph.sort(); | ||
assert(graph.moduleConfigList.length === 2); | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
core/metadata/test/fixtures/modules/app-graph-modules/root/Root.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { SingletonProto, Inject } from '@eggjs/core-decorator'; | ||
import { UsedProto } from '../used/Used'; | ||
|
||
@SingletonProto() | ||
export class RootProto { | ||
@Inject() usedProto: UsedProto; | ||
} |
6 changes: 6 additions & 0 deletions
6
core/metadata/test/fixtures/modules/app-graph-modules/root/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "root", | ||
"eggModule": { | ||
"name": "root" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
core/metadata/test/fixtures/modules/app-graph-modules/unused/Unused.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { SingletonProto } from '@eggjs/core-decorator'; | ||
|
||
@SingletonProto() | ||
export class UnusedProto { | ||
} |
6 changes: 6 additions & 0 deletions
6
core/metadata/test/fixtures/modules/app-graph-modules/unused/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "unused", | ||
"eggModule": { | ||
"name": "unused" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
core/metadata/test/fixtures/modules/app-graph-modules/used/Used.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { AccessLevel, SingletonProto } from '@eggjs/core-decorator'; | ||
|
||
@SingletonProto({ | ||
accessLevel: AccessLevel.PUBLIC, | ||
}) | ||
export class UsedProto { | ||
} |
6 changes: 6 additions & 0 deletions
6
core/metadata/test/fixtures/modules/app-graph-modules/used/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "used", | ||
"eggModule": { | ||
"name": "used" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { ModuleConfigUtil, ModuleReference, ReadModuleReferenceOptions } from '@eggjs/tegg-common-util'; | ||
import path from 'path'; | ||
|
||
export class ModuleScanner { | ||
private readonly baseDir: string; | ||
private readonly readModuleOptions: ReadModuleReferenceOptions; | ||
|
||
constructor(baseDir: string, readModuleOptions: ReadModuleReferenceOptions) { | ||
this.baseDir = baseDir; | ||
this.readModuleOptions = readModuleOptions; | ||
} | ||
|
||
/** | ||
* - load module references from config or scan from baseDir | ||
* - load framework module as optional module reference | ||
*/ | ||
loadModuleReferences(): readonly ModuleReference[] { | ||
const moduleReferences = ModuleConfigUtil.readModuleReference(this.baseDir, this.readModuleOptions || {}); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const appPkg = require(path.join(this.baseDir, 'package.json')); | ||
const framework = appPkg.egg?.framework; | ||
if (!framework) { | ||
return moduleReferences; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const frameworkPkg = require.resolve(`${framework}/package.json`, { | ||
paths: [ this.baseDir ], | ||
}); | ||
const frameworkDir = path.dirname(frameworkPkg); | ||
const optionalModuleReferences = ModuleConfigUtil.readModuleReference(frameworkDir, this.readModuleOptions || {}); | ||
return [ | ||
...moduleReferences, | ||
...optionalModuleReferences.map(t => { | ||
return { | ||
...t, | ||
optional: true, | ||
}; | ||
}), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.