Skip to content

Commit

Permalink
feat: set plugin module optional false if be enabled as a plugin (#190)
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]. -->

- [ ] `npm test` passes
- [ ] 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. -->

<!--
- any feature?
- close https://github.com/eggjs/egg/ISSUE_URL
-->
  • Loading branch information
killagu committed Feb 19, 2024
1 parent 8156bb9 commit 57a1adc
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 0 deletions.
7 changes: 7 additions & 0 deletions plugin/tegg/lib/EggModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export class EggModuleLoader {

private buildAppGraph(loaderCache: Map<string, Loader>) {
const appGraph = new AppGraph();
for (const plugin of Object.values(this.app.plugins)) {
if (!plugin.enable) continue;
const modulePlugin = this.app.moduleReferences.find(t => t.path === plugin.path);
if (modulePlugin) {
modulePlugin.optional = false;
}
}
for (const moduleConfig of this.app.moduleReferences) {
const modulePath = moduleConfig.path;
const moduleNode = new ModuleNode(moduleConfig);
Expand Down
36 changes: 36 additions & 0 deletions plugin/tegg/test/OptionalPluginModule.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import mm from 'egg-mock';
import assert from 'assert';
import path from 'path';
import { UsedProto } from './fixtures/apps/plugin-module/node_modules/foo-plugin/Used';

describe('test/OptionalPluginModule.test.ts', () => {
let app;
const fixtureDir = path.join(__dirname, 'fixtures/apps/plugin-module');

after(async () => {
await app.close();
});

afterEach(() => {
mm.restore();
});

before(async () => {
mm(process.env, 'EGG_TYPESCRIPT', true);
mm(process, 'cwd', () => {
return path.join(__dirname, '..');
});
app = mm.app({
baseDir: fixtureDir,
framework: require.resolve('egg'),
});
await app.ready();
});

it('should work', async () => {
await app.mockModuleContextScope(async ctx => {
const usedProto = await ctx.getEggObject(UsedProto);
assert(usedProto);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const path = require('path');

module.exports = function(appInfo) {
const config = {
keys: 'test key',
customLogger: {
xxLogger: {
file: path.join(appInfo.root, 'logs/xx.log'),
},
},
security: {
csrf: {
ignoreJSON: false,
}
},
};
return config;
};
18 changes: 18 additions & 0 deletions plugin/tegg/test/fixtures/apps/plugin-module/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

exports.tracer = {
package: 'egg-tracer',
enable: true,
};

exports.teggConfig = {
package: '@eggjs/tegg-config',
enable: true,
};

exports.eggFooPlugin = {
package: 'foo-plugin',
enable: true,
};

exports.watcher = false;

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 plugin/tegg/test/fixtures/apps/plugin-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "egg-app",
"egg": {
"framework": "foo"
}
}

0 comments on commit 57a1adc

Please sign in to comment.