Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(angular): handle packages with no exported package.json when collecting secondary entry points for mf builds #10216

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/angular/src/utils/mfe/mfe-webpack.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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