From 6e2e63731fa431620834f68804039fe1ed20a2f4 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Tue, 1 Oct 2024 13:55:42 +0100 Subject: [PATCH] fix(module-federation): additionalShared should check node_modules when applying to support transitive deps #28137 (#28216) ## Current Behavior Transitive deps may not exist on the project graph or be installed in the root package.json file. They could require the singleton pattern and should use the `additionalShared` functionality. However, the additionalShared will check project graph and root package.json, erroring if the package cannot be found. The last resort should be to use `require.resolve` to check the node_modules folder to find the package.json of the package and select a version from it. ## Expected Behavior ## Related Issue(s) Fixes #28137 --- .../webpack/src/utils/module-federation/share.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/webpack/src/utils/module-federation/share.ts b/packages/webpack/src/utils/module-federation/share.ts index a8c92dd40a4bf..50379b707186d 100644 --- a/packages/webpack/src/utils/module-federation/share.ts +++ b/packages/webpack/src/utils/module-federation/share.ts @@ -306,10 +306,14 @@ function addStringDependencyToSharedConfig( sharedConfig[dependency] = config; } else { - throw new Error( - `The specified dependency "${dependency}" in the additionalShared configuration does not exist in the project graph. ` + - `Please check your additionalShared configuration and make sure you are including valid workspace projects or npm packages.` - ); + const pkgJsonPath = require.resolve(`${dependency}/package.json`); + if (!pkgJsonPath) { + throw new Error( + `Could not find package ${dependency} when applying it as a shared package. Are you sure it has been installed?` + ); + } + const pkgJson = readJsonFile(pkgJsonPath); + const config = getNpmPackageSharedConfig(dependency, pkgJson.version); } }