Skip to content

Commit 4ac0fbc

Browse files
authored
docs(website-new): add nx guides (#3092)
1 parent cf14509 commit 4ac0fbc

File tree

6 files changed

+354
-1
lines changed

6 files changed

+354
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
{
4545
"text": "Zephyr Cloud",
4646
"link": "https://zephyr-cloud.io/"
47+
},
48+
{
49+
"text": "Nx",
50+
"link": "https://nx.dev/"
4751
}
4852
]
4953
}

apps/website-new/docs/en/guide/start/quick-start.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import { Badge } from "@theme";
2+
13
# Quick Start Guide
24

35
Before reading this guide, please first read the [Setting Up Environment](./setting-up-env). This guide will lead you step by step to learn how to use Module Federation. We will build two independent Single Page Applications (SPAs) that will share components using Module Federation. If you encounter unfamiliar terms in the following text, please refer to the [Glossary](./glossary).
46

7+
_<Badge text="Note:" type="info" /> <small>You can also use [Nx](https://nx.dev) to rapidly scaffold Module Federation projects for [React](../../practice/frameworks/react/using-nx-for-react) and [Angular](../../practice/frameworks/angular/using-nx-for-angular).</small>_
8+
59
## Creating a Producer
610

711
### 1. Initializing the Project

apps/website-new/docs/en/practice/frameworks/angular/_meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[
22
"angular-cli",
3+
"using-nx-for-angular",
34
"angular-mfe",
45
"mf-ssr-angular",
56
"service-workers-mf",
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import { Steps, Badge } from '@theme';
2+
3+
# Using Nx CLI for Angular
4+
5+
This guide explains how to integrate Module Federation for Angular projects using the [Nx CLI](https://nx.dev).
6+
7+
## Installation
8+
9+
To start, create a new Nx workspace ready for application development.
10+
11+
```bash
12+
npx -y create-nx-workspace@latest myorg --preset=apps
13+
cd myorg
14+
```
15+
16+
Next, we want to add the `@nx/angular` plugin to provide Angular capabilities to our Nx workspace.
17+
18+
```bash
19+
npx nx add @nx/angular
20+
```
21+
22+
## Generate Shell (Host) with Remotes
23+
24+
The Shell (Host) is crucial for Module Federation integration. Nx offers [generators](https://nx.dev/features/generate-code#generate-code) to simplify the process of setting up an application with Module Federation enabled.
25+
These are the [`host`](https://nx.dev/nx-api/angular/generators/host) and [`remote`](https://nx.dev/nx-api/angular/generators/remote) generators.
26+
The `host` generator actually allows you to specify a `--remotes` option, wherein you can generate remotes that you know you'll need all in one command.
27+
28+
_<Badge text="Note:" type="info" /> <small>Nx currently uses `@module-federation/enhanced/webpack` to provide Module Federation capabilities using Webpack for Angular.</small>_
29+
30+
<Steps>
31+
### Scaffold the Shell (Host) application
32+
33+
Let's create a shell called `shop` in the `apps/` directory with remotes called `products`, `cart` and `checkout`.
34+
35+
```bash
36+
npx nx g @nx/angular:host apps/shop --remotes=products,cart,checkout
37+
```
38+
39+
You'll notice four applications have been generated in the `apps/` directory.
40+
At this point, everything is generated and configured correctly.
41+
42+
_<Badge text="Note:" type="info" /> <small>Nx will configure the routing between the shell and the remote applications, as well as attaching each remote application to the shell in the `module-federation.config.ts` file to inform Module Federation of the remotes.</small>_
43+
44+
### Serving your Module Federation setup
45+
46+
Nx employs several techniques when working with Module Federation to ensure a great developer experience (DX), performance and scalability.
47+
Once such technique involves only serving remotes that you're actively working with file watching attached. In Nx, these are called `dev remotes`.
48+
The other projects are built by Nx and served statically via `http-server`. These are called `static remotes`.
49+
50+
_<Badge text="Note:" type="info" /> <small>A bonus benefit of using Nx with Static Remotes comes from [Nx Caching](https://nx.dev/features/cache-task-results). Any remote that you have not changed, will be restored from cache and served, reducing compute overhead.</small>_
51+
52+
To serve your shell with all remotes treated as Static Remotes, run:
53+
54+
```bash
55+
npx nx serve shop
56+
```
57+
58+
You'll now be able to access `shop` on `localhost:4200` and you can navigate between the remote applications using the links at the top of the page.
59+
60+
When you're actively working on a remote application (a Dev Remote), you can pass the `--devRemotes` flag to inform Nx to use `webpack-dev-server` to serve the remote application, allowing for HMR and file watching.
61+
62+
```bash
63+
npx nx serve shop --devRemotes=products
64+
```
65+
66+
Now, if you make changes to your `products` application, they will be reflected in the shell application in the browser.
67+
68+
_<Badge text="Note:" type="info" /> <small>To learn more about why Nx recommends serving everything, read [here](https://nx.dev/concepts/module-federation/module-federation-and-nx#develop-as-a-user).</small>_
69+
70+
### Building your Module Federation setup
71+
72+
Nx cares a lot about performance and DX, especially in monorepos. It offers you a command that will let you build everything at once. Helpful for full redeployments of all projects in the Module Federation system.
73+
74+
```bash
75+
npx nx run-many -t build
76+
```
77+
78+
When running this command, if Nx detects a project did not change, it will restore the cached build artifact for that project instead of rebuilding it. However, sometimes this is still overkill.
79+
Therefore, to only rebuild the projects that have actually been changed, and cut down any additional time spent restoring cached build artifacts, Nx offers a command to only recompute affected projects.
80+
81+
```bash
82+
npx nx affected -t build
83+
```
84+
85+
You can view the build artifacts at `dist/apps`.
86+
87+
</Steps>
88+
89+
## Adding Additional Remotes
90+
91+
As we develop our application, we often need to add more remote applications. This is when Nx's `remote` generator becomes extremely useful.
92+
Not only will it scaffold an Angular project with Module Federation configured, it also allows us to tell it which `host` application it belongs to and it will update the `host` to configure the new remote.
93+
94+
<Steps>
95+
### Scaffold new Remote application
96+
97+
Let's add a new remote called `login` and attach it to our `shop` shell.
98+
99+
```bash
100+
npx nx g @nx/angular:remote apps/login --host=shop
101+
```
102+
103+
It's that simple. If we run `npx nx serve shop --devRemotes`, we can now continue development on the `login` remote and see it reflected in the browser.
104+
</Steps>
105+
106+
## Understanding Nx's Module Federation Abstractions
107+
108+
Nx aims to make working with Module Federation as simplistic as possible while also providing a mechanism wherein the Nx team can ship optimizations, new features and fixes as seamlessly as possible.
109+
To achieve this, it uses some abstractions over the underlying `ModuleFederationPlugin`.
110+
111+
### Nx's ModuleFederationConfig
112+
113+
Nx provides it's own `ModuleFederationConfig` interface which is a streamlined version of what is needed for the `ModuleFederationPlugin`.
114+
You can view it in-depth [here](https://github.com/nrwl/nx/blob/master/packages/webpack/src/utils/module-federation/models/index.ts#L41).
115+
116+
Let's focus on the most important aspects
117+
118+
<Steps>
119+
#### Remotes
120+
121+
The `remotes` option gives you two options for defining remote applications used by the shell (host).
122+
123+
You can provide an array of Nx project names, or a tuple, defining the Nx project name, and the entry location for the remote.
124+
125+
```js
126+
remotes: ["products", ["cart", "http://my-live-cart.myapp.com/mf-manifest.json"]]
127+
```
128+
129+
Under the hood, Nx will parse it's [Project Graph](https://nx.dev/features/explore-graph) to discover information around the entry location of the remote project and configure the `ModuleFederationPlugin` accordingly.
130+
This is also used to serve the Module Federation setup when you run `npx nx serve shop`.
131+
132+
#### Exposes
133+
134+
The `exposes` option matches the exposes option in the `ModuleFederationPlugin` and provides you the ability to specify which modules should be federated by a remote application.
135+
136+
#### Shared
137+
138+
Nx will use its project graph to determine all the dependencies (npm and local) that are used by the projects in the Module Federation setup. It will then share all these dependencies at the detected version as singletons. This is great for getting up and running quickly, but sometimes, you need to control this behaviour.
139+
The `shared` option allows you to pass a function where you can write custom logic to determine how a dependency should be shared.
140+
141+
```js
142+
// do not share lodash to allow better tree-shaking
143+
shared: function(libraryName, shareConfig) {
144+
if(libraryName === 'lodash') {
145+
return false
146+
}
147+
}
148+
```
149+
</Steps>
150+
151+
### Nx's withModuleFederation
152+
153+
The `withModuleFederation` helper resides in the `webpack.config.ts` file.
154+
It's primary purpose is to set up the `ModuleFederationPlugin` based on the `config` provided by `module-federation.config.ts`
155+
156+
However, it also allows you to further configure other properties of `ModuleFederationConfig`.
157+
158+
```js
159+
withModuleFederation(config, {
160+
dts: false,
161+
runtimePlugins: []
162+
});
163+
```
164+
165+
## Learn More
166+
167+
The links below are useful to learn more about Nx's support for Module Federation.
168+
169+
- [Module Federation and Nx](https://nx.dev/concepts/module-federation/module-federation-and-nx)
170+
- [Micro Frontend Architecture](https://nx.dev/concepts/module-federation/micro-frontend-architecture)
171+
- [Nx Module Federation Technical Overview](https://nx.dev/concepts/module-federation/nx-module-federation-technical-overview)
172+
- [Nx Module Federation Recipes](https://nx.dev/recipes/module-federation)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
["index", "i18n-react"]
1+
["index", "using-nx-for-react", "i18n-react"]
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import { Steps, Badge } from '@theme';
2+
3+
# Using Nx CLI for React
4+
5+
This guide explains how to integrate Module Federation for React projects using the [Nx CLI](https://nx.dev).
6+
7+
## Installation
8+
9+
To start, create a new Nx workspace ready for application development.
10+
11+
```bash
12+
npx -y create-nx-workspace@latest myorg --preset=apps
13+
cd myorg
14+
```
15+
16+
Next, we want to add the `@nx/react` plugin to provide React capabilities to our Nx workspace.
17+
18+
```bash
19+
npx nx add @nx/react
20+
```
21+
22+
## Generate Shell (Host) with Remotes
23+
24+
The Shell (Host) is crucial for Module Federation integration. Nx offers [generators](https://nx.dev/features/generate-code#generate-code) to simplify the process of setting up an application with Module Federation enabled.
25+
These are the [`host`](https://nx.dev/nx-api/react/generators/host) and [`remote`](https://nx.dev/nx-api/react/generators/remote) generators.
26+
The `host` generator actually allows you to specify a `--remotes` option, wherein you can generate remotes that you know you'll need all in one command.
27+
28+
_<Badge text="Note:" type="info" /> <small>Nx currently uses `@module-federation/enhanced/rspack` to provide Module Federation capabilities using Rspack for React.</small>_
29+
30+
<Steps>
31+
### Scaffold the Shell (Host) application
32+
33+
Let's create a shell called `shop` in the `apps/` directory with remotes called `products`, `cart` and `checkout`.
34+
35+
```bash
36+
npx nx g @nx/react:host apps/shop --remotes=products,cart,checkout
37+
```
38+
39+
You'll notice four applications have been generated in the `apps/` directory.
40+
At this point, everything is generated and configured correctly.
41+
42+
_<Badge text="Note:" type="info" /> <small>Nx will configure the routing between the shell and the remote applications, as well as attaching each remote application to the shell in the `module-federation.config.ts` file to inform Module Federation of the remotes.</small>_
43+
44+
### Serving your Module Federation setup
45+
46+
Nx employs several techniques when working with Module Federation to ensure a great developer experience (DX), performance and scalability.
47+
Once such technique involves only serving remotes that you're actively working with file watching attached. In Nx, these are called `dev remotes`.
48+
The other projects are built by Nx and served statically via `http-server`. These are called `static remotes`.
49+
50+
_<Badge text="Note:" type="info" /> <small>A bonus benefit of using Nx with Static Remotes comes from [Nx Caching](https://nx.dev/features/cache-task-results). Any remote that you have not changed, will be restored from cache and served, reducing compute overhead.</small>_
51+
52+
To serve your shell with all remotes treated as Static Remotes, run:
53+
54+
```bash
55+
npx nx serve shop
56+
```
57+
58+
You'll now be able to access `shop` on `localhost:4200` and you can navigate between the remote applications using the links at the top of the page.
59+
60+
When you're actively working on a remote application (a Dev Remote), you can pass the `--devRemotes` flag to inform Nx to use `@rspack/dev-server` to serve the remote application, allowing for HMR and file watching.
61+
62+
```bash
63+
npx nx serve shop --devRemotes=products
64+
```
65+
66+
Now, if you make changes to your `products` application, they will be reflected in the shell application in the browser.
67+
68+
_<Badge text="Note:" type="info" /> <small>To learn more about why Nx recommends serving everything, read [here](https://nx.dev/concepts/module-federation/module-federation-and-nx#develop-as-a-user).</small>_
69+
70+
### Building your Module Federation setup
71+
72+
Nx cares a lot about performance and DX, especially in monorepos. It offers you a command that will let you build everything at once. Helpful for full redeployments of all projects in the Module Federation system.
73+
74+
```bash
75+
npx nx run-many -t build
76+
```
77+
78+
When running this command, if Nx detects a project did not change, it will restore the cached build artifact for that project instead of rebuilding it. However, sometimes this is still overkill.
79+
Therefore, to only rebuild the projects that have actually been changed, and cut down any additional time spent restoring cached build artifacts, Nx offers a command to only recompute affected projects.
80+
81+
```bash
82+
npx nx affected -t build
83+
```
84+
85+
You can view the build artifacts at `dist/apps`.
86+
87+
</Steps>
88+
89+
## Adding Additional Remotes
90+
91+
As we develop our application, we often need to add more remote applications. This is when Nx's `remote` generator becomes extremely useful.
92+
Not only will it scaffold an Angular project with Module Federation configured, it also allows us to tell it which `host` application it belongs to and it will update the `host` to configure the new remote.
93+
94+
<Steps>
95+
### Scaffold new Remote application
96+
97+
Let's add a new remote called `login` and attach it to our `shop` shell.
98+
99+
```bash
100+
npx nx g @nx/react:remote apps/login --host=shop
101+
```
102+
103+
It's that simple. If we run `npx nx serve shop --devRemotes`, we can now continue development on the `login` remote and see it reflected in the browser.
104+
</Steps>
105+
106+
## Understanding Nx's Module Federation Abstractions
107+
108+
Nx aims to make working with Module Federation as simplistic as possible while also providing a mechanism wherein the Nx team can ship optimizations, new features and fixes as seamlessly as possible.
109+
To achieve this, it uses some abstractions over the underlying `ModuleFederationPlugin`.
110+
111+
### Nx's ModuleFederationConfig
112+
113+
Nx provides it's own `ModuleFederationConfig` interface which is a streamlined version of what is needed for the `ModuleFederationPlugin`.
114+
You can view it in-depth [here](https://github.com/nrwl/nx/blob/master/packages/webpack/src/utils/module-federation/models/index.ts#L41).
115+
116+
Let's focus on the most important aspects
117+
118+
<Steps>
119+
#### Remotes
120+
121+
The `remotes` option gives you two options for defining remote applications used by the shell (host).
122+
123+
You can provide an array of Nx project names, or a tuple, defining the Nx project name, and the entry location for the remote.
124+
125+
```js
126+
remotes: ["products", ["cart", "http://my-live-cart.myapp.com/mf-manifest.json"]]
127+
```
128+
129+
Under the hood, Nx will parse it's [Project Graph](https://nx.dev/features/explore-graph) to discover information around the entry location of the remote project and configure the `ModuleFederationPlugin` accordingly.
130+
This is also used to serve the Module Federation setup when you run `npx nx serve shop`.
131+
132+
#### Exposes
133+
134+
The `exposes` option matches the exposes option in the `ModuleFederationPlugin` and provides you the ability to specify which modules should be federated by a remote application.
135+
136+
#### Shared
137+
138+
Nx will use its project graph to determine all the dependencies (npm and local) that are used by the projects in the Module Federation setup. It will then share all these dependencies at the detected version as singletons. This is great for getting up and running quickly, but sometimes, you need to control this behaviour.
139+
The `shared` option allows you to pass a function where you can write custom logic to determine how a dependency should be shared.
140+
141+
```js
142+
// do not share lodash to allow better tree-shaking
143+
shared: function(libraryName, shareConfig) {
144+
if(libraryName === 'lodash') {
145+
return false
146+
}
147+
}
148+
```
149+
</Steps>
150+
151+
### Nx's withModuleFederation
152+
153+
The `withModuleFederation` helper resides in the `rspack.config.ts` file.
154+
It's primary purpose is to set up the `ModuleFederationPlugin` based on the `config` provided by `module-federation.config.ts`
155+
156+
However, it also allows you to further configure other properties of `ModuleFederationConfig`.
157+
158+
```js
159+
withModuleFederation(config, {
160+
dts: false,
161+
runtimePlugins: []
162+
});
163+
```
164+
165+
## Learn More
166+
167+
The links below are useful to learn more about Nx's support for Module Federation.
168+
169+
- [Module Federation and Nx](https://nx.dev/concepts/module-federation/module-federation-and-nx)
170+
- [Micro Frontend Architecture](https://nx.dev/concepts/module-federation/micro-frontend-architecture)
171+
- [Nx Module Federation Technical Overview](https://nx.dev/concepts/module-federation/nx-module-federation-technical-overview)
172+
- [Nx Module Federation Recipes](https://nx.dev/recipes/module-federation)

0 commit comments

Comments
 (0)