@modern-js/main-doc 2.60.1 → 2.60.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. package/docs/en/apis/app/commands.mdx +6 -21
  2. package/docs/en/components/deploy-command.mdx +14 -0
  3. package/docs/en/components/serve-command.mdx +22 -0
  4. package/docs/en/configure/app/dev/watch-files.mdx +13 -6
  5. package/docs/en/configure/app/output/copy.mdx +4 -1
  6. package/docs/en/configure/app/output/inline-scripts.mdx +1 -1
  7. package/docs/en/configure/app/output/inline-styles.mdx +1 -1
  8. package/docs/en/configure/app/runtime/router.mdx +11 -3
  9. package/docs/en/configure/app/runtime/state.mdx +0 -2
  10. package/docs/en/guides/concept/_meta.json +1 -1
  11. package/docs/en/guides/concept/server.mdx +35 -0
  12. package/docs/en/guides/topic-detail/_meta.json +6 -0
  13. package/docs/en/guides/topic-detail/module-federation/_meta.json +1 -0
  14. package/docs/en/guides/topic-detail/module-federation/application.mdx +112 -0
  15. package/docs/en/guides/topic-detail/module-federation/deploy.mdx +28 -0
  16. package/docs/en/guides/topic-detail/module-federation/introduce.mdx +35 -0
  17. package/docs/en/guides/topic-detail/module-federation/ssr.mdx +31 -0
  18. package/docs/en/guides/topic-detail/module-federation/usage.mdx +180 -0
  19. package/docs/zh/apis/app/commands.mdx +6 -21
  20. package/docs/zh/components/deploy-command.mdx +14 -0
  21. package/docs/zh/components/serve-command.mdx +22 -0
  22. package/docs/zh/configure/app/dev/watch-files.mdx +13 -6
  23. package/docs/zh/configure/app/output/copy.mdx +4 -2
  24. package/docs/zh/configure/app/output/inline-scripts.mdx +1 -1
  25. package/docs/zh/configure/app/output/inline-styles.mdx +1 -1
  26. package/docs/zh/configure/app/runtime/router.mdx +10 -4
  27. package/docs/zh/configure/app/runtime/state.mdx +0 -2
  28. package/docs/zh/guides/concept/_meta.json +1 -1
  29. package/docs/zh/guides/concept/server.mdx +35 -0
  30. package/docs/zh/guides/topic-detail/_meta.json +6 -0
  31. package/docs/zh/guides/topic-detail/module-federation/_meta.json +1 -0
  32. package/docs/zh/guides/topic-detail/module-federation/application.mdx +112 -0
  33. package/docs/zh/guides/topic-detail/module-federation/deploy.mdx +28 -0
  34. package/docs/zh/guides/topic-detail/module-federation/introduce.mdx +35 -0
  35. package/docs/zh/guides/topic-detail/module-federation/ssr.mdx +31 -0
  36. package/docs/zh/guides/topic-detail/module-federation/usage.mdx +181 -0
  37. package/docs/zh/plugin/introduction.mdx +4 -4
  38. package/i18n.json +4 -0
  39. package/package.json +4 -4
