Skip to content

Commit f302eeb

Browse files
2heal1Nsttt
andauthored
feat: add Create a new Module Federation project cli (#3446)
Co-authored-by: Néstor <[email protected]>
1 parent eecee74 commit f302eeb

File tree

81 files changed

+2939
-899
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+2939
-899
lines changed

.changeset/beige-otters-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-module-federation': minor
3+
---
4+
5+
feat: add Create a new Module Federation project cli

.changeset/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"@module-federation/rsbuild-plugin",
2626
"@module-federation/error-codes",
2727
"@module-federation/inject-external-runtime-core-plugin",
28-
"@module-federation/runtime-core"
28+
"@module-federation/runtime-core",
29+
"create-module-federation"
2930
]
3031
],
3132
"ignorePatterns": ["^alpha|^beta"],

.changeset/dirty-rivers-sing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/rsbuild-plugin': patch
3+
---
4+
5+
chore(rsbuild-plugin): set 'loaded-first' as default shareStrategy

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
node_modules/
22
vite.config.ts
3-
**/dist/**
3+
**/dist/**

.github/workflows/e2e-modern-ssr.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ jobs:
4848
lsof -ti tcp:3050,3051,3052,3053,3054,3055,3056 | xargs -r kill &&
4949
pnpm run app:modern:dev &
5050
sleep 30 &&
51-
npx wait-on http://127.0.0.1:3050/mf-manifest.json &&
5251
npx wait-on http://127.0.0.1:3051/mf-manifest.json &&
53-
npx wait-on http://127.0.0.1:3052/mf-manifest.json &&
5452
npx wait-on http://127.0.0.1:3053/mf-manifest.json &&
55-
npx wait-on http://127.0.0.1:3054/mf-manifest.json &&
5653
npx wait-on http://127.0.0.1:3055/mf-manifest.json &&
5754
npx wait-on http://127.0.0.1:3056/mf-manifest.json &&
55+
npx wait-on http://127.0.0.1:3054/mf-manifest.json &&
56+
npx wait-on http://127.0.0.1:3052/mf-manifest.json &&
57+
npx wait-on http://127.0.0.1:3050/mf-manifest.json &&
5858
npx nx run modernjs-ssr-host:e2e &&
5959
lsof -ti tcp:3050,3051,3052,3053,3054,3055,3056 | xargs kill

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ apps/website-new/docs
1111

1212
/.nx/workspace-data
1313
/.nx/cache
14+
15+
**/*.handlebars

