Skip to content

Commit 7a4dcd2

Browse files
committed
feat: set plugin module optional false if be enabled as a plugin
1 parent 8156bb9 commit 7a4dcd2

File tree

8 files changed

+119
-0
lines changed

8 files changed

+119
-0
lines changed

plugin/tegg/lib/EggModuleLoader.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export class EggModuleLoader {
1818

1919
private buildAppGraph(loaderCache: Map<string, Loader>) {
2020
const appGraph = new AppGraph();
21+
for (const plugin of Object.values(this.app.plugins)) {
22+
const modulePlugin = this.app.moduleReferences.find(t => t.path === plugin.path);
23+
if (modulePlugin) {
24+
modulePlugin.optional = false;
25+
}
26+
}
2127
for (const moduleConfig of this.app.moduleReferences) {
2228
const modulePath = moduleConfig.path;
2329
const moduleNode = new ModuleNode(moduleConfig);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import mm from 'egg-mock';
2+
import assert from 'assert';
3+
import path from 'path';
4+
import { UsedProto } from './fixtures/apps/plugin-module/node_modules/foo-plugin/Used';
5+
6+
describe('test/OptionalPluginModule.test.ts', () => {
7+
let app;
8+
const fixtureDir = path.join(__dirname, 'fixtures/apps/plugin-module');
9+
10+
after(async () => {
11+
await app.close();
12+
});
13+
14+
afterEach(() => {
15+
mm.restore();
16+
});
17+
18+
before(async () => {
19+
mm(process.env, 'EGG_TYPESCRIPT', true);
20+
mm(process, 'cwd', () => {
21+
return path.join(__dirname, '..');
22+
});
23+
app = mm.app({
24+
baseDir: fixtureDir,
25+
framework: require.resolve('egg'),
26+
});
27+
await app.ready();
28+
});
29+
30+
it('should work', async () => {
31+
await app.mockModuleContextScope(async ctx => {
32+
const usedProto = await ctx.getEggObject(UsedProto);
33+
assert(usedProto);
34+
});
35+
});
36+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
module.exports = function(appInfo) {
6+
const config = {
7+
keys: 'test key',
8+
customLogger: {
9+
xxLogger: {
10+
file: path.join(appInfo.root, 'logs/xx.log'),
11+
},
12+
},
13+
security: {
14+
csrf: {
15+
ignoreJSON: false,
16+
}
17+
},
18+
};
19+
return config;
20+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
exports.tracer = {
4+
package: 'egg-tracer',
5+
enable: true,
6+
};
7+
8+
exports.teggConfig = {
9+
package: '@eggjs/tegg-config',
10+
enable: true,
11+
};
12+
13+
exports.eggFooPlugin = {
14+
package: 'foo-plugin',
15+
enable: true,
16+
};
17+
18+
exports.watcher = false;

plugin/tegg/test/fixtures/apps/plugin-module/node_modules/foo-plugin/Used.js

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/tegg/test/fixtures/apps/plugin-module/node_modules/foo-plugin/package.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/tegg/test/fixtures/apps/plugin-module/node_modules/foo/package.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "egg-app",
3+
"egg": {
4+
"framework": "foo"
5+
}
6+
}

0 commit comments

Comments
 (0)