Skip to content

Commit 22a3b83

Browse files
authored
fix(data-prefetch): apply DataPrefetchPlugin on demand (#3061)
1 parent a021643 commit 22a3b83

File tree

21 files changed

+248
-537
lines changed

21 files changed

+248
-537
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@module-federation/data-prefetch': patch
3+
'@module-federation/enhanced': patch
4+
'@module-federation/sdk': patch
5+
---
6+
7+
fix(data-prefetch): apply DataPrefetchPlugin on demand

.changeset/swift-pants-rescue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/data-prefetch': patch
3+
---
4+
5+
fix(data-prefetch): set sharedStrategy in build options instead of using runtime plugin

apps/manifest-demo/3010-rspack-provider/src/components/ButtonOldAnt.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import Button from 'antd/lib/button';
22
import { version } from 'antd/package.json';
33
import * as stuff from './stuff.module.css';
4-
console.log(stuff);
54

65
export default function ButtonOldAnt() {
76
return (
7+
// @ts-ignore
88
<Button className={stuff['test-remote2'] + ' test-remote2'}>
99
Button from antd@{version}
1010
</Button>

apps/manifest-demo/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ This example demos manifest
1313
Run `pnpm run app:manifest:dev` to start examples
1414

1515
- manifest-webpack-host: [localhost:3013](http://localhost:3013/)
16-
- view http://localhost:3008/basic to see the basic usage with manifest provider , just the same as js entry provider.
17-
- view http://localhost:3008/preload to see manifest provider can use preloadRemote to optimize page performance
16+
- view http://localhost:3013/basic to see the basic usage with manifest provider , just the same as js entry provider.
17+
- view http://localhost:3013/preload to see manifest provider can use preloadRemote to optimize page performance

apps/manifest-demo/webpack-host/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module.exports = composePlugins(withNx(), withReact(), (config, context) => {
4646
requiredVersion: '^18.3.1',
4747
},
4848
},
49+
dataPrefetch: true,
4950
experiments: { federationRuntime: 'hoisted' },
5051
runtimePlugins: [path.join(__dirname, './runtimePlugin.ts')],
5152
}),

apps/website-new/docs/en/practice/performance/prefetch.mdx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,22 @@ export default function Button () {
125125
};
126126
```
127127

128-
3. Set `dataPrefetch: true` in the producer's ModuleFederationPlugin configuration
128+
3. Set `dataPrefetch: true` in the provider's ModuleFederationPlugin configuration
129129
```ts
130130
new ModuleFederationPlugin({
131131
// ...
132132
dataPrefetch: true
133133
}),
134134
```
135+
136+
4. Set `dataPrefetch: true` in the consumer's ModuleFederationPlugin configuration
137+
```ts
138+
new ModuleFederationPlugin({
139+
// ...
140+
dataPrefetch: true
141+
}),
142+
```
143+
135144
This completes the interface pre-request. After `Button` is used by the hosts, the interface request will be sent out in advance (it will be sent when the js resource is loaded, and the normal project needs to wait until the component is rendered).
136145
In the above example, `Button` will first render `loading...`, and then display the data after 2s
137146
Click `Resend request with parameters` to re-trigger the request and add parameters to update the component

apps/website-new/docs/zh/practice/performance/prefetch.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ export default function Button () {
131131
dataPrefetch: true
132132
}),
133133
```
134+
135+
4. 消费者的 ModuleFederationPlugin 配置处设置 `dataPrefetch: true`
136+
```ts
137+
new ModuleFederationPlugin({
138+
// ...
139+
dataPrefetch: true
140+
}),
141+
```
142+
134143
这样就完成了接口预请求,在 `Button` 被消费者使用后会提前将接口请求发送出去(加载 js 资源时就会发出,正常项目需要等到组件渲染时),
135144
在上面的例子中 `Button` 首先会渲染 `loading...`, 然后在 2s 后展示数据
136145
点击`重新发送带参数的请求`可以重新触发请求并且添加参数,用于更新组件

packages/data-prefetch/package.json

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
"homepage": "https://github.com/module-federation/core",
77
"license": "MIT",
88
"scripts": {
9-
"dev": "cross-env WATCH=true tsup",
10-
"build": "rm -rf dist && tsup",
119
"test": "jest"
1210
},
1311
"publishConfig": {
@@ -38,11 +36,6 @@
3836
"import": "./dist/universal.esm.js",
3937
"require": "./dist/universal.cjs.js",
4038
"types": "./dist/universal.cjs.d.ts"
41-
},
42-
"./shared": {
43-
"import": "./dist/shared.esm.js",
44-
"require": "./dist/shared.cjs.js",
45-
"types": "./dist/shared/.cjs.d.ts"
4639
}
4740
},
4841
"typesVersions": {
@@ -59,9 +52,6 @@
5952
"universal": [
6053
"./dist/universal.cjs.d.ts"
6154
],
62-
"shared": [
63-
"./dist/shared.cjs.d.ts"
64-
],
6555
"babel-plugin": [
6656
"./dist/babel.cjs.d.ts"
6757
]
@@ -79,7 +69,6 @@
7969
"@types/jest": "^29.5.11",
8070
"@types/node": "^17.0.45",
8171
"@types/react": "~18.0.38",
82-
"esbuild-plugin-replace": "^1.4.0",
8372
"jest": "^29.7.0",
8473
"jest-environment-jsdom": "^29.7.0",
8574
"minimist": "^1.2.8",
@@ -88,7 +77,6 @@
8877
"react-router": "^6.21.3",
8978
"react-test-renderer": "^18.2.0",
9079
"ts-jest": "29.0.1",
91-
"tsup": "6.2.0",
9280
"webpack": "5.75.0"
9381
},
9482
"dependencies": {

packages/data-prefetch/rollup.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ module.exports = (rollupConfig, _projectOptions) => {
1818
babel: 'packages/data-prefetch/src/cli/babel.ts',
1919
universal: 'packages/data-prefetch/src/universal/index.ts',
2020
plugin: 'packages/data-prefetch/src/plugin.ts',
21-
shared: 'packages/data-prefetch/src/shared/index.ts',
2221
};
2322

2423
if (Array.isArray(rollupConfig.output)) {

packages/data-prefetch/src/cli/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { Compiler, WebpackPluginInstance } from 'webpack';
1212
import { TEMP_DIR } from '../common/constant';
1313
import { fileExistsWithCaseSync, fixPrefetchPath } from '../common/node-utils';
1414
import { getPrefetchId } from '../common/runtime-utils';
15+
import { SHARED_STRATEGY } from '../constant';
1516

1617
const { RuntimeGlobals, Template } = require(
1718
normalizeWebpackPath('webpack'),
@@ -48,12 +49,14 @@ export class PrefetchPlugin implements WebpackPluginInstance {
4849
}
4950

5051
const runtimePath = path.resolve(__dirname, './plugin.esm.js');
51-
const sharedPath = path.resolve(__dirname, './shared.esm.js');
5252
if (!this.options.runtimePlugins?.includes(runtimePath)) {
5353
this.options.runtimePlugins!.push(runtimePath);
5454
}
55-
if (!this.options.runtimePlugins?.includes(sharedPath)) {
56-
this.options.runtimePlugins!.push(sharedPath);
55+
if (this.options.shareStrategy !== SHARED_STRATEGY) {
56+
this.options.shareStrategy = SHARED_STRATEGY;
57+
console.warn(
58+
`[Module Federation Data Prefetch]: Your shared strategy is set to '${SHARED_STRATEGY}', this is a necessary condition for data prefetch`,
59+
);
5760
}
5861

5962
const encodedName = encodeName(name as string);

0 commit comments

Comments
 (0)