From 263467fc43a25eb5a1670de4778de127662a201b Mon Sep 17 00:00:00 2001 From: killa Date: Mon, 19 Feb 2024 21:09:02 +0800 Subject: [PATCH] fix: ignore duplicated module (#191) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ##### Checklist - [ ] `npm test` passes - [ ] tests and/or benchmarks are included - [ ] documentation is changed or added - [ ] commit message follows commit guidelines ##### Affected core subsystem(s) ##### Description of change --- .gitignore | 1 + plugin/config/lib/ModuleScanner.ts | 17 ++++++---- .../test/DuplicateOptionalModule.test.ts | 32 +++++++++++++++++++ .../config/config.default.js | 20 ++++++++++++ .../config/plugin.js | 8 +++++ .../node_modules/foo/package.json | 7 ++++ .../node_modules/unused/Unused.js | 16 ++++++++++ .../node_modules/unused/package.json | 6 ++++ .../node_modules/used/Used.js | 18 +++++++++++ .../node_modules/used/package.json | 6 ++++ .../duplicate-optional-module/package.json | 9 ++++++ 11 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 plugin/config/test/DuplicateOptionalModule.test.ts create mode 100644 plugin/config/test/fixtures/apps/duplicate-optional-module/config/config.default.js create mode 100644 plugin/config/test/fixtures/apps/duplicate-optional-module/config/plugin.js create mode 100644 plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/foo/package.json create mode 100644 plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/unused/Unused.js create mode 100644 plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/unused/package.json create mode 100644 plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/used/Used.js create mode 100644 plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/used/package.json create mode 100644 plugin/config/test/fixtures/apps/duplicate-optional-module/package.json diff --git a/.gitignore b/.gitignore index 3ac962d3..400766c0 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,4 @@ plugin/tegg/test/fixtures/apps/**/*.js !standalone/standalone/test/fixtures/**/node_modules !standalone/standalone/test/fixtures/**/node_modules/**/*.js !plugin/tegg/test/fixtures/**/node_modules +!plugin/config/test/fixtures/**/node_modules diff --git a/plugin/config/lib/ModuleScanner.ts b/plugin/config/lib/ModuleScanner.ts index 459ae5ef..14fd13bf 100644 --- a/plugin/config/lib/ModuleScanner.ts +++ b/plugin/config/lib/ModuleScanner.ts @@ -28,14 +28,17 @@ export class ModuleScanner { }); const frameworkDir = path.dirname(frameworkPkg); const optionalModuleReferences = ModuleConfigUtil.readModuleReference(frameworkDir, this.readModuleOptions || {}); - return [ + const result = [ ...moduleReferences, - ...optionalModuleReferences.map(t => { - return { - ...t, - optional: true, - }; - }), ]; + for (const optionalModuleReference of optionalModuleReferences) { + if (!result.some(t => t.path === optionalModuleReference.path)) { + result.push({ + ...optionalModuleReference, + optional: true, + }); + } + } + return result; } } diff --git a/plugin/config/test/DuplicateOptionalModule.test.ts b/plugin/config/test/DuplicateOptionalModule.test.ts new file mode 100644 index 00000000..34a19394 --- /dev/null +++ b/plugin/config/test/DuplicateOptionalModule.test.ts @@ -0,0 +1,32 @@ +import mm from 'egg-mock'; +import assert from 'assert'; +import path from 'path'; + +describe('test/DuplicateOptionalModule.test.ts', () => { + let app; + const fixturesPath = path.join(__dirname, './fixtures/apps/duplicate-optional-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: fixturesPath, + framework: require.resolve('egg'), + }); + await app.ready(); + }); + + it('should work', async () => { + assert.equal(app.moduleReferences.length, 2); + }); +}); diff --git a/plugin/config/test/fixtures/apps/duplicate-optional-module/config/config.default.js b/plugin/config/test/fixtures/apps/duplicate-optional-module/config/config.default.js new file mode 100644 index 00000000..6d1b8de5 --- /dev/null +++ b/plugin/config/test/fixtures/apps/duplicate-optional-module/config/config.default.js @@ -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; +}; diff --git a/plugin/config/test/fixtures/apps/duplicate-optional-module/config/plugin.js b/plugin/config/test/fixtures/apps/duplicate-optional-module/config/plugin.js new file mode 100644 index 00000000..e66ec9e2 --- /dev/null +++ b/plugin/config/test/fixtures/apps/duplicate-optional-module/config/plugin.js @@ -0,0 +1,8 @@ +'use strict'; + +exports.tracer = { + package: 'egg-tracer', + enable: true, +}; + +exports.watcher = false; diff --git a/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/foo/package.json b/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/foo/package.json new file mode 100644 index 00000000..06d0ea69 --- /dev/null +++ b/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/foo/package.json @@ -0,0 +1,7 @@ +{ + "name": "foo", + "dependencies": { + "used": "*", + "unused": "*" + } +} diff --git a/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/unused/Unused.js b/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/unused/Unused.js new file mode 100644 index 00000000..0f47ad6a --- /dev/null +++ b/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/unused/Unused.js @@ -0,0 +1,16 @@ +"use strict"; +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.UnusedProto = void 0; +const tegg_core_decorator_1 = require('../../../../../../../../core/core-decorator'); +let UnusedProto = class UnusedProto { +}; +exports.UnusedProto = UnusedProto; +exports.UnusedProto = UnusedProto = __decorate([ + (0, tegg_core_decorator_1.SingletonProto)() +], UnusedProto); diff --git a/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/unused/package.json b/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/unused/package.json new file mode 100644 index 00000000..4473072a --- /dev/null +++ b/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/unused/package.json @@ -0,0 +1,6 @@ +{ + "name": "unused", + "eggModule": { + "name": "unused" + } +} diff --git a/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/used/Used.js b/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/used/Used.js new file mode 100644 index 00000000..2bb8c288 --- /dev/null +++ b/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/used/Used.js @@ -0,0 +1,18 @@ +"use strict"; +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.UsedProto = void 0; +const tegg_core_decorator_1 = require('../../../../../../../../core/core-decorator'); +let UsedProto = class UsedProto { +}; +exports.UsedProto = UsedProto; +exports.UsedProto = UsedProto = __decorate([ + (0, tegg_core_decorator_1.SingletonProto)({ + accessLevel: tegg_core_decorator_1.AccessLevel.PUBLIC, + }) +], UsedProto); diff --git a/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/used/package.json b/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/used/package.json new file mode 100644 index 00000000..efa9b239 --- /dev/null +++ b/plugin/config/test/fixtures/apps/duplicate-optional-module/node_modules/used/package.json @@ -0,0 +1,6 @@ +{ + "name": "used", + "eggModule": { + "name": "used" + } +} diff --git a/plugin/config/test/fixtures/apps/duplicate-optional-module/package.json b/plugin/config/test/fixtures/apps/duplicate-optional-module/package.json new file mode 100644 index 00000000..ac32390c --- /dev/null +++ b/plugin/config/test/fixtures/apps/duplicate-optional-module/package.json @@ -0,0 +1,9 @@ +{ + "name": "egg-app", + "egg": { + "framework": "foo" + }, + "dependencies": { + "used": "*" + } +}