|
| 1 | +# Vite Plugin |
| 2 | + |
| 3 | +- Can build modules that meet the `Module Federation` loading specifications. |
| 4 | +- Can consume modules that adhere to the `Module Federation` specifications using aliases. |
| 5 | +- Can configure shared dependencies for modules, so that when the host environment of the loaded module already has the corresponding dependency, it will not be loaded again. |
| 6 | + |
| 7 | +:::warning Unsupported Options |
| 8 | +Except for the [dev、dts](../../configure/dev.html) options, all options are supported |
| 9 | +::: |
| 10 | +- roadmap 🗓️ |
| 11 | + - When a module has remote types, it will automatically download and consume the types of the remote modules. |
| 12 | + - Consuming remote modules will have hot update capabilities. |
| 13 | + - nuxt ssr |
| 14 | + |
| 15 | +## Quick Start |
| 16 | + |
| 17 | +### Installation |
| 18 | + |
| 19 | +You can install the plugin with the following commands: |
| 20 | + |
| 21 | +import { PackageManagerTabs } from '@theme'; |
| 22 | + |
| 23 | +<PackageManagerTabs |
| 24 | + command={{ |
| 25 | + npm: 'npm add @module-federation/vite --save', |
| 26 | + yarn: 'yarn add @module-federation/vite --save', |
| 27 | + pnpm: 'pnpm add @module-federation/vite --save', |
| 28 | + bun: 'bun add @module-federation/vite --save', |
| 29 | + }} |
| 30 | +/> |
| 31 | + |
| 32 | +### Register the Plugin |
| 33 | + |
| 34 | +In `Vite`, you can add the plugin through the `plugins` configuration item in the `vite.config.js` file: |
| 35 | + |
| 36 | +```ts title='vite.config.js' |
| 37 | +import { federation } from '@module-federation/vite'; |
| 38 | +module.exports = { |
| 39 | + server: { |
| 40 | + origin: 'http://localhost:2000', |
| 41 | + port: 2000, |
| 42 | + }, |
| 43 | + base: "http://localhost:2000", |
| 44 | + plugins: [ |
| 45 | + federation({ |
| 46 | + name: 'vite_provider', |
| 47 | + manifest: true, |
| 48 | + remotes: { |
| 49 | + esm_remote: { |
| 50 | + type: "module", |
| 51 | + name: "esm_remote", |
| 52 | + entry: "https://[...]/remoteEntry.js", |
| 53 | + }, |
| 54 | + var_remote: "var_remote@https://[...]/remoteEntry.js", |
| 55 | + }, |
| 56 | + exposes: { |
| 57 | + './button': './src/components/button', |
| 58 | + }, |
| 59 | + shared: { |
| 60 | + react: { |
| 61 | + singleton: true, |
| 62 | + }, |
| 63 | + 'react/': { |
| 64 | + singleton: true, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }), |
| 68 | + ], |
| 69 | + // Do you need to support build targets lower than chrome89? |
| 70 | + // You can use 'vite-plugin-top-level-await' plugin for that. |
| 71 | + build: { |
| 72 | + target: 'chrome89', |
| 73 | + }, |
| 74 | +}; |
| 75 | +``` |
| 76 | + |
| 77 | +## Configure the Build Plugin |
| 78 | + |
| 79 | +- Type: `ModuleFederationPlugin(options: ModuleFederationOptions)` |
| 80 | + |
| 81 | +- The configuration structure for the Module Federation plugin is as follows: |
| 82 | + |
| 83 | +```ts |
| 84 | +type ModuleFederationOptions = { |
| 85 | + name: string; |
| 86 | + filename?: string; |
| 87 | + remotes?: Array<RemoteInfo>; |
| 88 | + shared?: ShareInfos; |
| 89 | +}; |
| 90 | +``` |
| 91 | + |
| 92 | +You can find detailed explanations of all configuration items on the [Configuration Overview](../../configure/index) page. |
0 commit comments