fix: one tsconfig and one disposal per federation build context - #139
Merged
Merged
Conversation
Core now builds shared mappings apart from exposed modules, one Angular context per mapping bundle plus mapping-or-exposed, all against the one federation tsconfig. Rewriting its `files` per context left the other contexts compiling without their entry points: Angular re-reads the tsconfig on every rebuild and serves a dropped file from the shared SourceFileCache, so a mapping edited in watch mode kept its old content without an error. A union of all entry points would make every context compile everything instead. Each context now compiles against a generated tsconfig in the cache dir that extends the federation tsconfig and lists only its own entry points. The federation tsconfig is no longer rewritten, the schematics stop seeding `files`, and a `files` key left in an existing one gets a one-time warning. Default typeRoots are pinned to what the federation tsconfig resolves to, since TypeScript derives them from the leaf config's directory. Refs #138
Only 'mapping-or-exposed' was disposed, but core now builds each mapping bundle as a context of its own, so their compiler plugins never reset Angular's shared TS compilation state before the app build (#47). A mappings-only host has no 'mapping-or-exposed' at all: that dispose threw and the catch hid it, leaving every context undisposed. A full dispose() is no option either, since it stops esbuild, which the app build still needs. The adapter now offers disposeFederationContexts(), which disposes every mapping and exposed context and leaves esbuild running. Refs #138
Each build context now supplies its own `files`, so the list the schematics used to seed only goes stale and triggers a build warning. The 22.2.0 migration removes it from every project's tsconfig.federation.json, editing in place so the file keeps its comments and formatting. Other tsconfigs are left alone. Refs #138
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #138.
native-federation/native-federation-core#145 builds shared mappings apart from exposed modules, so
setup()/build()now run once per mapping bundle (mapping-bundle, ormapping-<pkg>withbuild: 'separate' | 'package') plus once formapping-or-exposed. Each run gets its own Angular context. The adapter still assumed a single context, and broke in two ways.1. Stale mappings in watch mode
Every context shared the one federation tsconfig, and each
setup()replaced itsfileswith only that context's own entry points. Two things in@angular/build(checked in 22.1.8) turn that into a silent failure:SourceFileCache.invalidatenever clearstypeScriptFileCache, so a file that dropped out of the program was served from its old emit, with no error.A union of all entry points in
fileswould fix the staleness, but Angular emits a context's whole program. Every context would then compile everything: N+1 full AOT compiles where core ran one before #145.Fix: each context now compiles against a generated tsconfig that extends the federation tsconfig and lists only its own entry points. The generated files live at
node_modules/.cache/native-federation/<project>/tsconfig/<hash>.<bundle>.json, and the user's tsconfig is never rewritten any more.typeRootsare pinned to what the federation tsconfig resolves to. TypeScript derives them from the leaf config's own directory, which is now the cache dir, and would miss e.g. a pnpm workspace package's ownnode_modules/@types.createRequire. The adapter still doesn't depend ontypescriptat runtime.json5is dropped; only the old rewrite used it.2. Mapping contexts never disposed
The builder disposed only
mapping-or-exposedbefore the app build. The mapping contexts' compiler plugins therefore never reset Angular's shared TS compilation state (#47).mapping-or-exposedcontext at all. Thatdispose()threw, and the.catch(() => undefined)hid it, so nothing was disposed.dispose()is no option, because it stops esbuild, which the app build still needs.Fix: the adapter gets
disposeFederationContexts(), which disposes every mapping and exposed context and leaves esbuild running. The swallowing.catchis gone.3. Stale
filesintsconfig.federation.jsonThe
fileslist intsconfig.federation.jsonis now dead weight.initandupdate22stop seeding it.fileskey in the federation tsconfig itself gets a one-time build warning. An inheritedfilesdoes not warn, since the app tsconfig it extends often hasfiles: ["src/main.ts"].update22-2) removesfilesfrom each project'stsconfig.federation.jsonand leaves other tsconfigs alone. It edits withjsonc-parser, already in the tree via@angular-devkit/*and now declared directly, so comments and formatting survive.Verification
tsc --noEmitclean,knipclean, lint 0 errors (34 pre-existing warnings in unrelated files).angular-examples/angular/tractor-store, on Angular 22.1.8 with packed coremainand packed adapter:explorewithignoreUnusedDeps: false, so theloggingmapping is reachable only throughfiles): on this PR's base, two edits tolibs/loggingduringng build --watchboth rebuilt without error but kept serving the old bundle. With this PR, both edits land.host(mappings only) andexplorepass.ng update @angular-architects/native-federation --migrate-only --from 22.1.3 --to 22.2.0removed exactly thefilesblock from all four projects. The builds that followed pass without the warning.decide) → add to basket → basket (checkout). There were no console errors or failed requests, and each shared mapping was loaded once, from the host.Before release
ng updatewon't run the migration.