Skip to content

Commit

Permalink
chore: add provideExternalRuntime field
Browse files Browse the repository at this point in the history
  • Loading branch information
2heal1 committed Dec 20, 2024
1 parent 4c3951d commit 2f5bc75
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 99 deletions.
3 changes: 2 additions & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"@module-federation/retry-plugin",
"@module-federation/data-prefetch",
"@module-federation/rsbuild-plugin",
"@module-federation/error-codes"
"@module-federation/error-codes",
"@module-federation/inject-external-runtime-core-plugin"
]
],
"ignorePatterns": ["^alpha|^beta"],
Expand Down
2 changes: 1 addition & 1 deletion .changeset/nervous-starfishes-whisper.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
'@module-federation/runtime': patch
---

feat: add externalRuntime option to help optimize assets size
feat: add externalRuntime and provideExternalRuntime fields to help optimize assets size
5 changes: 1 addition & 4 deletions apps/manifest-demo/webpack-host/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ module.exports = composePlugins(withNx(), withReact(), (config, context) => {
'modern-js-provider': 'app1@http://127.0.0.1:4001/mf-manifest.json',
},
filename: 'remoteEntry.js',
exposes: {
'./Button': './src/Button.tsx',
},
shared: {
lodash: {},
antd: {},
Expand All @@ -50,7 +47,7 @@ module.exports = composePlugins(withNx(), withReact(), (config, context) => {
// experiments: { federationRuntime: 'hoisted' },
runtimePlugins: [path.join(__dirname, './runtimePlugin.ts')],
experiments: {
externalRuntime: 'provide',
provideExternalRuntime: true,
federationRuntime: 'hoisted',
},
}),
Expand Down
18 changes: 12 additions & 6 deletions apps/website-new/docs/en/configure/experiments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,20 @@ This allows module federation to be avaliable ahead of time, thus enabling "Asyn

## externalRuntime

- Type: `boolean | 'provide'`
- Type: `boolean`
- Required: No
- Default: `false`

Used to control whether external mf runtime or mf runtime is provided.
After setting `true`, the external MF runtime will be used and the runtime provided by the consumer will be used. (Please make sure your consumer has `provideExternalRuntime: true` set, otherwise it will not run properly!)

Different behaviors can be performed through the following configurations
## provideExternalRuntime

* `true`: Usually set in the producer, after setting it will external MF runtime and use the runtime provided by the consumer. (Please make sure your consumer has `externalRuntime: "provide"` set, otherwise it will not run properly!)
* `'provide'`: Usually set in the top-level consumer. After setting, MF runtime will be injected globally. If the producer sets `externalRuntime: true`, then the runtime provided by this consumer will be used.
* `false`: Default configuration, no operation will be performed on runtime
- Type: `boolean`
- Required: No
- Default: `false`

::: warning note
Make sure to only configure it on the topmost consumer! If multiple consumers inject runtime at the same time, the ones executed later will not overwrite the existing runtime.
:::

Setting `true` will inject the MF runtime at the consumer.
18 changes: 12 additions & 6 deletions apps/website-new/docs/zh/configure/experiments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@ new ModuleFederationPlugin({

## externalRuntime

- Type: `boolean | 'provide'`
- Type: `boolean`
- Required: No
- Default: `false`

用于控制是否 external mf runtime 或提供 mf runtime
设置 `true` 后 会 external MF runtime,并使用消费者提供的 runtime 。(请确保你的消费者有设置 `provideExternalRuntime: true`,否则无法正常运行!)

可以通过下列的配置来进行不同的行为
## provideExternalRuntime

* `true`: 通常在生产者设置,设置后 会 external MF runtime,并使用消费者提供的 runtime 。(请确保你的消费者有设置 `externalRuntime: "provide"`,否则无法正常运行!)
* `'provide'`: 通常在最顶层消费者设置,设置后会在全局注入 MF runtime ,生产者若设置了 `externalRuntime: true` ,那么就会使用此消费者提供的运行时。
* `false`: 默认配置,不会对 runtime 进行任何操作
- Type: `boolean`
- Required: No
- Default: `false`

::: warning 注意
请确保仅在最顶层消费者配置!若同时有多个消费者注入 runtime,后执行的不会覆盖已有的 runtime。
:::

设置 `true` 后会在消费者处注入 MF runtime。
11 changes: 9 additions & 2 deletions packages/enhanced/src/lib/container/ModuleFederationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,21 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
compiler,
);
}
if (options.experiments?.externalRuntime === 'provide') {
if (options.experiments?.provideExternalRuntime) {
if (options.exposes) {
throw new Error(
'You can only set provideExternalRuntime: true in pure consumer which not expose modules.',
);
}
const runtimePlugins = options.runtimePlugins || [];
options.runtimePlugins = runtimePlugins.concat(
require.resolve(
'@module-federation/inject-external-runtime-core-plugin',
),
);
} else if (options.experiments?.externalRuntime === true) {
}

if (options.experiments?.externalRuntime === true) {
const Externals = compiler.webpack.ExternalsPlugin || ExternalsPlugin;
new Externals(compiler.options.externalsType || 'global', {
'@module-federation/runtime-core': '_FEDERATION_RUNTIME_CORE',
Expand Down
149 changes: 73 additions & 76 deletions packages/enhanced/src/schemas/container/ContainerPlugin.check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,11 @@ function l(
{
const t = f;
for (const t in r)
if ('externalRuntime' !== t && 'federationRuntime' !== t)
if (
'externalRuntime' !== t &&
'federationRuntime' !== t &&
'provideExternalRuntime' !== t
)
return (
(l.errors = [
{
Expand All @@ -1070,45 +1074,21 @@ function l(
);
if (t === f) {
if (void 0 !== r.externalRuntime) {
let t = r.externalRuntime;
const e = f,
n = f;
let s = !1;
const a = f;
if ('provide' !== t) {
const r = {
params: {},
};
null === p ? (p = [r]) : p.push(r), f++;
}
var u = a === f;
if (((s = s || u), !s)) {
const r = f;
if ('boolean' != typeof t) {
const r = {
params: {
type: 'boolean',
},
};
null === p ? (p = [r]) : p.push(r), f++;
}
(u = r === f), (s = s || u);
}
if (!s) {
const r = {
params: {},
};
const t = f;
if ('boolean' != typeof r.externalRuntime)
return (
null === p ? (p = [r]) : p.push(r),
f++,
(l.errors = p),
(l.errors = [
{
params: {
type: 'boolean',
},
},
]),
!1
);
}
(f = n), null !== p && (n ? (p.length = n) : (p = null));
var c = e === f;
} else c = !0;
if (c)
var u = t === f;
} else u = !0;
if (u) {
if (void 0 !== r.federationRuntime) {
let t = r.federationRuntime;
const e = f,
Expand All @@ -1121,8 +1101,8 @@ function l(
};
null === p ? (p = [r]) : p.push(r), f++;
}
var m = a === f;
if (((s = s || m), !s)) {
var c = a === f;
if (((s = s || c), !s)) {
const r = f;
if ('boolean' != typeof t) {
const r = {
Expand All @@ -1132,7 +1112,7 @@ function l(
};
null === p ? (p = [r]) : p.push(r), f++;
}
(m = r === f), (s = s || m);
(c = r === f), (s = s || c);
}
if (!s) {
const r = {
Expand All @@ -1147,14 +1127,31 @@ function l(
}
(f = n),
null !== p && (n ? (p.length = n) : (p = null)),
(c = e === f);
} else c = !0;
(u = e === f);
} else u = !0;
if (u)
if (void 0 !== r.provideExternalRuntime) {
const t = f;
if ('boolean' != typeof r.provideExternalRuntime)
return (
(l.errors = [
{
params: {
type: 'boolean',
},
},
]),
!1
);
u = t === f;
} else u = !0;
}
}
}
}
var y = e === f;
} else y = !0;
if (y) {
var m = e === f;
} else m = !0;
if (m) {
if (void 0 !== t.exposes) {
const r = f;
s(t.exposes, {
Expand All @@ -1165,9 +1162,9 @@ function l(
}) ||
((p = null === p ? s.errors : p.concat(s.errors)),
(f = p.length)),
(y = r === f);
} else y = !0;
if (y) {
(m = r === f);
} else m = !0;
if (m) {
if (void 0 !== t.filename) {
let e = t.filename;
const n = f;
Expand Down Expand Up @@ -1202,9 +1199,9 @@ function l(
!1
);
}
y = n === f;
} else y = !0;
if (y) {
m = n === f;
} else m = !0;
if (m) {
if (void 0 !== t.library) {
const r = f;
i(t.library, {
Expand All @@ -1215,9 +1212,9 @@ function l(
}) ||
((p = null === p ? i.errors : p.concat(i.errors)),
(f = p.length)),
(y = r === f);
} else y = !0;
if (y) {
(m = r === f);
} else m = !0;
if (m) {
if (void 0 !== t.name) {
let r = t.name;
const e = f;
Expand All @@ -1243,9 +1240,9 @@ function l(
!1
);
}
y = e === f;
} else y = !0;
if (y) {
m = e === f;
} else m = !0;
if (m) {
if (void 0 !== t.runtime) {
let r = t.runtime;
const e = f,
Expand All @@ -1258,8 +1255,8 @@ function l(
};
null === p ? (p = [r]) : p.push(r), f++;
}
var h = a === f;
if (((s = s || h), !s)) {
var y = a === f;
if (((s = s || y), !s)) {
const t = f;
if (f === t)
if ('string' == typeof r) {
Expand All @@ -1277,7 +1274,7 @@ function l(
};
null === p ? (p = [r]) : p.push(r), f++;
}
(h = t === f), (s = s || h);
(y = t === f), (s = s || y);
}
if (!s) {
const r = {
Expand All @@ -1292,9 +1289,9 @@ function l(
}
(f = n),
null !== p && (n ? (p.length = n) : (p = null)),
(y = e === f);
} else y = !0;
if (y) {
(m = e === f);
} else m = !0;
if (m) {
if (void 0 !== t.runtimePlugins) {
let r = t.runtimePlugins;
const e = f;
Expand Down Expand Up @@ -1334,8 +1331,8 @@ function l(
};
null === p ? (p = [r]) : p.push(r), f++;
}
var g = o === f;
if (((a = a || g), !a)) {
var h = o === f;
if (((a = a || h), !a)) {
const r = f;
if (f === r)
if (
Expand Down Expand Up @@ -1379,9 +1376,9 @@ function l(
null === p ? (p = [r]) : p.push(r),
f++;
}
var d = r === f;
} else d = !0;
if (d)
var g = r === f;
} else g = !0;
if (g)
if (void 0 !== t.import) {
let r = t.import;
const e = f;
Expand All @@ -1407,8 +1404,8 @@ function l(
: p.push(r),
f++;
}
d = e === f;
} else d = !0;
g = e === f;
} else g = !0;
}
}
} else {
Expand All @@ -1419,7 +1416,7 @@ function l(
};
null === p ? (p = [r]) : p.push(r), f++;
}
(g = r === f), (a = a || g);
(h = r === f), (a = a || h);
}
if (!a) {
const r = {
Expand All @@ -1441,9 +1438,9 @@ function l(
}
}
}
y = e === f;
} else y = !0;
if (y)
m = e === f;
} else m = !0;
if (m)
if (void 0 !== t.shareScope) {
let r = t.shareScope;
const e = f;
Expand All @@ -1469,8 +1466,8 @@ function l(
!1
);
}
y = e === f;
} else y = !0;
m = e === f;
} else m = !0;
}
}
}
Expand Down
Loading

0 comments on commit 2f5bc75

Please sign in to comment.