esbuild Adapter
The esbuild adapter — a framework-agnostic Native Federation adapter, used by the React reference example.
The @softarc/native-federation-esbuild package is a thin, framework-agnostic adapter that plugs the core builder into esbuild. It powers the React reference example and can be used with any stack where you drive your own build script — there is no CLI wrapper or framework coupling.
If you are on Angular, use the Angular adapter instead — it wraps this adapter and adds builder, schematics and dev-server integration. This section is for projects (React, Preact, Lit, plain TypeScript, …) that drive esbuild themselves.
Try the playground — runnable Native Federation host and remote examples on GitHub: native-federation/playground.
What's in the Box
| Piece | Entry point | Purpose |
|---|---|---|
| Builder | runEsBuildBuilder(configPath, options) | High-level entry point. Loads federation.config.js, runs the initial build, and — when watch: true — wires up the file watcher and rebuild queue. See Builder. |
| Low-level adapter | createEsBuildAdapter(config) | Returns an NFBuildAdapter (setup / build / dispose) for users who want to drive buildForFederation directly. See Build Adapters. |
| Build options | EsBuildBuilderOptions | Workspace-level options: outputPath, tsConfig, entryPoints, dev, watch, cachePath, … See Builder. |
| Adapter config | EsBuildAdapterConfig | esbuild-specific extension points: plugins, frameworks, fileReplacements, loader. See Configuration. |
Why an Adapter?
The core builder is intentionally bundler-agnostic — it orchestrates the federation build and delegates the actual bundling to an NFBuildAdapter. The esbuild adapter is that bridge for esbuild:
- Two bundling modes — source-code entries (your exposed modules) and node-modules entries (shared dependencies) are bundled with different esbuild configurations. Shared dependencies get
process.env.NODE_ENVdefined, and — when a framework preset requests it (the default React preset does) — run through@chialab/esbuild-plugin-commonjsso CommonJS libraries Just Work. - File writes + cache integration — esbuild runs with
write: false; the adapter writes outputs intooutputPathand tracks every input file through the core's federation cache, so watch-mode rebuilds only touch what changed. - Watch mode — wraps
esbuild.context()with a debouncedRebuildQueue, anAbortSignal-aware rebuild loop, and the core file watcher. Cancelled rebuilds are aborted cleanly rather than racing. - Framework presets — the
frameworksoption bundles per-framework esbuild settings (file replacements, loaders, extraresolveExtensions, the CommonJS plugin). A React preset ships built-in and is applied by default;fileReplacementslets you swap problematic CJS entry points for their pre-bundled variants (e.g. React'scjs/files).
Install
npm i -D esbuild @softarc/native-federation @softarc/native-federation-esbuild
On the host page, you also want the orchestrator runtime for import-map + remote loading:
npm i @softarc/native-federation-orchestrator
In this Section
- Getting Started — walk through the React reference example end-to-end:
federation.config.js,build.mjs, host page and run commands. - Builder — reference for
runEsBuildBuilderand every field ofEsBuildBuilderOptions, plus the lower-levelcreateEsBuildAdapter. - Adapter Configuration —
EsBuildAdapterConfig: extra esbuildplugins,frameworkspresets,fileReplacementsand customloadermappings. - React & CommonJS Interop — the built-in React preset, the CJS plugin, the
fileReplacementsmap it applies,shareAlloverrides, and the Shadow-DOM custom-element pattern.
Prerequisites
- Node 18+ — the adapter is ESM-only and uses top-level
awaitin typical build scripts. "type": "module"in your project'spackage.json— the adapter and its entry points are native ESM.- esbuild — you install it yourself; the adapter imports
esbuilddirectly rather than bundling a copy. @softarc/native-federation~4.1.0 — pulled in as a dependency; the adapter callsbuildForFederation/rebuildForFederationfrom it.@chialab/esbuild-plugin-commonjsships bundled too, so neither needs a separate install.
A complete working example lives at native-federation-examples-react — React 18 + esbuild + Shadow-DOM custom element + orchestrator host page.