0.2.5 • Published 5 months ago

@liuyang0826/vite-plugin-federation v0.2.5

Weekly downloads
-
License
MulanPSL-2.0
Repository
github
Last release
5 months ago

vite-plugin-federation

NOTE: this plugin is forked from @originjs/vite-plugin-federation, It is a low-level tool for specific use cases.

A Vite/Rollup plugin which support Module Federation. Inspired by Webpack and compatible with Webpack Module Federation.

Install

npm install @liuyang0826/vite-plugin-federation --save-dev

or

yarn add @liuyang0826/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:
// vite.config.js
import federation from "@liuyang0826/vite-plugin-federation";
export default {
    plugins: [
        federation({
            name: 'remote-app',
            filename: 'remoteEntry.js',
            // Modules to expose
            exposes: {
                './Button': './src/Button.vue',
            },
            shared: ['vue']
        })
    ]
}
  • for a rollup project, modify rollup.config.js:
// rollup.config.js
import federation from '@liuyang0826/vite-plugin-federation'
export default {
    input: 'src/index.js',
    plugins: [
        federation({
            name: 'remote-app',
            filename: 'remoteEntry.js',
            // Modules to expose
            exposes: {
                './Button': './src/button'.
            },
            shared: ['vue']
        })
    ]
}

Step 2: Configure the host side

  • for a vite project, modify vite.config.js:
// vite.config.js
import federation from "@liuyang0826/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:
// rollup.config.js
import federation from '@liuyang0826/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

ExamplesHostRemote
basic-host-remoterollup+esmrollup+esm
react-in-vuevite+esmvite+esm
simple-react-esmrollup+esmrollup+esm
simple-react-systemjsrollup+systemjsrollup+systemjs
simple-react-webpackrollup+systemjswebpack+systemjs
vue2-demovite+esmvite+esm
vue3-advanced-demovite+esm vue-router/piniavite+esmvue-router/pinia
vue3-demo-esmvite+esmvite+esm
vue3-demo-systemjsvite+systemjsvite+systemjs
vue3-demo-webpack-esm-esmvite/webpack+esmvite/webpack+esm
vue3-demo-webpack-esm-varvite+esmwebpack+var
vue3-demo-webpack-systemjsvite+systemjswebpack+systemjs
0.2.5

5 months ago

0.2.4

8 months ago

0.2.3

8 months ago

0.2.2

8 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.1.1

8 months ago

0.1.0

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago