Skip to content

Commit ca546d8

Browse files
authored
feat(vite): Compatibility documentation (#3065)
1 parent b41faaa commit ca546d8

File tree

5 files changed

+187
-3
lines changed

5 files changed

+187
-3
lines changed

apps/website-new/docs/en/configure/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Configuration Overview
22

3-
The current page lists all the configuration options for `Module Federation`. Please refer to the documentation for [`Rspack Plugin`](../guide/basic/rspack) and [`Webpack Plugin`](../guide/basic/webpack) to understand how to use them.
3+
The current page lists all the configuration options for `Module Federation`. Please refer to the documentation for [`Rspack Plugin`](../guide/basic/rspack) and [`Webpack Plugin`](../guide/basic/webpack) and [`Vite Plugin`](../guide/basic/vite) to understand how to use them.
44

55
```ts
66
type ModuleFederationOptions = {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
["runtime", "rspack", "webpack", "chrome-devtool", "type-prompt", "error-catalog"]
1+
["runtime", "rspack", "webpack", "vite", "chrome-devtool", "type-prompt", "error-catalog"]
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
["runtime", "rspack", "webpack", "chrome-devtool", "type-prompt"]
1+
["runtime", "rspack", "webpack", "vite", "chrome-devtool", "type-prompt"]
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Vite Plugin
2+
3+
- 能够构建出满足 `Module Federation` 加载规范的模块
4+
- 能够使用别名消费 `Module Federation` 规范的模块
5+
- 能够设置模块的共享依赖配置,当加载模块的宿主环境已经存在对应依赖时将不会重复加载
6+
7+
:::warning 不支持的选项
8+
除了[dev、dts](../../configure/dev.html) 选项外,其他选项全部支持
9+
:::
10+
- roadmap 🗓️
11+
- 当模块具备远程类型时将会自动把远程模块的类型下载下来消费
12+
- 消费远程模块时将具备热更新能力
13+
- nuxt ssr
14+
15+
## 快速开始
16+
17+
### 安装
18+
19+
你可以通过如下的命令安装插件:
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+
### 注册插件
33+
34+
`vite` 中,你可以通过 `vite.config.js` 配置文件中的 `plugins` 配置项来添加插件:
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+
remotes: {
44+
esm_remote: {
45+
type: "module",
46+
name: "esm_remote",
47+
entry: "https://[...]/remoteEntry.js",
48+
},
49+
var_remote: "var_remote@https://[...]/remoteEntry.js",
50+
},
51+
base: "http://localhost:2000",
52+
plugins: [
53+
federation({
54+
name: 'vite_provider',
55+
manifest: true,
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+
## 配置构建插件
78+
79+
- Type: `ModuleFederationPlugin(options: ModuleFederationOptions)`
80+
81+
- Module federation 插件的配置结构如下所示:
82+
83+
```ts
84+
type ModuleFederationOptions {
85+
name: string;
86+
filename?: string,
87+
remotes?: Array<RemoteInfo>;
88+
shared?: ShareInfos;
89+
};
90+
```
91+
92+
你可以在 [Config 总览](../../configure/index) 页面找到所有配置项的详细说明。

0 commit comments

Comments
 (0)