|
| 1 | +# `@module-federation/metro` |
| 2 | + |
| 3 | +## Getting started |
| 4 | + |
| 5 | +### Installation |
| 6 | + |
| 7 | +Use your favorite package manager to install these required packages to your React Native project. |
| 8 | + |
| 9 | +```shell |
| 10 | +# Using pnpm |
| 11 | +pnpm add @module-federation/metro |
| 12 | + |
| 13 | +# If your project is using React Native CLI |
| 14 | +pnpm add @module-federation/metro-plugin-rnc-cli |
| 15 | + |
| 16 | +# If your project is using RNEF |
| 17 | +pnpm add @module-federation/metro-plugin-rnef |
| 18 | +``` |
| 19 | + |
| 20 | +### Configuration |
| 21 | + |
| 22 | +Wrap Metro configuration with `withModuleFederation` function that enables module federation in your project. |
| 23 | +You should be wrapping all the federated modules' Metro configuration in this hook: host application and mini applications. |
| 24 | + |
| 25 | +```javascript |
| 26 | +const { withModuleFederation } = require('@module-federation/metro'); |
| 27 | +const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config'); |
| 28 | + |
| 29 | +const config = {}; |
| 30 | + |
| 31 | +module.exports = withModuleFederation( |
| 32 | + mergeConfig(getDefaultConfig(__dirname), config), |
| 33 | + { |
| 34 | + // Module Federation configuration follows the same format as documented at: |
| 35 | + // https://module-federation.io/configure/index.html |
| 36 | + // Note: Some features might not be available in React Native environment |
| 37 | + name: 'YourAppName', |
| 38 | + remotes: { |
| 39 | + // Define remote applications (for host apps) |
| 40 | + // remoteName: 'remoteName@http://localhost:8082/mf-manifest.json', |
| 41 | + }, |
| 42 | + exposes: { |
| 43 | + // Expose modules (for remote apps) |
| 44 | + // './Component': './src/Component.tsx', |
| 45 | + }, |
| 46 | + shared: { |
| 47 | + // Host applications should set eager: true for all the shared dependencies |
| 48 | + react: { |
| 49 | + singleton: true, |
| 50 | + eager: true, |
| 51 | + requiredVersion: '19.1.0', |
| 52 | + version: '19.1.0', |
| 53 | + }, |
| 54 | + 'react-native': { |
| 55 | + singleton: true, |
| 56 | + eager: true, |
| 57 | + requiredVersion: '0.80.0', |
| 58 | + version: '0.80.0', |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + // These experimental flags have to be enabled in order to patch older packages |
| 64 | + // Can be omitted if your project is using supported React Native and Metro versions |
| 65 | + flags: { |
| 66 | + // Enable patching HMR Client from React Native |
| 67 | + unstable_patchHMRClient: true, |
| 68 | + // Enable patching React Native CLI |
| 69 | + unstable_patchInitializeCore: true, |
| 70 | + // Enable patching runtime require from Metro |
| 71 | + unstable_patchRuntimeRequire: true, |
| 72 | + }, |
| 73 | + } |
| 74 | +); |
| 75 | +``` |
| 76 | + |
| 77 | +#### Additional Configuration for RNEF Users |
| 78 | + |
| 79 | +If you're using React Native Enterprise Framework (RNEF), follow the additional configuration instructions in the [RNEF Plugin README](../plugin-rnef/README.md). |
| 80 | + |
| 81 | +### App Async Boundary Setup |
| 82 | + |
| 83 | +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. |
| 84 | + |
| 85 | +```javascript |
| 86 | +import { withAsyncStartup } from '@module-federation/runtime'; |
| 87 | +import { AppRegistry } from 'react-native'; |
| 88 | + |
| 89 | +// Create async boundary through withAsyncStartup helper |
| 90 | +// Pass the getter function for the app component |
| 91 | +// Optionally pass a getter function for the fallback component |
| 92 | +const WrappedApp = withAsyncStartup( |
| 93 | + () => require('./App'), |
| 94 | + () => require('./Fallback') // Optional fallback component |
| 95 | +); |
| 96 | + |
| 97 | +AppRegistry.registerComponent('YourAppName', WrappedApp); |
| 98 | +``` |
| 99 | + |
| 100 | +The `withAsyncStartup` function: |
| 101 | +- Waits for Module Federation runtime initialization before rendering your app |
| 102 | +- Uses React Suspense to handle the async loading |
| 103 | +- Accepts an optional fallback component to show during initialization |
| 104 | + |
| 105 | +## API Reference |
| 106 | + |
| 107 | +### `withModuleFederation(metroConfig, federationConfig, options?)` |
| 108 | + |
| 109 | +Wraps your Metro configuration to enable Module Federation. |
| 110 | + |
| 111 | +#### Parameters |
| 112 | + |
| 113 | +- `metroConfig` (MetroConfig) - Your existing Metro configuration |
| 114 | +- `federationConfig` (FederationConfig) - Module Federation configuration |
| 115 | +- `options` (Options) - Optional configuration for experimental features |
| 116 | + |
| 117 | +#### FederationConfig |
| 118 | + |
| 119 | +```typescript |
| 120 | + |
| 121 | +export interface ModuleFederationConfig { |
| 122 | + name: string; |
| 123 | + filename?: string; |
| 124 | + remotes?: Record<string, string>; |
| 125 | + exposes?: Record<string, string>; |
| 126 | + shared?: Shared; |
| 127 | + shareStrategy?: 'loaded-first' | 'version-first'; |
| 128 | + plugins?: string[]; |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +#### SharedConfig |
| 133 | + |
| 134 | +```typescript |
| 135 | +export interface SharedConfig { |
| 136 | + singleton: boolean; |
| 137 | + eager: boolean; |
| 138 | + version: string; |
| 139 | + requiredVersion: string; |
| 140 | + import?: false; |
| 141 | +} |
| 142 | +``` |
| 143 | + |
0 commit comments