Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions core/common-util/src/ModuleConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,38 @@ export class ModuleConfigUtil {
path: moduleDir,
});
}
const moduleReferences = this.readModuleFromNodeModules(baseDir);
for (const moduleReference of moduleReferences) {
const moduleBasePath = path.basename(moduleReference.path);
moduleDirSet.forEach(modulePath => {
if (path.basename(modulePath) === moduleBasePath) {
throw new Error('duplicate import of module reference: ' + moduleBasePath);
}
});
ref.push({
path: moduleReference.path,
});
}
return ref;
}

private static readModuleFromNodeModules(baseDir: string) {
const ref: ModuleReference[] = [];
const pkgContent = fs.readFileSync(path.join(baseDir, 'package.json'), 'utf8');
const pkg = JSON.parse(pkgContent);
for (const dependencyKey of Object.keys(pkg.dependencies || {})) {
const absolutePkgPath = path.join(baseDir, '/node_modules', dependencyKey);
const realPkgPath = fs.realpathSync(absolutePkgPath);
try {
if (this.readModuleNameSync(realPkgPath)) {
ref.push({
path: realPkgPath,
});
}
} catch (_) {
continue;
}
}
return ref;
}

Expand Down
6 changes: 6 additions & 0 deletions core/common-util/test/ModuleConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ describe('test/ModuleConfig.test.ts', () => {
{ path: path.join(fixturesPath, 'app/module-a') },
{ path: path.join(fixturesPath, 'app/module-b') },
{ path: path.join(fixturesPath, 'app/module-b/test/fixtures/module-e') },
{ path: path.join(fixturesPath, 'node_modules/module-c') },
]);
});

it('duplicated module', () => {
const fixturesPath = path.join(__dirname, './fixtures/apps/app-with-no-module-json-duplicated');
assert.throws(() => { ModuleConfigUtil.readModuleReference(fixturesPath); }, /duplicate import of module reference/, 'did not throw with expected message');
});

describe('has symlink', () => {
it('should work', () => {
const fixturesPath = path.join(__dirname, './fixtures/apps/app-with-symlink');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "module-d",
"eggModule": {
"name": "moduleD"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "module-c",
"eggModule": {
"name": "moduleC"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "module-a",
"eggModule": {
"name": "moduleA"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "module-b",
"eggModule": {
"name": "moduleB"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "module-e",
"eggModule": {
"name": "moduleE"
}
}

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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "foo",
"dependencies": {
"module-b": "^1.0.0"
}
}

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.

Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"name": "foo"
"name": "foo",
"dependencies": {
"module-c": "^1.0.0",
"dep": "^1.0.0"
}
}