@@ -153,28 +153,9 @@ The `--config` parameter needs to use a JSON string.
153
153
  pnpm does not support the use of JSON strings as parameter values currently. Use `npm new` to turn on.【[Relate Issue](https://github.com/pnpm/pnpm/issues/3876)】
154
154
  :::
155
155
 
156
- ## modern serve
156
+ import ServeCommand from "@site-docs-en/components/serve-command";
157
157
 
158
- The `modern serve` command is used to start a Modern.js project in the production environment. It can also be used to preview the artifacts built for the production environment locally. Please note that you need to execute the [`build`](/apis/app/commands#modern-build) command beforehand to generate the corresponding artifacts.
159
-
160
- ```bash
161
- Usage: modern serve [options]
162
-
163
- Options:
164
- -c --config <config> specify the configuration file, which can be a relative or absolute path
165
- -h, --help show command help
166
- --api-only only run API service
167
- ```
168
-
169
- By default, the project will run in `localhost:8080`, you can modify the server port number with `server.port`:
170
-
171
- ```js
172
- export default defineConfig({
173
- server: {
174
- port: 8081,
175
- },
176
- });
177
- ```
158
+ <ServeCommand />
178
159
 
179
160
  ## modern upgrade
180
161
 
@@ -249,3 +230,7 @@ Inspect config succeed, open following files to view the content:
249
230
  - Webpack Config (web): /root/my-project/dist/webpack.config.web.mjs
250
231
  - Webpack Config (node): /root/my-project/dist/webpack.config.node.mjs
251
232
  ```
233
+
234
+ import DeployCommand from "@site-docs-en/components/deploy-command";
235
+
236
+ <DeployCommand />
@@ -0,0 +1,14 @@
1
+ ## modern deploy
2
+
3
+ The `modern deploy` command is used to generate artifacts required for the deployment platform.
4
+
5
+ ```bash
6
+ Usage: modern deploy [options]
7
+
8
+ Options:
9
+ -c --config <config> Specify configuration file path, either relative or absolute
10
+ -s --skip-build Skip the build stage
11
+ -h, --help Display command help
12
+ ```
13
+
14
+ For more details, refer to [Deploy Application](/guides/basic-features/deploy).
@@ -0,0 +1,22 @@
1
+ ## modern serve
2
+
3
+ The `modern serve` command is used to start a Modern.js project in the production environment. It can also be used to preview the artifacts built for the production environment locally. Please note that you need to execute the [`build`](/apis/app/commands#modern-build) command beforehand to generate the corresponding artifacts.
4
+
5
+ ```bash
6
+ Usage: modern serve [options]
7
+
8
+ Options:
9
+ -c --config <config> specify the configuration file, which can be a relative or absolute path
10
+ -h, --help show command help
11
+ --api-only only run API service
12
+ ```
13
+
14
+ By default, the project will run in `localhost:8080`, you can modify the server port number with `server.port`:
15
+
16
+ ```js
17
+ export default defineConfig({
18
+ server: {
19
+ port: 8081,
20
+ },
21
+ });
22
+ ```
@@ -8,12 +8,19 @@ configName: dev.watchFiles
8
8
  - **Type:**
9
9
 
10
10
  ```ts
11
- type WatchFiles = {
12
- paths: string | string[];
13
- type?: 'reload-page' | 'reload-server';
14
- // watch options for chokidar
15
- options?: WatchOptions;
16
- };
11
+ type WatchFiles =
12
+ | {
13
+ paths: string | string[];
14
+ type?: 'reload-page';
15
+ // watch options for chokidar
16
+ options?: WatchOptions;
17
+ }
18
+ | {
19
+ paths: string | string[];
20
+ type?: 'reload-server';
21
+ };
22
+
23
+ type WatchFilesConfig = WatchFiles | WatchFiles[];
17
24
  ```
18
25
 
19
26
  - **Default:** `undefined`
@@ -1,5 +1,6 @@
1
1
  ---
2
2
  title: copy
3
+ configName: output.copy
3
4
  ---
4
5
 
5
6
  # output.copy
@@ -9,4 +10,6 @@ title: copy
9
10
 
10
11
  Copies the specified file or directory to the dist directory, implemented based on [rspack.CopyRspackPlugin](https://rspack.dev/zh/plugins/rspack/copy-rspack-plugin).
11
12
 
12
- For more detailed configuration, please refer to: [rspack.CopyRspackPlugin](https://rspack.dev/zh/plugins/rspack/copy-rspack-plugin).
13
+ import RsbuildConig from '@site-docs-en/components/rsbuild-config-tooltip';
14
+
15
+ <RsbuildConig />
@@ -26,7 +26,7 @@ type InlineScripts =
26
26
  Whether to inline output scripts files (.js files) into HTML with `<script>` tags.
27
27
 
28
28
  :::tip
29
- When using convention-based routing, you need to set [output.splitRouteChunks](https://modernjs.dev/en/configure/app/output/splitRouteChunks.html) to false if this option is turned on.
29
+ When using convention-based routing, you need to set [output.splitRouteChunks](/configure/app/output/split-route-chunks) to false if this option is turned on.
30
30
  :::
31
31
 
32
32
  import RsbuildConig from '@site-docs-en/components/rsbuild-config-tooltip';
@@ -26,7 +26,7 @@ type InlineStyles =
26
26
  Whether to inline output style files (.css files) into HTML with `<style>` tags.
27
27
 
28
28
  :::tip
29
- When using convention-based routing, you need to set [output.splitRouteChunks](https://modernjs.dev/en/configure/app/output/splitRouteChunks.html) to false if this option is turned on.
29
+ When using convention-based routing, you need to set [output.splitRouteChunks](/configure/app/output/split-route-chunks) to false if this option is turned on.
30
30
  :::
31
31
 
32
32
  import RsbuildConig from '@site-docs-en/components/rsbuild-config-tooltip';
@@ -8,12 +8,16 @@ import RouterLegacyTip from "@site-docs-en/components/router-legacy-tip"
8
8
 
9
9
  <RouterLegacyTip />
10
10
 
11
- # runtime.router
12
-
13
11
  - **Type:** `boolean | Object`
14
12
  - **Default:** `false`
15
13
 
16
- When enabled, the router option provides routing management for Modern.js conventional routes. It is based on [React Router 6](https://reactrouter.com/).
14
+ This value is set to `true` when the project is created, supporting the use of [conventional routing](/guides/concept/entries.html#约定式路由) provided by Modern.js for routing management.
15
+
16
+ If you wish to use [controlled routing](/guides/concept/entries.html#自控式路由), please remove this value or set it to `false`.
17
+
18
+ :::note
19
+ All sub-configurations of `router` will only take effect when using conventional routing.
20
+ :::
17
21
 
18
22
  ## basename
19
23
 
@@ -22,6 +26,10 @@ When enabled, the router option provides routing management for Modern.js conven
22
26
 
23
27
  The basename option specifies the subpath where the app is deployed, for situations where it cannot be deployed to the root domain.
24
28
 
29
+ :::tip
30
+ 推荐使用 [`server.baseUrl`](/configure/app/server/base-url) 进行配置。
31
+ :::
32
+
25
33
  ## supportHtml5History
26
34
 
27
35
  - **Type:** `boolean`
@@ -9,8 +9,6 @@ title: state
9
9
 
10
10
  Once `state` is enabled, you can use [Model](/guides/topic-detail/model/quick-start) for state management.
11
11
 
12
- The specific configuration options are as follows:
13
-
14
12
  ## models
15
13
 
16
14
  - **Type:** `Array<Model>`
@@ -1 +1 @@
1
- ["entries", "builder"]
1
+ ["entries", "builder", "server"]
@@ -0,0 +1,35 @@
1
+ # Web Server
2
+
3
+ Modern.js provides an integrated Web server for applications that can run in any container environment with Node.js. Whether executing the `dev` command in a local development environment, running the `build && serve` commands in a production environment, or using the official deployment solution, it all runs through this Web server to host the application.
4
+
5
+ ## Underlying Dependencies
6
+
7
+ Modern.js builds its Web server based on the [Hono framework](https://hono.dev/). Hono is a small, simple, and ultra-fast web standard-based framework that can run on any JavaScript runtime.
8
+
9
+ ## Development & Production
10
+
11
+ The Web server flow in both Modern.js development and production environments is entirely isomorphic, so you don't need to worry about differences between them.
12
+
13
+ As mentioned in the [Build Tools](/guides/concept/builder) section, Modern.js' underlying build capability is provided by Rsbuild, and some server-side capabilities in the development environment are coupled with the build tools, such as HMR. Modern.js needs to reuse these capabilities of the Rsbuild Dev Server.
14
+
15
+ In the development environment, Modern.js directly uses the middlewares provided by Rsbuild, which includes capabilities needed during the development stage such as HMR and Proxy. Additionally, Modern.js provides capabilities such as Mock, routing, and rendering on top of this:
16
+
17
+ ![Server](https://lf3-static.bytednsdoc.com/obj/eden-cn/nuvjhpqnuvr/modern-website/web-server-modern.jpeg)
18
+
19
+ Therefore, in Modern.js, the development environment merely adds middleware to the production environment. All capabilities of the production environment are also applicable in the development environment, ensuring no fragmentation between the two.
20
+
21
+ :::tip
22
+ Static asset files can be directly hosted by Modern.js' server, but it is highly recommended to upload these files to a CDN in a production environment.
23
+ :::
24
+
25
+ ## Running in CI Environments
26
+
27
+ Modern.js supports running built artifacts in any Node.js environment. Typically, the CI environment has already installed all application dependencies.
28
+
29
+ You can run the [`modern build`](/apis/app/commands#modern-build) command to build the application and the [`modern serve`](/apis/app/commands#modern-serve) command to run the Web server, starting the Modern.js application.
30
+
31
+ ## Running in Production Environments
32
+
33
+ When deploying to production, the artifact size should be as small as possible. The aforementioned method for running in CI environments keeps all artifacts from the original project. Therefore, it is not recommended to run the application using the above commands in a production environment.
34
+
35
+ Modern.js offers a standalone deployment solution. When running the [`modern deploy`](/apis/app/commands#modern-deploy) command, the artifacts will include an entry file for running the Web server.
@@ -1,4 +1,10 @@
1
1
  [
2
+ {
3
+ "type": "dir",
4
+ "name": "module-federation",
5
+ "label": "module-federation",
6
+ "collapsed": true
7
+ },
2
8
  {
3
9
  "type": "dir",
4
10
  "name": "micro-frontend",
@@ -0,0 +1 @@
1
+ ["introduce", "usage", "application", "ssr", "deploy"]
@@ -0,0 +1,112 @@
1
+ # Application-Level Modules
2
+
3
+ Modern.js provides runtime APIs to quickly export application-level Module Federation modules from your application.
4
+
5
+ We use the application created in [Using Module Federation](/guides/topic-detail/module-federation/usage) as an example to further explain how to import application-level modules.
6
+
7
+ ## Install Dependencies
8
+
9
+ Building on the existing application, we need to install the `@module-federation/bridge-react` dependency to use Bridge for loading application-level modules.
10
+
11
+ import { PackageManagerTabs } from '@theme';
12
+
13
+ <PackageManagerTabs command="add @module-federation/bridge-react" />
14
+
15
+ ## Exporting Modules from Producer
16
+
17
+ Unlike directly exporting component-level modules, we need to create a separate entry for application-level modules to be exported via `Module Federation`.
18
+
19
+ We create the `src/export-App.tsx` file:
20
+
21
+ :::note
22
+ The filename can be arbitrary; Modern.js does not enforce a specific naming convention.
23
+ :::
24
+
25
+ ```ts title="src/export-App.tsx"
26
+ import '@modern-js/runtime/registry/main'; // This line must be included, it will import micro frontend runtime dependencies by default
27
+ import { render } from '@modern-js/runtime/browser';
28
+ import { createRoot } from '@modern-js/runtime/react';
29
+ import { createBridgeComponent } from '@module-federation/bridge-react';
30
+
31
+ const ModernRoot = createRoot();
32
+ export const provider = createBridgeComponent({
33
+ rootComponent: ModernRoot,
34
+ render: (Component, dom) => render(Component, dom),
35
+ });
36
+
37
+ export default provider;
38
+ ```
39
+
40
+ This file will pass the root component of the `main` entry application to the Bridge API and render it to the specified node via Bridge's render function.
41
+
42
+ Next, we configure `module-federation.config.ts` to modify the export to `src/export-App.tsx`:
43
+
44
+ ```ts title="module-federation.config.ts"
45
+ import { createModuleFederationConfig } from '@module-federation/modern-js';
46
+
47
+ export default createModuleFederationConfig({
48
+ name: 'remote',
49
+ filename: 'remoteEntry.js',
50
+ exposes: {
51
+ './app': './src/export-App.tsx',
52
+ },
53
+ shared: {
54
+ react: { singleton: true },
55
+ 'react-dom': { singleton: true },
56
+ },
57
+ });
58
+ ```
59
+
60
+ :::info
61
+ [`createBridgeComponent`](https://module-federation.io/zh/practice/bridge/react-bridge.html#createbridgecomponent) is used to export application-level modules. Modern.js related APIs can be found at [createRoot](/apis/app/runtime/core/create-root) and [render](/apis/app/runtime/core/render).
62
+ :::
63
+
64
+ ## Using Modules in Consumer
65
+
66
+ We then modify the consumer configuration by removing the previously created `src/routes/remote/page.tsx` route file.
67
+
68
+ We want all routes that access `/remote` to enter the aforementioned application-level module, so we add `src/routes/remote/$.tsx` instead.
69
+
70
+ :::note
71
+ If you are not familiar with the capabilities of `$.tsx`, please read [Wildcard Routes](/guides/basic-features/routes.html#通配路由).
72
+ :::
73
+
74
+ ```tsx title="src/routes/remote/$.tsx"
75
+ import { createRemoteComponent } from '@module-federation/bridge-react';
76
+ import { loadRemote } from '@module-federation/modern-js/runtime';
77
+
78
+ const ErrorBoundary = (info?: { error: { message: string } }) => {
79
+ return (
80
+ <div>
81
+ <h2>This is ErrorBoundary Component, Something went wrong:</h2>
82
+ <pre style={{ color: 'red' }}>{info?.error.message}</pre>
83
+ </div>
84
+ );
85
+ };
86
+ const Loading = <div>loading...</div>;
87
+ const RemoteApp = createRemoteComponent({
88
+ loader: () => loadRemote('remote/app'),
89
+ fallback: ErrorBoundary,
90
+ loading: Loading,
91
+ });
92
+
93
+ export default RemoteApp;
94
+ ```
95
+
96
+ :::info
97
+ [`createRemoteComponent`](https://module-federation.io/zh/practice/bridge/react-bridge.html#createremotecomponent) is used to load application-level modules.
98
+ :::
99
+
100
+ ## Start the Application
101
+
102
+ Now, both the producer and consumer applications are set up. We can run `modern dev` locally to start both applications.
103
+
104
+ After startup, when the consumer application accesses the `/remote` route, it will enter the producer application. Accessing `http://localhost:8080/remote` will display a complete page of the producer's remote module in the browser.
105
+
106
+ You can create new route files in the producer application and add route navigation in the code. These functionalities will also work as expected.
107
+
108
+ You can refer to the example here: [Modern.js & Module Federation Application-Level Modules](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/module-federation/app-export).
109
+
110
+ ## Related Documentation
111
+
112
+ - [Module Federation Bridge](https://module-federation.io/zh/practice/bridge/index.html)
@@ -0,0 +1,28 @@
1
+ # Deployment
2
+
3
+ Typically, deploying a Module Federation application requires adjusting the remote module address on the consumer side to its online address.
4
+
5
+ For example, if the producer is deployed to the domain `https://my-remote-module`, you can modify the consumer's `module-federation.config.ts` file as follows:
6
+
7
+ ```ts title="module-federation.config.ts"
8
+ import { createModuleFederationConfig } from '@module-federation/modern-js';
9
+
10
+ export default createModuleFederationConfig({
11
+ name: 'host',
12
+ remotes: {
13
+ remote: 'remote@http://my-remote-module/mf-manifest.json',
14
+ },
15
+ shared: {
16
+ react: { singleton: true },
17
+ 'react-dom': { singleton: true },
18
+ },
19
+ });
20
+ ```
21
+
22
+ At this point, the consumer will load the `manifest` configuration file of the `remote` module in the production environment.
23
+
24
+ ## Deployment via Platform
25
+
26
+ The above deployment method is merely the simplest practice. In real-world scenarios, there are many constraints, such as version management, release sequencing, and more. Within ByteDance, we have set up a deployment process for Module Federation applications on our deployment platform, which helps developers address these issues.
27
+
28
+ We will continue to keep an eye on platforms with similar functionalities in the community and, in the future, enhance the documentation for deploying Modern.js + Module Federation on these types of platforms.
@@ -0,0 +1,35 @@
1
+ # Introduction
2
+
3
+ Module Federation is an architectural pattern for dividing JavaScript applications, allowing you to share code and resources among multiple JavaScript applications.
4
+
5
+ In this divided model, it can help improve application performance, enhance code maintainability, and more.
6
+
7
+ ## Module Federation 2.0
8
+
9
+ Module Federation, a highlight feature introduced with Webpack 5, has been around for more than three years. This year, ByteDance, along with the author of Module Federation, [@Zack Jackson](https://github.com/ScriptedAlchemy), and community members jointly launched **Module Federation 2.0**.
10
+
11
+ Module Federation 2.0 is based on internal practices at ByteDance and the existing community ecosystem of Module Federation, addressing many issues in the previous version.
12
+
13
+ Within ByteDance, frameworks based on Modern.js have already deeply integrated with Module Federation 2.0. We are gradually integrating these features into Modern.js and hope to explore the future together with community developers.
14
+
15
+ :::info
16
+ Refer to [Module Federation 2.0 Announcement](https://module-federation.io/zh/blog/announcement.html) for more related content.
17
+ :::
18
+
19
+ ## Modern.js MF Plugin
20
+
21
+ Based on internal practices at ByteDance, the Module Federation team officially provides the [Modern.js Plugin](https://www.npmjs.com/package/@module-federation/modern-js) to help developers use Module Federation more easily.
22
+
23
+ The plugin recognizes the current build engine (Webpack or Rspack), injects the corresponding Module Federation plugin into Modern.js applications, and automatically handles build configurations and adds runtime code.
24
+
25
+ Moreover, the plugin also supports the use of Module Federation in Modern.js SSR applications, providing a better performance experience.
26
+
27
+ For more details, refer to [Using Module Federation](/guides/topic-detail/module-federation/usage) and [Module Federation Server-Side Rendering](/guides/topic-detail/module-federation/ssr).
28
+
29
+ ## Application-Level Modules
30
+
31
+ **Application-level modules** possess the application's framework rendering capabilities and routing capabilities, allowing them to operate like applications. Application-level modules are a crucial capability in **micro-frontend frameworks**, providing the ability to load and render across application frameworks (React, Vue) and supporting the loading of modules with routing.
32
+
33
+ Module Federation 2.0 offers the [Bridge](https://module-federation.io/zh/practice/bridge/index.html) capability to load application-level modules.
34
+
35
+ Modern.js, based on Bridge and its internal implementation, provides APIs to easily export application-level modules. For more details, refer to [Application-Level Modules](/guides/topic-detail/module-federation/application).
@@ -0,0 +1,31 @@
1
+ # Server-Side Rendering
2
+
3
+ `@module-federation/modern-js` offers powerful capabilities, enabling developers to easily combine Module Federation with server-side rendering (SSR) in Modern.js applications.
4
+
5
+ ## Enable SSR
6
+
7
+ Using the application created in [Using Module Federation](/guides/topic-detail/module-federation/usage) as an example, you only need to add the `server.ssr` configuration to both the producer and the consumer:
8
+
9
+ ```ts title="modern.config.ts"
10
+ import { appTools, defineConfig } from '@modern-js/app-tools';
11
+
12
+ export default defineConfig({
13
+ server: {
14
+ ssr: {
15
+ mode: 'stream',
16
+ },
17
+ },
18
+ });
19
+ ```
20
+
21
+ For better performance, we only support using this capability combination in Streaming SSR scenarios.
22
+
23
+ :::warning
24
+ Currently, `@module-federation/bridge-react` is not compatible with the Node environment. You must remove it from the dependencies to use Module Federation and server-side rendering correctly. This means Bridge cannot work with server-side rendering.
25
+ :::
26
+
27
+ ## Data Fetching
28
+
29
+ :::note
30
+ Stay tuned
31
+ :::
@@ -0,0 +1,180 @@
1
+ # Getting Started
2
+
3
+ To use Module Federation in Modern.js, we recommend using the official plugin `@module-federation/modern-js`.
4
+
5
+ This section will introduce how to set up both producer and consumer applications using the official plugin. First, create two applications by following the [Modern.js Quick Start](/guides/get-started/quick-start).
6
+
7
+ ## Install the Plugin
8
+
9
+ After creating the applications, install the plugin for both projects:
10
+
11
+ import { PackageManagerTabs } from '@theme';
12
+
13
+ <PackageManagerTabs command="add @module-federation/modern-js" />
14
+
15
+ ## Register the Plugin
16
+
17
+ After installing the plugin, you need to register it in the `modern.config.js` file:
18
+
19
+ ```ts
20
+ import { appTools, defineConfig } from '@modern-js/app-tools';
21
+ import { moduleFederationPlugin } from '@module-federation/modern-js';
22
+
23
+ export default defineConfig({
24
+ runtime: {
25
+ router: true,
26
+ },
27
+ plugins: [
28
+ appTools({
29
+ bundler: 'rspack',
30
+ }),
31
+ moduleFederationPlugin(),
32
+ ],
33
+ });
34
+ ```
35
+
36
+ ## Export Modules from Producer
37
+
38
+ Next, modify the producer's code to export the Module Federation module.
39
+
40
+ Create the `src/components/Button.tsx` file and export a Button component:
41
+
42
+ ```tsx title="src/components/Button.tsx"
43
+ import React from 'react';
44
+
45
+ export const Button = () => {
46
+ return <button type="button">Remote Button</button>;
47
+ };
48
+ ```
49
+
50
+ Then, add the `module-federation.config.ts` file at the project root to configure the Module Federation module's name, shared dependencies, and exports:
51
+
52
+ ```ts title="module-federation.config.ts"
53
+ import { createModuleFederationConfig } from '@module-federation/modern-js';
54
+
55
+ export default createModuleFederationConfig({
56
+ name: 'remote',
57
+ filename: 'remoteEntry.js',
58
+ exposes: {
59
+ './Button': './src/components/Button.tsx',
60
+ },
61
+ shared: {
62
+ react: { singleton: true },
63
+ 'react-dom': { singleton: true },
64
+ },
65
+ });
66
+ ```
67
+
68
+ Additionally, modify `modern.config.ts` to provide a development environment port for the producer, allowing the consumer to access the producer's resources through this port:
69
+
70
+ ```ts title="modern.config.ts"
71
+ import { appTools, defineConfig } from '@modern-js/app-tools';
72
+ import { moduleFederationPlugin } from '@module-federation/modern-js';
73
+
74
+ export default defineConfig({
75
+ dev: {
76
+ port: 3051,
77
+ },
78
+ runtime: {
79
+ router: true,
80
+ },
81
+ plugins: [
82
+ appTools({
83
+ bundler: 'rspack',
84
+ }),
85
+ moduleFederationPlugin(),
86
+ ],
87
+ });
88
+ ```
89
+
90
+ ## Use Modules in Consumer
91
+
92
+ Now, modify the consumer's code to use the module exported by the producer.
93
+
94
+ Add the `module-federation.config.ts` file at the project root to configure the Module Federation module's name, shared dependencies, and the remote module to use:
95
+
96
+ ```ts title="module-federation.config.ts"
97
+ import { createModuleFederationConfig } from '@module-federation/modern-js';
98
+
99
+ export default createModuleFederationConfig({
100
+ name: 'host',
101
+ remotes: {
102
+ remote: 'remote@http://localhost:3051/mf-manifest.json',
103
+ },
104
+ shared: {
105
+ react: { singleton: true },
106
+ 'react-dom': { singleton: true },
107
+ },
108
+ });
109
+ ```
110
+
111
+ `mf-manifest.json` is the file produced by the producer after packaging, containing all the information about the modules exported by the producer.
112
+
113
+ Create a new route file `src/routes/remote/page.tsx` and import the producer module:
114
+
115
+ ```tsx title="src/routes/remote/page.tsx"
116
+ import React, { useState, Suspense } from 'react';
117
+ import { Button } from 'remote/Button';
118
+
119
+ const Index = (): JSX.Element => {
120
+ return (
121
+ <div>
122
+ <Suspense fallback={<div>Loading...</div>}>
123
+ <Button />
124
+ </Suspense>
125
+ </div>
126
+ );
127
+ };
128
+
129
+ export default Index;
130
+ ```
131
+
132
+ At this point, importing `remote/Button` will result in a type error because the local environment doesn't have the type for the remote module. Module Federation 2.0 provides [type hints](https://module-federation.io/zh/guide/basic/type-prompt.html), which will automatically generate type definitions for remote modules during the producer's build and download them during the consumer's build.
133
+
134
+ To ensure the types take effect, add a new `path` in `tsconfig.json`:
135
+
136
+ ```json title="tsconfig.json"
137
+ {
138
+ "compilerOptions": {
139
+ "paths": {
140
+ "*": ["./@mf-types/*"]
141
+ }
142
+ }
143
+ }
144
+ ```
145
+
146
+ :::tip
147
+ In the consumer, we reference the remote module using `remote/Button`. Here's a brief explanation of what this path specifically represents. You can abstract it as `[remoteAlias]/[remoteExpose]`.
148
+
149
+ The first part, `remoteAlias`, is the alias of the producer in the consumer. It is the `key` configured in the `remotes` field of the consumer's `module-federation.config.ts`:
150
+
151
+ ```ts
152
+ {
153
+ remotes: {
154
+ [remoteAlias]: '[remoteModuleName]@[URL_ADDRESS]',
155
+ }
156
+ }
157
+ ```
158
+
159
+ Here, we also abstract the remote address as `[remoteModuleName]@[URL_ADDRESS]`. The part before `@` must correspond to the module name of the producer.
160
+
161
+ The second part, `remoteExpose`, is the `key` configured in the `exposes` field of the producer's `module-federation.config.ts`.
162
+ :::
163
+
164
+ ## Start the Applications
165
+
166
+ Now, both the producer and consumer applications are set up. You can run `modern dev` locally to start both applications.
167
+
168
+ Once started, the imports of the producer's modules in the consumer will no longer throw errors, and the types will be downloaded to the consumer application.
169
+
170
+ :::note
171
+ After modifying the producer's code, the consumer will automatically fetch the producer's types.
172
+ :::
173
+
174
+ Access `http://localhost:8080/remote`, and you will see that the page includes the `Button` component from the producer's remote module.
175
+
176
+ You can refer to this example: [Modern.js & Module Federation Basic Example](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/module-federation/base).
177
+
178
+ ## Related Documentation
179
+
180
+ - [Module Federation Official Documentation](https://module-federation.io/zh/guide/framework/modernjs.html)
@@ -153,28 +153,9 @@ pnpm 暂不支持使用 JSON 字符串作为参数值,可使用 `npm new` 开
153
153
 
154
154
  :::
155
155
 
156
- ## modern serve
156
+ import ServeCommand from "@site-docs/components/serve-command";
157
157
 
158
- `modern serve` 命令用于在生产环境下启动 Modern.js 工程, 也可以用于在本地预览生产环境构建的产物。注意你需要提前执行 [`build`](/apis/app/commands#modern-build) 命令构建出对应产物。
159
-
160
- ```bash
161
- Usage: modern serve [options]
162
-
163
- Options:
164
- -c --config <config> 指定配置文件路径,可以为相对路径或绝对路径
165
- -h, --help 显示命令帮助
166
- --api-only 仅启动 API 接口服务
167
- ```
168
-
169
- 默认情况下,应用将会在 `localhost:8080` 启动,可以通过 `server.port` 修改 Server 端口号:
170
-
171
- ```js
172
- export default defineConfig({
173
- server: {
174
- port: 8081,
175
- },
176
- });
177
- ```
158
+ <ServeCommand />
178
159
 
179
160
  ## modern upgrade
180
161
 
@@ -249,3 +230,7 @@ Inspect config succeed, open following files to view the content:
249
230
  - Webpack Config (web): /root/my-project/dist/webpack.config.web.mjs
250
231
  - Webpack Config (node): /root/my-project/dist/webpack.config.node.mjs
251
232
  ```
233
+
234
+ import DeployCommand from "@site-docs/components/deploy-command";
235
+
236
+ <DeployCommand />