Skip to content

Commit 8b478e9

Browse files
Esemesek2heal1
andauthored
chore: add react-native docs (#3905)
Co-authored-by: Hanric <[email protected]>
1 parent 8a55b4c commit 8b478e9

File tree

4 files changed

+527
-2
lines changed

4 files changed

+527
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"name": "runtime",
55
"label": "Runtime"
66
},
7-
"rsbuild", "rspack", "webpack", "rspress","vite","type-prompt","cli", "css-isolate",
7+
"rsbuild", "rspack", "webpack", "rspress","vite","metro","type-prompt","cli", "css-isolate",
88
{
99
"type": "dir",
1010
"name": "data-fetch",
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
# Metro
2+
3+
Metro Module Federation brings the power of distributed architecture to React Native mobile development.
4+
5+
## Overview
6+
7+
The Module Federation implementation for React Native uses a custom Metro bundler integration that enables:
8+
9+
- **Micro-frontend architecture** for React Native apps
10+
- **Code sharing** between multiple React Native applications
11+
- **Independent deployment** of application modules
12+
- **Runtime module loading** for dynamic updates
13+
14+
> **Note**: Module Federation support for Metro bundler is still experimental and may lack some functionality or certain integrations. Projects or libraries that rely on custom Metro configurations aren't supported yet and might not work.
15+
16+
## Packages
17+
18+
The React Native Module Federation ecosystem consists of several packages:
19+
20+
- `@module-federation/metro` - Core integration with Metro to enable Module Federation
21+
- `@module-federation/metro-plugin-rnc-cli` - React Native CLI integration
22+
- `@module-federation/metro-plugin-rnef` - React Native Enterprise Framework integration
23+
24+
## Installation
25+
26+
Install the required packages to your React Native project using your preferred package manager:
27+
28+
```shell
29+
# Core package (required)
30+
pnpm add @module-federation/metro
31+
32+
# If your project is using React Native CLI
33+
pnpm add @module-federation/metro-plugin-rnc-cli
34+
35+
# If your project is using React Native Enterprise Framework (RNEF)
36+
pnpm add @module-federation/metro-plugin-rnef
37+
```
38+
39+
## Configuration
40+
41+
Wrap your Metro configuration with the `withModuleFederation` function to enable Module Federation. You should be wrapping all the federated modules' Metro configuration in this hook: host application and mini applications.
42+
43+
```javascript
44+
const { withModuleFederation } = require('@module-federation/metro');
45+
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
46+
47+
const config = {};
48+
49+
module.exports = withModuleFederation(
50+
mergeConfig(getDefaultConfig(__dirname), config),
51+
{
52+
// Module Federation configuration follows the same format as documented at:
53+
// https://module-federation.io/configure/index.html
54+
// Note: Some features might not be available in React Native environment
55+
name: 'YourAppName',
56+
remotes: {
57+
// Define remote applications (for host apps)
58+
// remoteName: 'remoteName@http://localhost:8082/mf-manifest.json',
59+
},
60+
exposes: {
61+
// Expose modules (for remote apps)
62+
// './Component': './src/Component.tsx',
63+
},
64+
shared: {
65+
// Host applications should set eager: true for all the shared dependencies
66+
react: {
67+
singleton: true,
68+
eager: true,
69+
requiredVersion: '19.1.0',
70+
version: '19.1.0',
71+
},
72+
'react-native': {
73+
singleton: true,
74+
eager: true,
75+
requiredVersion: '0.80.0',
76+
version: '0.80.0',
77+
},
78+
},
79+
},
80+
{
81+
// These experimental flags have to be enabled in order to patch older packages
82+
// Can be omitted if your project is using supported React Native and Metro versions
83+
flags: {
84+
// Enable patching HMR Client from React Native
85+
unstable_patchHMRClient: true,
86+
// Enable patching React Native CLI
87+
unstable_patchInitializeCore: true,
88+
// Enable patching runtime require from Metro
89+
unstable_patchRuntimeRequire: true,
90+
},
91+
}
92+
);
93+
```
94+
95+
### App Async Boundary Setup
96+
97+
Wrap your main App component with `withAsyncStartup` to enable Module Federation runtime. This creates an async boundary that ensures the Module Federation runtime is properly initialized before your app component renders.
98+
99+
```javascript
100+
import { withAsyncStartup } from '@module-federation/runtime';
101+
import { AppRegistry } from 'react-native';
102+
103+
// Create async boundary through withAsyncStartup helper
104+
// Pass the getter function for the app component
105+
// Optionally pass a getter function for the fallback component
106+
const WrappedApp = withAsyncStartup(
107+
() => require('./App'),
108+
() => require('./Fallback') // Optional fallback component
109+
);
110+
111+
AppRegistry.registerComponent('YourAppName', WrappedApp);
112+
```
113+
114+
The `withAsyncStartup` function:
115+
116+
- Waits for Module Federation runtime initialization before rendering your app
117+
- Uses React Suspense to handle the async loading
118+
- Accepts an optional fallback component to show during initialization
119+
120+
## Usage Patterns
121+
122+
### Host Application
123+
124+
A host application consumes remote modules from other applications:
125+
126+
```javascript
127+
// metro.config.js for host app
128+
module.exports = withModuleFederation(
129+
mergeConfig(getDefaultConfig(__dirname), config),
130+
{
131+
name: 'HostApp',
132+
remotes: {
133+
'mini-app': 'miniApp@http://localhost:8082/mf-manifest.json',
134+
'another-app': 'anotherApp@http://192.168.1.100:8083/mf-manifest.json',
135+
},
136+
shared: {
137+
react: { singleton: true, eager: true },
138+
'react-native': { singleton: true, eager: true },
139+
},
140+
}
141+
);
142+
```
143+
144+
### Remote Application (Mini App)
145+
146+
A remote application exposes modules that can be consumed by host applications:
147+
148+
```javascript
149+
// metro.config.js for remote app
150+
module.exports = withModuleFederation(
151+
mergeConfig(getDefaultConfig(__dirname), config),
152+
{
153+
name: 'MiniApp',
154+
exposes: {
155+
'./Screen': './src/Screen.tsx',
156+
'./Component': './src/Component.tsx',
157+
},
158+
shared: {
159+
react: { singleton: true, eager: true },
160+
'react-native': { singleton: true, eager: true },
161+
},
162+
}
163+
);
164+
```
165+
166+
## CLI Commands
167+
168+
The React Native CLI integration provides additional commands for bundling federated applications:
169+
170+
### Bundle Module Federation Host
171+
172+
Bundle a host application that consumes remote modules:
173+
174+
```shell
175+
# Bundle for iOS
176+
react-native bundle-mf-host --entry-file index.js --platform ios
177+
178+
# Bundle for Android
179+
react-native bundle-mf-host --entry-file index.js --platform android
180+
```
181+
182+
### Bundle Module Federation Remote
183+
184+
Bundle a remote application (mini app) that exposes modules:
185+
186+
```shell
187+
# Bundle for iOS
188+
react-native bundle-mf-remote --platform ios
189+
190+
# Bundle for Android
191+
react-native bundle-mf-remote --platform android
192+
```
193+
194+
Both commands support all the same options as the standard `react-native bundle` command.
195+
196+
> **Note**: These commands are provided by the `@module-federation/metro-plugin-rnc-cli` package.
197+
198+
## React Native Enterprise Framework (RNEF) Integration
199+
200+
For projects using [React Native Enterprise Framework (RNEF)](https://github.com/callstack/react-native-enterprise-framework), add the Module Federation plugin to your `rnef.config.mjs`:
201+
202+
```javascript
203+
import { pluginMetroModuleFederation } from '@module-federation/metro-plugin-rnef';
204+
import { platformAndroid } from '@rnef/platform-android';
205+
import { platformIOS } from '@rnef/platform-ios';
206+
import { pluginMetro } from '@rnef/plugin-metro';
207+
208+
/** @type {import('@rnef/config').Config} */
209+
export default {
210+
bundler: pluginMetro(),
211+
platforms: {
212+
ios: platformIOS(),
213+
android: platformAndroid(),
214+
},
215+
plugins: [pluginMetroModuleFederation()],
216+
};
217+
```
218+
219+
## API Reference
220+
221+
### `withModuleFederation(metroConfig, federationConfig, options?)`
222+
223+
Wraps your Metro configuration to enable Module Federation.
224+
225+
#### Parameters
226+
227+
- `metroConfig` (MetroConfig) - Your existing Metro configuration
228+
- `federationConfig` (FederationConfig) - Module Federation configuration
229+
- `options` (Options) - Optional configuration for experimental features
230+
231+
#### FederationConfig Interface
232+
233+
```typescript
234+
export interface ModuleFederationConfig {
235+
name: string;
236+
filename?: string;
237+
remotes?: Record<string, string>;
238+
exposes?: Record<string, string>;
239+
shared?: Shared;
240+
shareStrategy?: 'loaded-first' | 'version-first';
241+
plugins?: string[];
242+
}
243+
```
244+
245+
#### SharedConfig Interface
246+
247+
```typescript
248+
export interface SharedConfig {
249+
singleton: boolean;
250+
eager: boolean;
251+
version: string;
252+
requiredVersion: string;
253+
import?: false;
254+
}
255+
```
256+
257+
## Examples and Best Practices
258+
259+
The configuration follows the standard [Module Federation configuration format](https://module-federation.io/configure/). For comprehensive information about Module Federation concepts, configuration options, and usage patterns, please refer to the official [Module Federation documentation](https://module-federation.io/).
260+
261+
For working examples and detailed implementation guides, check out the [Module Federation Metro repository](https://github.com/module-federation/metro) which includes several example applications demonstrating different usage patterns and integrations.

apps/website-new/docs/zh/guide/basic/_meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"name": "runtime",
55
"label": "运行时"
66
},
7-
"rsbuild", "rspack", "webpack", "rspress","vite","type-prompt","cli","css-isolate",
7+
"rsbuild", "rspack", "webpack", "rspress","vite","metro","type-prompt","cli","css-isolate",
88
{
99
"type": "dir",
1010
"name": "data-fetch",

0 commit comments

Comments
 (0)