apps/rslib-module/rslib.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ export default defineConfig({
3636
root: './dist/mf',
3737
},
3838
},
39-
server: {
40-
port: 3001,
41-
},
4239
},
4340
],
41+
server: {
42+
port: 3001,
43+
},
4444
plugins: [
4545
pluginReact(),
4646
pluginModuleFederation({

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

Lines changed: 168 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,174 @@
1-
import { Badge } from "@theme";
1+
import { Badge, PackageManagerTabs } from "@theme";
22

33
# Quick Start Guide
44

55
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).
66

77
_<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>_
88

9-
## Creating a Producer
9+
## Creating a New Project
1010

11-
### 1. Initializing the Project
11+
Module Federation provides the `create-module-federation` tool to create projects. You don't need to install it globally; you can directly use `npx` to run it on-demand.
1212

13-
Use [Rsbuild](https://rsbuild.dev/) to create a producer and call the following command:
13+
You can use `create-module-federation` to create a Module Federation project by running the following command:
1414

15+
<PackageManagerTabs
16+
command={{
17+
npm: 'npm create module-federation@latest',
18+
yarn: 'yarn create module-federation',
19+
pnpm: 'pnpm create module-federation@latest',
20+
}}
21+
/>
22+
23+
Just follow the prompts step by step. During the creation process, you can select the project type, role type, etc.
24+
25+
### Templates
26+
27+
When creating a project, you can choose from the following templates provided by `create-module-federation`:
28+
29+
| Template | Description |
30+
| --------------------- | ------------------------------------------------------------ |
31+
| provider-modern | A provider using [Modern.js](https://modernjs.dev/) |
32+
| provider-rsbuild | A provider using [Rsbuild](https://rsbuild.dev/) |
33+
| provider-rslib | A provider using [Rslib](https://lib.rsbuild.dev/) |
34+
| provider-rslib-storybook | A provider using [Rslib](https://lib.rsbuild.dev/) with the storybook feature enabled |
35+
| consumer-modern | A consumer using [Modern.js](https://modernjs.dev/) |
36+
| consumer-rsbuild | A consumer using [Rsbuild](https://rsbuild.dev/) |
37+
38+
### Quick Creation
39+
40+
[create-module-federation](https://www.npmjs.com/package/create-module-federation) provides some CLI options. By setting these CLI options, you can skip the interactive selection steps and create a project with one click.
41+
42+
For example, to create a Modern.js provider project named `provider` in the `my-project` directory with one click:
43+
44+
```bash
45+
npx create-module-federation --dir my-project --template provider-modern --name provider
46+
47+
# Use abbreviations
48+
npx create-module-federation -d my-project -t provider-modern -n provider
49+
```
50+
51+
The complete CLI options for `create-module-federation` are as follows:
52+
53+
```text
54+
Usage: create-module-federation [options]
55+
56+
Options:
57+
58+
-h, --help display help for command
59+
-d, --dir create project in specified directory
60+
-t, --template specify the template to use
61+
-n, --name specify the mf name
62+
-r, --role specify the mf role type: provider or consumer
63+
--override override files in target directory
64+
65+
Templates:
66+
67+
provider-modern, provider-rsbuild, provider-rslib, provider-rslib-storybook, consumer-modern, consumer-rsbuild
68+
```
69+
70+
### Create a provider
71+
72+
Execute the `create-module-federation` command, select the required framework and type according to your needs, and select the role type as provider to create a project.
73+
74+
Here we take creating an rsbuild provider project as an example:
75+
76+
```bash
77+
~ ✗ npm create module-federation@latest
78+
79+
> npx
80+
> create-module-federation
81+
82+
◆ Create Module Federation Project
83+
84+
◇ Please input Module Federation name:
85+
│ mf_provider
86+
87+
◇ Please select the type of project you want to create:
88+
│ Application
89+
90+
◇ Select template
91+
│ Rsbuild
92+
93+
◇ Please select the role of project you want to create:
94+
│ Provider
95+
96+
◇ Next steps ─────╮
97+
│ │
98+
cd mf_provider │
99+
│ npm install │
100+
│ npm run dev │
101+
│ │
102+
├──────────────────╯
103+
104+
└ Done.
105+
```
106+
107+
### Create a consumer
108+
109+
Execute the `create-module-federation` command, select the required framework and type according to your needs, and select the role type as consumer to create a project.
110+
111+
Here is an example of creating an rsbuild consumer project:
112+
113+
```bash
114+
~ ✗ npm create module-federation@latest
115+
116+
> npx
117+
> create-module-federation
118+
119+
◆ Create Module Federation Project
120+
121+
◇ Please input Module Federation name:
122+
│ mf_consumer
123+
124+
◇ Please select the type of project you want to create:
125+
│ Application
126+
127+
◇ Select template
128+
│ Rsbuild
129+
130+
◇ Please select the role of project you want to create:
131+
│ Consumer
132+
133+
◇ Next steps ─────╮
134+
│ │
135+
cd mf_provider │
136+
│ npm install │
137+
│ npm run dev │
138+
│ │
139+
├──────────────────╯
140+
141+
└ Done.
142+
```
143+
144+
#### Replace provider
145+
146+
The default consumer project created will consume a published provider. If you want to replace the provider, you need to modify the `remotes` configuration in `module-federation.config.ts`.
147+
148+
```diff title="module-federation.config.ts"
149+
import { createModuleFederationConfig } from '@module-federation/rsbuild-plugin';
150+
151+
export default createModuleFederationConfig({
152+
name: 'mf_consumer',
153+
remotes: {
154+
- 'provider': 'rslib_provider@https://unpkg.com/module-federation-rslib-provider@latest/dist/mf/mf-manifest.json',
155+
+ 'provider': 'rslib_provider@http://localhost:3000/mf-manifest.json',
156+
},
157+
shared: {
158+
react: { singleton: true },
159+
'react-dom': { singleton: true },
160+
},
161+
});
162+
163+
```
164+
165+
## Create from existing project
166+
167+
### Creating a Producer
168+
169+
#### 1. Initializing the Project
15170
16-
import { PackageManagerTabs } from '@theme';
171+
Use [Rsbuild](https://rsbuild.dev/) to create a provider and call the following command:
17172
18173
<PackageManagerTabs
19174
command={{
@@ -33,7 +188,7 @@ import { PackageManagerTabs } from '@theme';
33188
? Select language -> TypeScript
34189
```
35190
36-
### 2. Installing Module Federation Build Plugin
191+
#### 2. Installing Module Federation Build Plugin
37192
38193
Following the steps above for initializing the project, we created a `React` project named `federation_provider`. Execute the following commands in sequence:
39194
@@ -62,7 +217,7 @@ bun add @module-federation/rsbuild-plugin --save-dev
62217
}}
63218
/>
64219
65-
### 3. Exporting Modules by the Producer
220+
#### 3. Exporting Modules by the Producer
66221
67222
> Change the entry file to asynchronous
68223
@@ -118,7 +273,7 @@ export default defineConfig({
118273
});
119274
```
120275
121-
### 4. Starting the Producer
276+
#### 4. Starting the Producer
122277
123278
<PackageManagerTabs
124279
command={{
@@ -147,9 +302,9 @@ start Compiling...
147302
148303
After the project starts, the `Manifest Link: http://localhost:3000/mf-manifest.json` message appears, which is the manifest information link for Module Federation.
149304
150-
## Creating a Consumer
305+
### Creating a Consumer
151306
152-
### 1. Initializing the Project
307+
#### 1. Initializing the Project
153308
154309
Use [Rsbuild](https://rsbuild.dev/) to create a consumer and call the following command:
155310
@@ -171,7 +326,7 @@ Use [Rsbuild](https://rsbuild.dev/) to create a consumer and call the following
171326
? Select language -> TypeScript
172327
```
173328
174-
### 2. Installing Module Federation Build Plugin
329+
#### 2. Installing Module Federation Build Plugin
175330
176331
Following the steps above for initializing the project, we created a `React` project named `federation_consumer`. Execute the following commands in sequence:
177332
@@ -201,7 +356,7 @@ bun add @module-federation/rsbuild-plugin --save-dev
201356
/>
202357
203358
204-
### 3. Consuming the Producer
359+
#### 3. Consuming the Producer
205360
206361
> Declare the `Module Federation` type path in `tsconfig.json`
207362
@@ -285,7 +440,7 @@ export default App;
285440
286441
## Summary
287442
288-
Through the above process, you have completed the export of a component from a 'producer' for use by a 'consumer' based on Module Federation. Along the way, you have preliminarily used and understood the configurations of remotes, exposes, and shared in the Module Federation plugin.
443+
Through the above process, you have completed the export of a component from a 'provider' for use by a 'consumer' based on Module Federation. Along the way, you have preliminarily used and understood the configurations of remotes, exposes, and shared in the Module Federation plugin.
289444
290445
If you wish to learn how to directly export and consume remote modules on Webpack and Rspack build tools, you can refer to: [Rspack Plugin](../basic/rspack), [Webpack Plugin](../basic/webpack)
291446

0 commit comments

Comments
 (0)