vite-plugin-federation
A Vite/Rollup plugin which support Module Federation.
Inspired by Webpack and compatible with Webpack Module Federation.
Install
npm install @originjs/vite-plugin-federation --save-dev
or
yarn add @originjs/vite-plugin-federation --dev
Usage
Using the Module Federation
usually requires more than 2 projects, one as the host side
and one as the remote side
.
Step 1: Configure the remote side.
- for a vite project, modify
vite.config.js
:
import federation from "@originjs/vite-plugin-federation";
export default {
plugins: [
federation({
name: 'remote-app',
filename: 'remoteEntry.js',
exposes: {
'./Button': './src/Button.vue',
},
shared: ['vue']
})
]
}
- for a rollup project, modify
rollup.config.js
:
import federation from '@originjs/vite-plugin-federation'
export default {
input: 'src/index.js',
plugins: [
federation({
name: 'remote-app',
filename: 'remoteEntry.js',
exposes: {
'./Button': './src/button'.
},
shared: ['vue']
})
]
}
Step 2: Configure the host side
- for a vite project, modify
vite.config.js
:
import federation from "@originjs/vite-plugin-federation";
export default {
plugins: [
federation({
name: 'host-app',
remotes: {
remote_app: "http://localhost:5001/assets/remoteEntry.js",
},
shared: ['vue']
})
]
}
- for a rollup project, modify
rollup.config.js
:
import federation from '@originjs/vite-plugin-federation'
export default {
input: 'src/index.js',
plugins: [
federation({
name: 'host-app',
remotes: {
remote_app: "http://localhost:5001/remoteEntry.js",
},
shared: ['vue']
})
]
}
Step 3: Using remote modules on the host side
Using a Vue project as an example
import { createApp, defineAsyncComponent } from "vue";
const app = createApp(Layout);
...
const RemoteButton = defineAsyncComponent(() => import("remote_app/Button"));
app.component("RemoteButton", RemoteButton);
app.mount("#root");
Using remote components in templates
<template>
<div>
<RemoteButton />
</div>
</template>
Example projects