pixability / federated-types

A node-based CLI module that facilitates the sharing of Typescript definition files across the packages in a typescript mono-repo.
MIT License
58 stars 17 forks source link

ERROR: Found x federation configs. #6

Closed derekdon closed 3 years ago

derekdon commented 3 years ago

Hey man,

I've a lerna based mono repo with packages at different levels.

packages/a/b/c/federation.config.json packages/e/f/federation.config.json

Getting the ERROR: Found x federation configs.

~Do we need to update some of the path resolution to use process.cwd()?~

In fact I can see by debugging it that findFederationConfigs is traversing the node_modules of the package, and lerna has npm linked to the other package, so that script is finding the federation.config.json of that project too. If we really need to do this traversing, filtering out node_modules might be worth doing:

const findFederationConfigs = (base, files, result) => { files = files || fs.readdirSync(base); files = files.filter(f => f !== 'node_modules'); // something like this. result = result || [];

gthmb commented 3 years ago

Hey Derek, thanks for submitting this issue. I am not familiar with how Lerna links packages, but it sounds like it's doing some sort of npm link under the hood (creating a symlink from the node_modules directory of one package to the linked package). Let me take a closer look here, but it sure sounds like you are correct that the traversing needs to be more defensive.

gthmb commented 3 years ago

I'll have some time to open up a PR for this today. In addition to ignoring the node_modules when trying to find the federation.config.json file, I'll stop the traversal when it finds the closest federation.config.json.

derekdon commented 3 years ago

Thanks for looking at this @gthmb.

Perhaps I'm using it wrong but I've also noticed the generated types code doesn't match up what I expect. For example if you have this:

{ "name": "myRemote", "exposes": { "./MyRemoteButton": "./src/button/index.ts", } }

Working in the context of the latest WP5 module federation setup (at the time of this comment), I'd reference it like 'myRemote/MyRemoteButton' in the code, ie:

const { MyButton } = await import('myRemote/MyRemoteButton');

However it seems the top level node_modules/@types/__federated_types/* declare the modules with the exposes path value, not the exposes key value:

declare module "myRemote/button/index"

... so typescript doesn't actually have the typings and I still have to drop a // @ts-ignore above my dynamic remote import line.

Any ideas?