Skip to content

Commit

Permalink
feat: support dependency module for standalone (#155)
Browse files Browse the repository at this point in the history
<!--
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
gxkl authored Sep 18, 2023
1 parent ea75fdc commit df2e2a6
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ plugin/dal/test/fixtures/modules/*/dal/
!plugin/orm/test/fixtures/prepare.js
!benchmark/**/*.js
plugin/tegg/test/fixtures/apps/**/*.js
!standalone/standalone/test/fixtures/**/node_modules
!standalone/standalone/test/fixtures/**/node_modules/**/*.js
13 changes: 11 additions & 2 deletions standalone/standalone/src/Runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ModuleConfigUtil, ModuleReference, RuntimeConfig } from '@eggjs/tegg-common-util';
import { ModuleConfigUtil, ModuleReference, ReadModuleReferenceOptions, RuntimeConfig } from '@eggjs/tegg-common-util';
import {
EggPrototype, EggPrototypeLifecycleUtil,
LoadUnit,
Expand Down Expand Up @@ -26,6 +26,10 @@ import { ConfigSourceQualifierAttribute } from './ConfigSource';
import { ConfigSourceLoadUnitHook } from './ConfigSourceLoadUnitHook';
import { LoadUnitInnerClassHook } from './LoadUnitInnerClassHook';

export interface ModuleDependency extends ReadModuleReferenceOptions {
baseDir: string;
}

export interface RunnerOptions {
/**
* @deprecated
Expand All @@ -35,6 +39,7 @@ export interface RunnerOptions {
env?: string;
name?: string;
innerObjectHandlers?: Record<string, InnerObject[]>;
dependencies?: (string | ModuleDependency)[];
}

export class Runner {
Expand Down Expand Up @@ -62,7 +67,11 @@ export class Runner {
this.cwd = cwd;
this.env = options?.env;
this.name = options?.name;
this.moduleReferences = ModuleConfigUtil.readModuleReference(this.cwd);
const moduleDirs = (options?.dependencies || []).concat(this.cwd);
this.moduleReferences = moduleDirs.reduce((list, baseDir) => {
const module = typeof baseDir === 'string' ? { baseDir } : baseDir;
return list.concat(...ModuleConfigUtil.readModuleReference(module.baseDir, module));
}, [] as readonly ModuleReference[]);
this.moduleConfigs = {};
this.innerObjects = {
moduleConfigs: [{
Expand Down
19 changes: 19 additions & 0 deletions standalone/standalone/test/fixtures/dependency/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ContextProto, Inject } from '@eggjs/tegg';
import { Runner, MainRunner } from '@eggjs/tegg/standalone';
import { Hello } from 'dependency-2/foo';
import { ConfigSourceQualifier } from '../../../src/ConfigSource';

@ContextProto()
@Runner()
export class Foo implements MainRunner<string> {
@Inject()
hello: Hello;

@Inject()
@ConfigSourceQualifier('dependency2')
moduleConfig: any;

async main(): Promise<string> {
return this.hello.hello() + JSON.stringify(this.moduleConfig);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions standalone/standalone/test/fixtures/dependency/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "entry",
"eggModule": {
"name": "entry"
}
}
11 changes: 11 additions & 0 deletions standalone/standalone/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ describe('test/index.test.ts', () => {
});
});

describe('runner with dependency', () => {
it('should work', async () => {
const msg: string = await main(path.join(__dirname, './fixtures/dependency'), {
dependencies: [
path.join(__dirname, './fixtures/dependency/node_modules/dependency-1'),
],
});
assert(msg === 'hello!{"features":{"dynamic":{"foo":"bar"}}}');
});
});

describe('runner with inner object', () => {
it('should work', async () => {
const msg: string = await main(path.join(__dirname, './fixtures/inner-object'), {
Expand Down

0 comments on commit df2e2a6

Please sign in to comment.