Vite Federation Plugin
@originjs/vite-plugin-federation, producer/consumer setup, dynamic import of remotes.
Vite Federation Plugin is a free Vue Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Federation on Vite
Module Federation started in Webpack, but many Vue apps use Vite. The community plugin @originjs/vite-plugin-federation brings federation to Vite-based producers and consumers.
Installing the Plugin
Add the plugin to both the producing app (the remote) and the consuming app (the host).
// terminal
// npm install -D @originjs/vite-plugin-federationProducer Config
In the producer (remote) vite.config.ts, add federation with a name and an exposes object mapping public names to local files.
import federation from "@originjs/vite-plugin-federation"
export default {
plugins: [
federation({
name: "remote_app",
filename: "remoteEntry.js",
exposes: {
"./Widget": "./src/components/Widget.vue",
},
}),
],
}Build Settings for Federation
The plugin requires a modern build target and disabled minify-of-modules in some setups. Set the build target so dynamic imports and top-level await work.
export default {
plugins: [federation({ /* ... */ })],
build: {
target: "esnext",
},
}The exposes Object
Like Webpack, keys are the public import paths consumers use; values are paths to local source files. Expose only what other teams need.
exposes: {
"./Widget": "./src/components/Widget.vue",
"./useCart": "./src/composables/useCart.ts",
}Consumer Config
In the consumer (host), add federation with a remotes object mapping an alias to the URL of the remote’s remoteEntry.js.
import federation from "@originjs/vite-plugin-federation"
export default {
plugins: [
federation({
name: "host_app",
remotes: {
remote_app: "https://remote.example.com/assets/remoteEntry.js",
},
}),
],
}The remotes URL
The value is the full URL to the built remoteEntry.js. In dev you point at the remote’s preview server; in production at its deployed CDN path.
remotes: {
remote_app: "https://remote.example.com/assets/remoteEntry.js",
}Shared Dependencies
Add a shared array so the host and remote use a single copy of common libraries. Listing vue is essential to keep one Vue instance.
federation({
name: "host_app",
remotes: { /* ... */ },
shared: ["vue", "pinia"],
})Consuming a Remote Component
Import from the alias plus the exposed name. Use a dynamic import with defineAsyncComponent since the code is fetched at runtime.
import { defineAsyncComponent } from "vue"
const Widget = defineAsyncComponent(() =>
import("remote_app/Widget")
)Type Declarations for Remotes
TypeScript does not know remote module types. Declare them so imports type-check.
// remotes.d.ts
declare module "remote_app/Widget" {
import type { DefineComponent } from "vue"
const component: DefineComponent
export default component
}Preview, Not Dev, for Remotes
Federation needs the built remoteEntry.js, which the Vite dev server does not produce the same way. Build the remote and serve it with vite preview so the host can fetch it.
// remote terminal
// vite build && vite preview --port 5001Quick Check
Check your understanding of the Vite federation plugin.
Recap
You learned Vite federation:
@originjs/vite-plugin-federationbrings Module Federation to Vite.- The producer config sets
nameand anexposesobject (public name to local file). - The consumer config sets a
remotesobject mapping an alias to the remote’sremoteEntry.jsURL. - List shared deps (including
vue) inshared. - Consume via dynamic
import("alias/Module"); build +vite previewthe remote.
Frequently asked questions
Is the “Vite Federation Plugin” lesson free?
Yes — the full text of “Vite Federation Plugin” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.
What will I learn in “Vite Federation Plugin”?
@originjs/vite-plugin-federation, producer/consumer setup, dynamic import of remotes. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Vue Academy?
No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Vite Federation Plugin” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Vue Academy lesson?
Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.