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: relative name of exposed component #11

Merged
merged 3 commits into from
Oct 8, 2021

Conversation

serrg
Copy link
Contributor

@serrg serrg commented Jun 4, 2021

I would like to propose new optional --useFederatedAlias argument which takes precedence over automatically generated module name in declaration file (*.d.ts):

AS IS:
a) With federation.config.json like this:

{
  "name": "app",
  "exposes": {
    "./ComponentA": "./src/shared/ui/ComponentA",
    "./ComponentB": "./src/shared/ui/ComponentB",
  }
}

make-federated-types produces the following declarations (shared path is omitted):

declare module "app/ui/ComponentA" 
declare module "app/ui/ComponentB" 

b) With federation.config.json like this:

{
  "name": "app",
  "exposes": {
    "./ComponentA": "./src/shared/ui/ComponentA",
    "./ComponentB": "./src/test/ui/ComponentB",
  }
}

make-federated-types produces the following declarations (shared and test path are taken into module names):

declare module "app/shared/ui/ComponentA" 
declare module "app/test/ui/ComponentB" 

As you can see generated module name depends on source path (imho ts.createProgram somehow combines path) and you can not rely on this name during the imports on remote applications because you can have two possible import paths (and you don't know which one will be generated):

import ComponentA from "app/ui/ComponentA"; // with a) 
// or
import ComponentA from "app/shared/ui/ComponentA"; // with b)

TO BE:

make-federated-types --config ./path/to/my/config.json --useFederatedAlias

When you have federation.config.json like this:

{
  "name": "app",
  "exposes": {
    "./ComponentA": "./src/shared/ui/ComponentA",
    "./ComponentB": "./src/shared/ui/ComponentB",
  }
}

make-federated-types produces the following declarations:

declare module "app/ComponentA" 
declare module "app/ComponentB" 

When you have federation.config.json like this:

{
  "name": "app",
  "exposes": {
    "./myScope/ComponentA": "./src/shared/ui/ComponentA",
    "./myScope/ComponentB": "./src/test/ui/ComponentB",
  }
}

make-federated-types produces the following declarations:

declare module "app/myScope/ComponentA" 
declare module "app/myScope/ComponentB" 

@serrg serrg changed the title feat: declare module name based on federation alias feat: declare module name based on federation alias with --useFederatedAlias Jun 4, 2021
@crutch12
Copy link

crutch12 commented Jun 10, 2021

Nice PR!

But

You should use path.join instead of replace('./', '')

Just like in my closed PR:
https://github.com/pixability/federated-types/pull/12/files#diff-180b3d2af89416b99563fff8c37eb8ca1ef7591fc1ddd736b5dacf740ccd3760R80

const moduleDeclareName = path.join(federationConfig.name, exposeName).replace(/[\\/]/g, '/');

Also:
ts.createProgram + emit() may generate additional declarations

Example:

"exposes": {
  "./BellIcon": "./src/components/icons/BellIcon",
  "./MainAppContextProvider": "./src/components/main-app-context/MainAppContextProvider"
}

Generates 3 declarations, not 2:

declare module "icons/BellIcon" {
  ...
}
declare module "main-app-context/MainAppContext" {
  ...
}
declare module "main-app-context/MainAppContextProvider" {
  ...
}

So your compileKeys[index] won't match found moduleName

@serrg
Copy link
Contributor Author

serrg commented Jun 17, 2021

@crutch12 Nice catch! Thanks!
I have fixed PR with your solution and removed redundant --useFederatedAlias

@serrg serrg changed the title feat: declare module name based on federation alias with --useFederatedAlias fix: relative name of exposed component Jun 17, 2021
@serrg
Copy link
Contributor Author

serrg commented Jun 17, 2021

@gthmb WDYT about this? :)

@crutch12
Copy link

@crutch12 Nice catch! Thanks!
I have fixed PR with your solution and removed redundant --useFederatedAlias

Nice!

I think it's right decision: useFederatedAlias should be default behavior.
But it's breaking change, so we have to release a new minor version :) (v0.2.0)

@serrg
Copy link
Contributor Author

serrg commented Jun 17, 2021

I think it's right decision: useFederatedAlias should be default behavior.

Exactly, I think the same that exposed name should take precedence over module name 👍

But it's breaking change

We can keep compatibility but after this we should introduce new argument for using exposed name as module name

@serrg
Copy link
Contributor Author

serrg commented Sep 14, 2021

@gthmb Do you still support this package?

@gthmb
Copy link
Contributor

gthmb commented Oct 7, 2021

Hi! I lost access to this package when I changed jobs -_-. I have requested write access again to approve/merge PRs. Worst case , I can fork it and maintain a new fork, but I'd like to maintain this repo if possible.

@pix-pete
Copy link

pix-pete commented Oct 7, 2021

Hi! I lost access to this package when I changed jobs -_-. I have requested write access again to approve/merge PRs. Worst case , I can fork it and maintain a new fork, but I'd like to maintain this repo if possible.

@gthmb Hey buddy! You should be good now ;-)

@gthmb
Copy link
Contributor

gthmb commented Oct 8, 2021

This looks great! Thanks for sussing through the naming stuff here. I also agree that the exposed name should be the default behavior. I'll make a 0.2.0 release!

@gthmb gthmb merged commit cdd3068 into pixability:main Oct 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants