angular-architects / module-federation-plugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Building federation artefacts creates a tsconfig.federation.json with server files in a monorepo project

rezoled opened this issue · comments

I have an NX monorepo project, with multiple node applications.
I created a host and remote angular apps, at first by running the simple NX create angular app command:
nx g @nx/angular:app client/host
nx g @nx/angular:app client/settings
then by using native-federation:init command to create the federation:
nx g @angular-architects/native-federation:init --project client-settings --port 4201 --type remote
nx g @angular-architects/native-federation:init --project client-host --port 4200 --type dynamic-host

Commands ran without any issues.
I then tried to serve the remote:
nx serve client-settings -o
And the command fails since angular compiler tries to build my shared lib server files, even if they were not imported to the app.

Looking at the generated tsconfig.federation.json (which was not deleted since the command could not complete) I can see the following (redacted):

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../../dist/out-tsc",
    "types": []
  },
  "files": [
    "src/main.ts"
  ],
  "include": [
    "src/**/*.d.ts",
    "../../../libs/common/frontend/src/index.ts",
    "../../../libs/common/server/src/index.ts",
    "../../../libs/common/server/src/lib/auth",
    "../../../libs/common/server/src/lib/db",
    "../../../libs/common/server/src/lib/gql",
    "../../../libs/common/server/src/lib/internationalization",
    "../../../libs/common/server/src/lib/kafka",
    "../../../libs/common/server/src/lib/secrets",
    "../../../libs/common/server/src/lib/subscriptions",
    "../../../libs/common/server/src/lib/swagger",
    "../../../libs/common/shared/src/index.ts",
    "../../../libs/constants/src/index.ts",
  ],
  "exclude": [
    "jest.config.ts",
    "src*.spec.ts"
  ]
}

More information, it seems like the created tsconfig.federation.json looks at all of my paths from my root tsconfig.base.json and adds the referenced file to the includes array in the generated tsconfig.federation.json file.
I don't know if this is intentional, but to add every path even if it's not used to the compilation source can be problematic.

And using share instead of shareAll doesn't help

Thanks for pointing this out. By default, Native Federation shares all libs. Please add the mapped path names, e.g. @my-workspace/my-lib, of the server libs into the skip list found in your federation.config.json files

Btw: The next major version won't need to write such a tsconfig.federation.json. We will directly delegate the necessary settings to the Angular Compiler.

Thanks for pointing this out. By default, Native Federation shares all libs. Please add the mapped path names, e.g. @my-workspace/my-lib, of the server libs into the skip list found in your federation.config.json files

Excellent, thanks. Maybe you can add that too in the documentation, because the page for module federation doesn’t really explain the api for the configuration file