Skip to content

Commit 2eea0d0

Browse files
feat(enhanced): runtimePlugins support pass params (#4118)
Co-authored-by: ScriptedAlchemy <[email protected]>
1 parent c66c21e commit 2eea0d0

File tree

17 files changed

+3008
-1426
lines changed

17 files changed

+3008
-1426
lines changed

.changeset/giant-falcons-tie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/enhanced': patch
3+
---
4+
5+
feat(enhanced): runtimePlugins support pass params

apps/website-new/docs/en/configure/_meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
{
3333
"type": "file",
3434
"name": "runtimeplugins",
35-
"label": "Runtime Plugins"
35+
"label": "runtimePlugins"
3636
},
3737
{
3838
"type": "file",

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

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
# runtimePlugins
22

3-
- Type: `string[]`
3+
- Type: `string[] | Array<[string, object]>`
44
- Required: No
55
- Default: `undefined`
66

7-
The `runtimePlugins` configuration is used to add additional plugins needed at runtime. The value should be the path to the specific plugin, which can be an absolute/relative path or a package name. You can learn more about how to develop `runtimePlugin` details by visiting the [Plugin System](../plugin/dev/index).
7+
The `runtimePlugins` configuration is used to add additional plugins needed at runtime. The value can be:
8+
- A string representing the path to the specific plugin (absolute/relative path or package name)
9+
- An array where each element can be either a string or a tuple with [string path, object options]
10+
11+
You can learn more about how to develop `runtimePlugin` details by visiting the [Plugin System](../plugin/dev/index).
812

913
Once set, runtime plugins will be automatically injected and used during the build process.
1014

1115
- Examples
1216

17+
**Basic usage:**
1318
To create a runtime plugin file, you can name it `custom-runtime-plugin.ts`:
1419

1520
```ts title="custom-runtime-plugin.ts"
@@ -47,3 +52,52 @@ module.exports = {
4752
],
4853
};
4954
```
55+
56+
**With options:**
57+
You can also provide options to runtime plugins by using a tuple format:
58+
59+
```ts title="rspack.config.ts"
60+
const path = require('path');
61+
module.exports = {
62+
plugins: [
63+
new ModuleFederationPlugin({
64+
name: 'host',
65+
remotes: {
66+
'manifest-provider':
67+
'manifest_provider@http://localhost:3011/mf-manifest.json',
68+
},
69+
runtimePlugins: [
70+
path.resolve(__dirname, './custom-runtime-plugin.ts'),
71+
[
72+
path.resolve(__dirname, './another-plugin.ts'),
73+
{
74+
debug: true,
75+
timeout: 5000,
76+
customConfig: 'value'
77+
}
78+
]
79+
],
80+
}),
81+
],
82+
};
83+
```
84+
85+
The plugin can then access these options:
86+
87+
```ts title="another-plugin.ts"
88+
import { ModuleFederationRuntimePlugin } from '@module-federation/enhanced/runtime';
89+
90+
export default function (options: any): ModuleFederationRuntimePlugin {
91+
console.log('Plugin options:', options);
92+
93+
return {
94+
name: 'another-plugin',
95+
beforeInit(args) {
96+
if (options.debug) {
97+
console.log('[debug] beforeInit: ', args);
98+
}
99+
return args;
100+
},
101+
};
102+
}
103+
```

apps/website-new/docs/zh/configure/runtimeplugins.mdx

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
# runtimePlugins
22

3-
- 类型:`string[]`
3+
- 类型:`string[] | Array<[string, object]>`
44
- 是否必填:否
55
- 默认值:`undefined`
66

7-
用于添加运行时需要的额外插件,值为具体插件的路径,支持绝对/相对路径、包名,通过「[插件系统](../plugin/dev/index)」了解更多关于如何开发 runtimePlugin 细节。
7+
用于添加运行时需要的额外插件。值可以是:
8+
- 表示具体插件路径的字符串(支持绝对/相对路径、包名)
9+
- 一个数组,其中每个元素可以是字符串或元组 [字符串路径, 对象配置]
10+
11+
通过「[插件系统](../plugin/dev/index)」了解更多关于如何开发 runtimePlugin 细节。
812

913
设置后,运行时插件会自动在构建时注入并使用。
1014

11-
- examples
15+
- 示例
1216

17+
**基础用法:**
1318
创建运行时插件文件: `custom-runtime-plugin.ts`
1419

1520
```ts title="custom-runtime-plugin.ts"
@@ -48,3 +53,53 @@ module.exports = {
4853
],
4954
};
5055
```
56+
57+
**带参数用法:**
58+
你还可以通过使用元组格式为运行时插件提供配置选项:
59+
60+
```ts title="rspack.config.ts"
61+
const path = require('path');
62+
module.exports = {
63+
plugins: [
64+
new ModuleFederationPlugin({
65+
name: 'host',
66+
remotes: {
67+
'manifest-provider':
68+
'manifest_provider@http://localhost:3011/mf-manifest.json',
69+
},
70+
runtimePlugins: [
71+
path.resolve(__dirname, './custom-runtime-plugin.ts'),
72+
[
73+
path.resolve(__dirname, './another-plugin.ts'),
74+
{
75+
debug: true,
76+
timeout: 5000,
77+
customConfig: 'value'
78+
}
79+
]
80+
],
81+
}),
82+
],
83+
};
84+
85+
```
86+
87+
插件可以访问这些配置选项:
88+
89+
```ts title="another-plugin.ts"
90+
import { ModuleFederationRuntimePlugin } from '@module-federation/enhanced/runtime';
91+
92+
export default function (options: any): ModuleFederationRuntimePlugin {
93+
console.log('插件配置:', options);
94+
95+
return {
96+
name: 'another-plugin',
97+
beforeInit(args) {
98+
if (options.debug) {
99+
console.log('[调试] beforeInit: ', args);
100+
}
101+
return args;
102+
},
103+
};
104+
}
105+
```

packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,30 @@ class FederationRuntimePlugin {
8282
);
8383

8484
let runtimePluginTemplates = '';
85-
const runtimePluginNames: string[] = [];
85+
const runtimePluginCalls: string[] = [];
8686

8787
if (Array.isArray(runtimePlugins)) {
8888
runtimePlugins.forEach((runtimePlugin, index) => {
89+
if (!runtimePlugin) {
90+
return;
91+
}
8992
const runtimePluginName = `plugin_${index}`;
93+
const runtimePluginEntry = Array.isArray(runtimePlugin)
94+
? runtimePlugin[0]
95+
: runtimePlugin;
9096
const runtimePluginPath = normalizeToPosixPath(
91-
path.isAbsolute(runtimePlugin)
92-
? runtimePlugin
93-
: path.join(process.cwd(), runtimePlugin),
97+
path.isAbsolute(runtimePluginEntry)
98+
? runtimePluginEntry
99+
: path.join(process.cwd(), runtimePluginEntry),
94100
);
95-
101+
const paramsStr =
102+
Array.isArray(runtimePlugin) && runtimePlugin.length > 1
103+
? JSON.stringify(runtimePlugin[1])
104+
: 'undefined';
96105
runtimePluginTemplates += `import ${runtimePluginName} from '${runtimePluginPath}';\n`;
97-
runtimePluginNames.push(runtimePluginName);
106+
runtimePluginCalls.push(
107+
`${runtimePluginName} ? (${runtimePluginName}.default || ${runtimePluginName})(${paramsStr}) : false`,
108+
);
98109
});
99110
}
100111
const embedRuntimeLines = Template.asString([
@@ -118,13 +129,11 @@ class FederationRuntimePlugin {
118129
embedRuntimeLines,
119130
`if(!${federationGlobal}.instance){`,
120131
Template.indent([
121-
runtimePluginNames.length
132+
runtimePluginCalls.length
122133
? Template.asString([
123134
`var pluginsToAdd = [`,
124135
Template.indent(
125-
runtimePluginNames.map(
126-
(item) => `${item} ? (${item}.default || ${item})() : false,`,
127-
),
136+
Template.indent(runtimePluginCalls.map((call) => `${call},`)),
128137
),
129138
`].filter(Boolean);`,
130139
`${federationGlobal}.initOptions.plugins = ${federationGlobal}.initOptions.plugins ? `,

0 commit comments

Comments
 (0)