@module-federation/modern-js 0.0.0-next-20240701075157 → 0.0.0-next-20240708090519
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -163
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -2,166 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
This plugin provides Module Federation supporting functions for Modern.js
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
- modern.js ^2.54.2
|
|
8
|
-
- Server-Side Rendering
|
|
9
|
-
|
|
10
|
-
We highly recommend referencing this application which takes advantage of the best capabilities:
|
|
11
|
-
[module-federation example](https://github.com/module-federation/core/tree/feat/modernjs-ssr/apps/modernjs-ssr)
|
|
12
|
-
|
|
13
|
-
## Quick Start
|
|
14
|
-
|
|
15
|
-
### Installation
|
|
16
|
-
|
|
17
|
-
You can install the plugin with the following commands:
|
|
18
|
-
|
|
19
|
-
import { PackageManagerTabs } from '@theme';
|
|
20
|
-
|
|
21
|
-
<PackageManagerTabs
|
|
22
|
-
command={{
|
|
23
|
-
npm: 'npm add @module-federation/modern-js --save',
|
|
24
|
-
yarn: 'yarn add @module-federation/modern-js --save',
|
|
25
|
-
pnpm: 'pnpm add @module-federation/modern-js --save',
|
|
26
|
-
bun: 'bun add @module-federation/modern-js --save',
|
|
27
|
-
}}
|
|
28
|
-
/>
|
|
29
|
-
|
|
30
|
-
### Apply Plugin
|
|
31
|
-
|
|
32
|
-
Apply this plugin in `plugins` of `modern.config.ts`:
|
|
33
|
-
|
|
34
|
-
```ts title="modern.config.ts"
|
|
35
|
-
import { appTools, defineConfig } from '@modern-js/app-tools';
|
|
36
|
-
import { moduleFederationPlugin } from '@module-federation/modern-js';
|
|
37
|
-
|
|
38
|
-
export default defineConfig({
|
|
39
|
-
dev: {
|
|
40
|
-
port: 3005,
|
|
41
|
-
},
|
|
42
|
-
runtime: {
|
|
43
|
-
router: true,
|
|
44
|
-
},
|
|
45
|
-
// moduleFederationPlugin is a plug-in for modern.js, which can make certain modifications to the build/runtime
|
|
46
|
-
plugins: [appTools(), moduleFederationPlugin()],
|
|
47
|
-
});
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
Then create the `module-federation.config.ts` file and write the required configuration:
|
|
51
|
-
|
|
52
|
-
```ts title="module-federation.config.ts"
|
|
53
|
-
import { createModuleFederationConfig } from '@module-federation/modern-js';
|
|
54
|
-
export default createModuleFederationConfig({
|
|
55
|
-
name: 'host',
|
|
56
|
-
remotes: {
|
|
57
|
-
remote: 'remote@http://localhost:3006/mf-manifest.json',
|
|
58
|
-
},
|
|
59
|
-
shared: {
|
|
60
|
-
react: { singleton: true },
|
|
61
|
-
'react-dom': { singleton: true },
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### Type support
|
|
67
|
-
|
|
68
|
-
add `/// <reference types='@module-federation/modern-js/types' />` in `modern-app-env.d.ts` to get type support.
|
|
69
|
-
|
|
70
|
-
```diff title='modern-app-env.d.ts'
|
|
71
|
-
+ /// <reference types='@module-federation/modern-js/types' />
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
## Server-Side Rendering
|
|
75
|
-
|
|
76
|
-
:::info
|
|
77
|
-
For a better performance experience, Module Federation X Modern.js SSR only supports stream SSR.
|
|
78
|
-
:::
|
|
79
|
-
|
|
80
|
-
There is no difference between using Module Federation in SSR scenarios and CSR scenarios. Developers can just keep following the original development behavior.
|
|
81
|
-
|
|
82
|
-
But for a better user experience, we provide supporting functions/components to help developers better use Module Federation.
|
|
83
|
-
|
|
84
|
-
### createRemoteSSRComponent
|
|
85
|
-
|
|
86
|
-
```ts
|
|
87
|
-
declare function createRemoteSSRComponent(
|
|
88
|
-
props: CreateRemoteSSRComponentOptions
|
|
89
|
-
): (props: ComponentType) => React.JSX.Element;
|
|
90
|
-
|
|
91
|
-
type CreateRemoteSSRComponentOptions = {
|
|
92
|
-
loader: () => Promise<T>;
|
|
93
|
-
loading: React.ReactNode;
|
|
94
|
-
fallback: ErrorBoundaryPropsWithComponent['FallbackComponent'];
|
|
95
|
-
export?: E;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
type ComponentType = T[E] extends (...args: any) => any
|
|
99
|
-
? Parameters<T[E]>[0] extends undefined
|
|
100
|
-
? Record<string, never>
|
|
101
|
-
: Parameters<T[E]>[0]
|
|
102
|
-
: Record<string, never>;
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
This function will also help inject the corresponding style tag/script while loading the component. This behavior can help avoid the CSS flickering problem caused by streaming rendering and accelerate the PID (first screen interactive time).
|
|
106
|
-
|
|
107
|
-
#### Example
|
|
108
|
-
|
|
109
|
-
```tsx
|
|
110
|
-
import React, { FC, memo, useEffect } from 'react';
|
|
111
|
-
import { registerRemotes, createRemoteSSRComponent } from '@modern-js/runtime/mf';
|
|
112
|
-
// The remote declared in the build plug-in can be imported directly at the top level
|
|
113
|
-
import RemoteComp from 'remote/Image';
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const RemoteSSRComponent = createRemoteSSRComponent({
|
|
117
|
-
// The remote declared in the build plug-in can also be loaded using this function: loader: () => import('remote/Image'),
|
|
118
|
-
loader: () => loadRemote('dynamic_remote/Image'),
|
|
119
|
-
loading: <div>loading...</div>,
|
|
120
|
-
fallback: ({ error }) => {
|
|
121
|
-
if (error instanceof Error && error.message.includes('not exist')) {
|
|
122
|
-
return <div>fallback - not existed id</div>;
|
|
123
|
-
}
|
|
124
|
-
return <div>fallback</div>;
|
|
125
|
-
},
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
const Product: FC = () => {
|
|
129
|
-
registerRemotes([
|
|
130
|
-
{
|
|
131
|
-
name: 'dynamic_remote',
|
|
132
|
-
entry: 'http://localhost:3008/mf-manifest.json',
|
|
133
|
-
},
|
|
134
|
-
]);
|
|
135
|
-
|
|
136
|
-
const fallback = (err: Error) => {
|
|
137
|
-
if (err.message.includes('does not exist in container')) {
|
|
138
|
-
return <div>404</div>;
|
|
139
|
-
}
|
|
140
|
-
throw err;
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
return <>
|
|
144
|
-
<RemoteSSRComponent />
|
|
145
|
-
<RemoteComp />
|
|
146
|
-
</>;
|
|
147
|
-
};
|
|
148
|
-
export default Product;
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
#### loading
|
|
152
|
-
|
|
153
|
-
- Type:`React.ReactNode`
|
|
154
|
-
- Required: Yes
|
|
155
|
-
- Default value: `undefined`
|
|
156
|
-
|
|
157
|
-
Set module loading status.
|
|
158
|
-
|
|
159
|
-
#### fallback
|
|
160
|
-
|
|
161
|
-
- Type:`((err: Error) => React.ReactElement)`
|
|
162
|
-
- Required: Yes
|
|
163
|
-
- Default value: `undefined`
|
|
164
|
-
|
|
165
|
-
A fault-tolerant component that is rendered when the component fails to **load** or **render**.
|
|
166
|
-
|
|
167
|
-
Note: This component only renders this fault-tolerant component on the client side when **rendering** fails.
|
|
5
|
+
See [documentation](https://module-federation.io/guide/framework/modernjs.html) for more details .
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/modern-js",
|
|
3
|
-
"version": "0.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-20240708090519",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist/",
|
|
6
6
|
"types.d.ts",
|
|
@@ -48,9 +48,9 @@
|
|
|
48
48
|
"node-fetch": "~3.3.0",
|
|
49
49
|
"react-error-boundary": "4.0.13",
|
|
50
50
|
"hoist-non-react-statics": "3.3.2",
|
|
51
|
-
"@module-federation/sdk": "0.0.0-next-
|
|
52
|
-
"@module-federation/enhanced": "0.0.0-next-
|
|
53
|
-
"@module-federation/node": "0.0.0-next-
|
|
51
|
+
"@module-federation/sdk": "0.0.0-next-20240708090519",
|
|
52
|
+
"@module-federation/enhanced": "0.0.0-next-20240708090519",
|
|
53
|
+
"@module-federation/node": "0.0.0-next-20240708090519"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@types/hoist-non-react-statics": "3.3.2",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"@modern-js/runtime": "^2.54.2",
|
|
60
60
|
"@modern-js/module-tools": "^2.54.2",
|
|
61
61
|
"@modern-js/tsconfig": "^2.54.2",
|
|
62
|
-
"@module-federation/manifest": "0.0.0-next-
|
|
62
|
+
"@module-federation/manifest": "0.0.0-next-20240708090519"
|
|
63
63
|
},
|
|
64
64
|
"peerDependencies": {
|
|
65
65
|
"react": ">=17",
|