diff --git a/core/common-util/src/ModuleConfig.ts b/core/common-util/src/ModuleConfig.ts index a1cc7db6..3e7fac73 100644 --- a/core/common-util/src/ModuleConfig.ts +++ b/core/common-util/src/ModuleConfig.ts @@ -145,7 +145,7 @@ export class ModuleConfigUtil { return ref; } - private static readModuleFromNodeModules(baseDir: string) { + public static readModuleFromNodeModules(baseDir: string) { const ref: ModuleReference[] = []; let pkgContent: string; try { @@ -154,11 +154,16 @@ export class ModuleConfigUtil { return []; } const pkg = JSON.parse(pkgContent); - if (!fs.existsSync(path.join(baseDir, '/node_modules'))) { - return ref; - } for (const dependencyKey of Object.keys(pkg.dependencies || {})) { - const absolutePkgPath = path.join(baseDir, '/node_modules', dependencyKey); + let packageJsonPath: string; + try { + // https://nodejs.org/api/packages.html#package-entry-points + // ignore cases where the package entry is exports but package.json is not exported + packageJsonPath = require.resolve(`${dependencyKey}/package.json`, { paths: [ baseDir ] }); + } catch (_) { + continue; + } + const absolutePkgPath = path.dirname(packageJsonPath); const realPkgPath = fs.realpathSync(absolutePkgPath); try { if (this.readModuleNameSync(realPkgPath)) { diff --git a/core/common-util/test/ModuleConfig.test.ts b/core/common-util/test/ModuleConfig.test.ts index 6a338ae3..4d531423 100644 --- a/core/common-util/test/ModuleConfig.test.ts +++ b/core/common-util/test/ModuleConfig.test.ts @@ -62,4 +62,37 @@ describe('test/ModuleConfig.test.ts', () => { }); }); }); + + describe('read package dependencies', () => { + + it('should success if package.json not exist', async () => { + const dir = path.resolve(__dirname, './fixtures/monorepo/foo'); + const ret = ModuleConfigUtil.readModuleFromNodeModules(dir); + assert.deepStrictEqual(ret, []); + }); + + it('should success whether dependencies entry has exported package.json', async () => { + const dir = path.resolve(__dirname, './fixtures/monorepo/packages/d'); + const ret = ModuleConfigUtil.readModuleFromNodeModules(dir); + assert.deepStrictEqual(ret, [{ + path: path.resolve(__dirname, './fixtures/monorepo/packages/d/node_modules/e'), + }]); + }); + + it('should read dependencies from self node_modules', async () => { + const dir = path.resolve(__dirname, './fixtures/monorepo/packages/a'); + const ret = ModuleConfigUtil.readModuleFromNodeModules(dir); + assert.deepStrictEqual(ret, [{ + path: path.resolve(__dirname, './fixtures/monorepo/packages/a/node_modules/c'), + }]); + }); + + it('should read dependencies from parent node_modules', async () => { + const dir = path.resolve(__dirname, './fixtures/monorepo/packages/b'); + const ret = ModuleConfigUtil.readModuleFromNodeModules(dir); + assert.deepStrictEqual(ret, [{ + path: path.resolve(__dirname, './fixtures/monorepo/packages/a'), + }]); + }); + }); }); diff --git a/core/common-util/test/fixtures/monorepo/foo/.gitkeep b/core/common-util/test/fixtures/monorepo/foo/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/core/common-util/test/fixtures/monorepo/node_modules/a b/core/common-util/test/fixtures/monorepo/node_modules/a new file mode 120000 index 00000000..670d7186 --- /dev/null +++ b/core/common-util/test/fixtures/monorepo/node_modules/a @@ -0,0 +1 @@ +../packages/a \ No newline at end of file diff --git a/core/common-util/test/fixtures/monorepo/packages/a/node_modules/c/package.json b/core/common-util/test/fixtures/monorepo/packages/a/node_modules/c/package.json new file mode 100644 index 00000000..889cdf3c --- /dev/null +++ b/core/common-util/test/fixtures/monorepo/packages/a/node_modules/c/package.json @@ -0,0 +1,8 @@ +{ + "eggModule": { + "name": "c" + }, + "dependencies": {}, + "name": "c", + "author": "" +} \ No newline at end of file diff --git a/core/common-util/test/fixtures/monorepo/packages/a/package.json b/core/common-util/test/fixtures/monorepo/packages/a/package.json new file mode 100644 index 00000000..3d93da3f --- /dev/null +++ b/core/common-util/test/fixtures/monorepo/packages/a/package.json @@ -0,0 +1,10 @@ +{ + "eggModule": { + "name": "a" + }, + "dependencies": { + "c": "*" + }, + "name": "b", + "author": "" +} \ No newline at end of file diff --git a/core/common-util/test/fixtures/monorepo/packages/b/package.json b/core/common-util/test/fixtures/monorepo/packages/b/package.json new file mode 100644 index 00000000..840de94d --- /dev/null +++ b/core/common-util/test/fixtures/monorepo/packages/b/package.json @@ -0,0 +1,10 @@ +{ + "eggModule": { + "name": "b" + }, + "dependencies": { + "a": "*" + }, + "name": "b", + "author": "" +} diff --git a/core/common-util/test/fixtures/monorepo/packages/d/node_modules/e/package.json b/core/common-util/test/fixtures/monorepo/packages/d/node_modules/e/package.json new file mode 100644 index 00000000..ccf90a50 --- /dev/null +++ b/core/common-util/test/fixtures/monorepo/packages/d/node_modules/e/package.json @@ -0,0 +1,10 @@ +{ + "eggModule": { + "name": "e" + }, + "name": "e", + "exports": { + "./package.json": "./package.json" + }, + "author": "" +} \ No newline at end of file diff --git a/core/common-util/test/fixtures/monorepo/packages/d/node_modules/f/package.json b/core/common-util/test/fixtures/monorepo/packages/d/node_modules/f/package.json new file mode 100644 index 00000000..51d24c2e --- /dev/null +++ b/core/common-util/test/fixtures/monorepo/packages/d/node_modules/f/package.json @@ -0,0 +1,10 @@ +{ + "eggModule": { + "name": "f" + }, + "name": "f", + "exports": { + "./index.js": "./index.js" + }, + "author": "" +} \ No newline at end of file diff --git a/core/common-util/test/fixtures/monorepo/packages/d/package.json b/core/common-util/test/fixtures/monorepo/packages/d/package.json new file mode 100644 index 00000000..22a2cbf2 --- /dev/null +++ b/core/common-util/test/fixtures/monorepo/packages/d/package.json @@ -0,0 +1,11 @@ +{ + "eggModule": { + "name": "d" + }, + "dependencies": { + "e": "*", + "f": "*" + }, + "name": "d", + "author": "" +} \ No newline at end of file