Skip to content

fix: pass define through to the Angular target and exposed modules - #143

Merged
Aukevanoost merged 3 commits into
mainfrom
fix/define-passthrough
Sep 24, 2026
Merged

Aukevanoost merged 3 commits into
mainfrom
fix/define-passthrough

Conversation

@Aukevanoost

@Aukevanoost Aukevanoost commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #129

  • Builder define option — merged over the Angular target's own define, so values can be passed per invocation. Same shape as Angular's (additionalProperties: string), so Angular CLI's --define KEY=VALUE parsing applies to it.
  • Dev server — serveWithVite reads the build target's options itself, so a define merged into the builder options never reached the main bundle under serve (exposed modules did get it). It's merged in the internal Angular builder instead, next to the federation externals. createInternalAngularBuilder moved into its own module so this is unit-tested.
  • Exposed modules — the federation esbuild context hardcoded its define, so identifiers the application target defines stayed unreplaced in exposed modules and threw a ReferenceError once a host loaded them. This was already broken for define in the target's own options, not only for CLI overrides. User defines go first so Angular's flags win, matching application-code-bundle.ts.

Shared externals are left alone: a changing define there would have to become part of the externals cache key.

Verified on angular-examples/angular/nx (mfe1, BUILD_ID referenced in main.ts and the exposed bootstrap.ts)

main.js exposed Component.js
before, define in the esbuild target replaced unreplaced
before, --define.BUILD_ID=… unreplaced unreplaced
after, build --define.BUILD_ID="\"'abc'\"" "abc" "abc"
after, serve --define.BUILD_ID="\"'abc'\"" "abc" "abc"
serve without the dev-server pass-through (control) unreplaced "abc"

Nx notes: it strips one layer of quotes (so "'abc'" becomes the identifier abc), coerces numbers (=12345 fails the string schema), and rejects the --define KEY=VALUE form outright. Hence the nested quotes above.

The federation esbuild context hardcoded its `define`, so identifiers the application
target defines stayed unreplaced in exposed modules and threw a ReferenceError once a
host loaded them. User defines go first so Angular's own flags win, as in the
application builder.
Merged over the target's own `define`, so values can be supplied per invocation,
e.g. `nx run app:build --define.BUILD_ID="\"'123'\""`.

Closes #129
serveWithVite reads the build target's options itself, so the define merged into
ngBuilderOptions never reached the main bundle under serve, while exposed modules
did get it. Merge it in the internal Angular builder instead, next to the externals.
Moves createInternalAngularBuilder into its own module so it can be tested.
@Aukevanoost
Aukevanoost merged commit 2c798b7 into main Sep 24, 2026
1 check passed
Aukevanoost added a commit that referenced this pull request Sep 24, 2026
Backports 22.1.3..22.2.0 onto the Angular 21 layout.

Incoming:
- #138: each federation build context gets its own generated tsconfig
  (write-context-tsconfig) instead of rewriting `files` in
  tsconfig.federation.json, and every mapping/exposed context is disposed
  before the app build. init no longer writes `files`; the new update22-2
  migration removes it from existing tsconfig.federation.json files.
- #143: builder accepts a `define` option, merged over the Angular target's
  define for exposed modules and passed through to the dev server
  (createInternalAngularBuilder moved to internal-angular-builder.ts).
- #141: config types exported, shared builders accepted without get(), init
  generates the shared config with fromPackageJson and patches @angular/core.
- #142: README overhaul pointing to the documentation website;
  MIGRATION_GUIDE.md removed in favour of the website's migration guide.
- @softarc/native-federation ~4.7.0, jsonc-parser added, json5 dropped.

Angular 21 specific adaptations:
- Layout: write-context-tsconfig lands in src/utils/ next to the bundler;
  src/utils/update-federation-tsconfig.* is removed as on main. Comment
  pointers to tools/esbuild/ respelled to utils/.
- Skipped d535e07 (Angular 22.2 upgrade): node-modules-bundler keeps the 21.2
  JavaScriptTransformer/Cache signatures, and federation-source-files keeps
  reading SourceFileCache.keys(), which is still a Map on 21.2.
- internal-angular-builder drops instrumentForCoverage, which this branch
  never carried; it only forwards define.
- Restores the pre-app-build dispose (#47) that the earlier main merge
  (d00b9fc) dropped, now as disposeFederationContexts() per #138.
- update22-2 keeps main's path and key for easier future merges, versioned
  21.2.13 so it runs when updating the -v4 package.
- README respelled for -v4 (badges, install, init, config import, ng update)
  with the version example set to 21.2.x.
- Kept deleted as before: update22 schematic, share-utils.spec.ts and
  tools/esbuild/angular-esbuild-adapter.spec.ts.
- Kept the ng21 pins, -v4 package name, 21.2.12 version and the TS 5.9 /
  eslint 9 toolchain; took only the core bump and jsonc-parser from main.
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.

Allow passing define cli arguments to angular build target from native federation builder

1 participant