angular-architects / module-federation-plugin

MIT License
730 stars 200 forks source link

share() does not work if used in a yarn workspace #94

Open janhommes opened 3 years ago

janhommes commented 3 years ago

Yarn workspaces use node_modules resolution algorithm to allow a mono-repository structure. Mostly all modules are placed on the root level so that they can be resolved by any other package in the monorepo. The currently used approach to determine the secondary entry points leads to an error when used in such a setup, as the node_modules resolving is not using the same resolving mechanism. Mostly an error is thrown that the file could not be found, as it might only exist on a node_modules folder some level up.

I guess this line needs to be considered https://github.com/angular-architects/module-federation-plugin/blob/790d0e4a20c54b570267c036ddd20154556e959d/libs/mf/src/utils/share-utils.ts#L86

It maybe should use require.resolve to use the same resolving algorithm like node_modules.

Markiewic commented 2 years ago

Same issue!

I use Nx monorepo workspace which doesn't create local node_modules folders, but uses only the project's global node_modules. So when the plugin tryies to resolve secondary entry points, it looks them relatively package.json file, which in my case is not located next to the node_modules folder. The path that the plugin is trying to scan looks like this:

{projectFolder}/apps/{appName}/node_modules/@angular/common

{projectFolder}/node_modules <-- true node_modules folder is right there

So share falls into:

ENOENT: no such file or directory, scandir {projectFolder}/apps/{appName}/node_modules/@angular/common
manfredsteyer commented 2 years ago

The new Angular Package Format now defines an official way of declaring secondary entry points. It is implemented in version 14.3 of this plugin. Can you please try it out?

Markiewic commented 2 years ago

Now it works fine! Thanks.