|
| 1 | +--- |
| 2 | +id: metro |
| 3 | +title: Metro |
| 4 | +--- |
| 5 | + |
| 6 | +React Native uses [Metro](https://facebook.github.io/metro/) to build your JavaScript code and assets. |
| 7 | + |
| 8 | +## Configuring Metro |
| 9 | + |
| 10 | +Configuration options for Metro can be customized in your project's `metro.config.js` file. This can export either: |
| 11 | + |
| 12 | +- **An object (recommended)** that will be merged on top of Metro's internal config defaults. |
| 13 | +- [**A function**](#advanced-using-a-config-function) that will be called with Metro's internal config defaults and should return a final config object. |
| 14 | + |
| 15 | +:::tip |
| 16 | +Please see [**Configuring Metro**](https://facebook.github.io/metro/docs/configuration) on the Metro website for documentation on all available config options. |
| 17 | +::: |
| 18 | + |
| 19 | +In React Native, your Metro config should extend either [@react-native/metro-config](https://www.npmjs.com/package/@react-native/metro-config) or [@expo/metro-config](https://www.npmjs.com/package/@expo/metro-config). These packages contain essential defaults necessary to build and run React Native apps. |
| 20 | + |
| 21 | +Below is the default `metro.config.js` file in a React Native template project: |
| 22 | + |
| 23 | +<!-- prettier-ignore --> |
| 24 | +```js |
| 25 | +const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config'); |
| 26 | + |
| 27 | +/** |
| 28 | + * Metro configuration |
| 29 | + * https://facebook.github.io/metro/docs/configuration |
| 30 | + * |
| 31 | + * @type {import('metro-config').MetroConfig} |
| 32 | + */ |
| 33 | +const config = {}; |
| 34 | + |
| 35 | +module.exports = mergeConfig(getDefaultConfig(__dirname), config); |
| 36 | +``` |
| 37 | + |
| 38 | +Metro options you wish to customize can be done so within the `config` object. We strongly recommend defining all config values statically within this file. |
| 39 | + |
| 40 | +### Advanced: Using a config function |
| 41 | + |
| 42 | +Exporting a config function is an opt-in to managing the final config yourself — **Metro will not apply any internal defaults**. This pattern can be useful when needing to read the base default config object from Metro or to set options dynamically. |
| 43 | + |
| 44 | +:::info |
| 45 | +**From @react-native/metro-config `0.72.1`**, it is no longer necessary to use a config function to access the complete default config. See the **Tip** section below. |
| 46 | +::: |
| 47 | + |
| 48 | +<!-- prettier-ignore --> |
| 49 | +```js |
| 50 | +const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config'); |
| 51 | + |
| 52 | +module.exports = function (baseConfig) { |
| 53 | + const defaultConfig = mergeConfig(baseConfig, getDefaultConfig(__dirname)); |
| 54 | + const {resolver: {assetExts, sourceExts}} = defaultConfig; |
| 55 | + |
| 56 | + return mergeConfig( |
| 57 | + defaultConfig, |
| 58 | + { |
| 59 | + resolver: { |
| 60 | + assetExts: assetExts.filter(ext => ext !== 'svg'), |
| 61 | + sourceExts: [...sourceExts, 'svg'], |
| 62 | + }, |
| 63 | + }, |
| 64 | + ); |
| 65 | +}; |
| 66 | +``` |
| 67 | + |
| 68 | +:::tip |
| 69 | +Using a config function is for advanced use cases. A simpler method than the above, e.g. for customising `sourceExts`, would be to read these defaults from **@react-native/metro-config**. |
| 70 | + |
| 71 | +**Alternative** |
| 72 | + |
| 73 | +<!-- prettier-ignore --> |
| 74 | +```js |
| 75 | +const defaultConfig = getDefaultConfig(__dirname); |
| 76 | + |
| 77 | +const config = { |
| 78 | + resolver: { |
| 79 | + sourceExts: [...defaultConfig.resolver.sourceExts, 'svg'], |
| 80 | + }, |
| 81 | +}; |
| 82 | + |
| 83 | +module.exports = mergeConfig(defaultConfig, config); |
| 84 | +``` |
| 85 | + |
| 86 | +**However!**, we recommend copying and editing when overriding these config values — placing the source of truth in your config file. |
| 87 | + |
| 88 | +✅ **Recommended** |
| 89 | + |
| 90 | +<!-- prettier-ignore --> |
| 91 | +```js |
| 92 | +const config = { |
| 93 | + resolver: { |
| 94 | + sourceExts: ['js', 'ts', 'tsx', 'svg'], |
| 95 | + }, |
| 96 | +}; |
| 97 | +``` |
| 98 | + |
| 99 | +::: |
| 100 | + |
| 101 | +## Learn more about Metro |
| 102 | + |
| 103 | +- [Metro website](https://facebook.github.io/metro/) |
| 104 | +- [Video: "Metro & React Native DevX" talk at App.js 2023](https://www.youtube.com/watch?v=c9D4pg0y9cI) |
0 commit comments