Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: improve README and fix typo #109

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 69 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,74 +29,90 @@ pnpm install && pnpm run preview-vv # vite+vite build demo

https://module-federation.io/guide/basic/webpack.html

```js
// vite.config.js
With **@module-federation/vite**, the process becomes delightfully simple, you will only find the differences from a normal Vite configuration.

> This example is with Vue.js
> The @module-federation/vite configuration remains the same for different frameworks.

## The Remote Application configuration

file: **remote/vite.config.ts**

```ts
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { federation } from '@module-federation/vite';
import topLevelAwait from 'vite-plugin-top-level-await';
import { federation } from '@module-federation/vite'; 👈

// https://vitejs.dev/config/
export default defineConfig({
[...]
plugins: [
federation({
name: 'bbc',
remotes: {
mfapp01: 'mfapp01@https://unpkg.com/mf-app-01@1.0.9/dist/remoteEntry.js',
remote2: 'mfapp02@https://unpkg.com/mf-app-02/dist/remoteEntry.js',
remote3:
'remote1@https://unpkg.com/react-manifest-example_remote1@1.0.6/dist/mf-manifest.json',
// "remote4": {
// entry: "http://localhost:xxxx/remoteEntry.js",
// globalEntryName: "xxxx",
// type: "module"
// }
},
[...]
federation({ 👈
name: "remote",
filename: "remoteEntry.js",
exposes: {
'./App': './src/App.vue',
"./remote-app": "./src/App.vue",
},
filename: 'remoteEntry-[hash].js',
// https://github.com/module-federation/vite/issues/87
manifest: true,
shared: {
vue: {},
react: {
requiredVersion: '18',
},
'react-dom': {
requiredVersion: '18',
},
'react-dom/': {
requiredVersion: '18',
shared: ["vue"],
}),
]
[...]
});
```

In this remote app configuration, we define a remoteEntry.js file that will expose the App component.
The shared property ensures that both host and remote applications use the same vue library.

## The Host Application configuration

file **host/vite.config.ts**

```ts
import { defineConfig } from 'vite';
import { federation } from '@module-federation/vite'; 👈

export default defineConfig({
[...]
plugins: [
[...]
federation({ 👈
name: "host",
remotes: {
remote: {
type: "module",
name: "remote",
entry: "https://[...]/remoteEntry.js",
entryGlobalName: "remote",
shareScope: "default",
},
},
filename: "remoteEntry.js",
shared: ["vue"],
}),
// If you set build.target: "chrome89", you can remove this plugin
// topLevelAwait(),
],
server: {
port: 5173,
// dev mode please set origin
origin: 'http://localhost:5173',
},
build: {
target: 'chrome89',
},
]
[...]
});
```

## roadmap
The host app configuration specifies its name, the filename of its exposed remote entry remoteEntry.js, and importantly, the configuration of the remote application to load.

## Load the Remote App

- ✅ ~~feat: generate mf-manifest.json~~
- ✅ ~~feat: support chrome plugin~~
In your host app, you can now import and use the remote app with **defineAsyncComponent**

* ✅ ~~feat: support runtime plugins~~
* feat: nuxt ssr
file **host/src/App.vue**

- feat: download remote d.ts
- feat: generate d.ts
- feat: support @vitejs/plugin-legacy
- feat: Another plugin, when only some remote modules are started, automatically completes HMR[(#54)](https://github.com/module-federation/vite/issues/54)
```ts
<script setup lang="ts">
import { defineAsyncComponent } from "vue";
const RemoteMFE = defineAsyncComponent( 👈
() => import("remote/remote-app")
);
</script>

<template>
<RemoteMFE v-if="!!RemoteMFE" /> 👈
</template>
```

### So far so good 🎉

Expand Down
3 changes: 2 additions & 1 deletion src/virtualModules/virtualRemoteEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ export function generateRemoteEntry(options: NormalizedModuleFederationOptions):
remotes: usedRemotes,
shared: usedShared,
plugins: [${pluginImportNames.map((item) => `${item[0]}()`).join(', ')}],
${options.shareStrategy ? `shareStrategy: ${options.shareStrategy}` : ''});
${options.shareStrategy ? `shareStrategy: ${options.shareStrategy}` : ''}
});
initRes.initShareScopeMap('${options.shareScope}', shared);
initResolve(initRes)
return initRes
Expand Down