Skip to content

Commit

Permalink
fix(angular): handle packages with no exported package.json when coll…
Browse files Browse the repository at this point in the history
…ecting secondary entry points for mf builds (#10216)
  • Loading branch information
leosvelperez committed May 9, 2022
1 parent e707461 commit 2503592
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
19 changes: 19 additions & 0 deletions packages/angular/src/utils/mfe/mfe-webpack.spec.ts
Expand Up @@ -231,6 +231,25 @@ describe('MFE Webpack Utils', () => {
},
});
});

it('should not throw when the main entry point package.json cannot be required', () => {
// ARRANGE
(fs.existsSync as jest.Mock).mockImplementation(
(file) => !file.endsWith('non-existent-top-level-package/package.json')
);
jest.spyOn(devkit, 'readJsonFile').mockImplementation((file) => ({
name: file
.replace(/\\/g, '/')
.replace(/^.*node_modules[/]/, '')
.replace('/package.json', ''),
dependencies: { '@angular/core': '~13.2.0' },
}));

// ACT & ASSERT
expect(() =>
sharePackages(['non-existent-top-level-package'])
).not.toThrow();
});
});
});

Expand Down
21 changes: 17 additions & 4 deletions packages/angular/src/utils/mfe/mfe-webpack.ts
Expand Up @@ -137,10 +137,23 @@ function collectPackageSecondaryEntryPoints(
pkgVersion: string,
collectedPackages: { name: string; version: string }[]
): void {
const packageJsonPath = require.resolve(`${pkgName}/package.json`, {
paths: [workspaceRoot],
});
const pathToPackage = dirname(packageJsonPath);
let pathToPackage: string;
try {
const packageJsonPath = require.resolve(`${pkgName}/package.json`, {
paths: [workspaceRoot],
});
pathToPackage = dirname(packageJsonPath);
} catch {
// the package.json might not resolve if the package has the "exports"
// entry and is not exporting the package.json file, fall back to trying
// to find it from the top-level node_modules
pathToPackage = join(workspaceRoot, 'node_modules', pkgName);
if (!existsSync(join(pathToPackage, 'package.json'))) {
// might not exist if it's nested in another package, just return here
return;
}
}

const subDirs = getNonNodeModulesSubDirs(pathToPackage);
recursivelyCollectSecondaryEntryPointsFromDirectory(
pkgName,
Expand Down

0 comments on commit 2503592

Please sign in to comment.