Skip to content

Commit c7be4b2

Browse files
Copilotmeta-codesync[bot]
authored andcommitted
Update Configuration docs for TypeScript, ESM support and YAML deprecation (#1632)
Summary: Update docs to reflect recent additions to config file support Pull Request resolved: #1632 Test Plan: Ran new TypeScript merging example through `tsc` to check it Reviewed By: vzaidman Differential Revision: D89770691 Pulled By: robhogan fbshipit-source-id: 4d3f7d7771cc35b0d26c8703a6e3645ae5da68ae
1 parent 85a75c6 commit c7be4b2

1 file changed

Lines changed: 52 additions & 55 deletions

File tree

docs/Configuration.md

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,28 @@ id: configuration
33
title: Configuring Metro
44
---
55

6-
A Metro config can be created in these three ways (ordered by priority):
6+
A Metro config can be created in the following file formats (ordered by priority):
77

8-
1. `metro.config.js`
9-
2. `metro.config.json`
10-
3. The `metro` field in `package.json`
8+
1. `metro.config.js` / `metro.config.cjs` / `metro.config.mjs` (CommonJS or ESM)
9+
2. `metro.config.ts` / `metro.config.cts` / `metro.config.mts` (TypeScript)
10+
3. `metro.config.json`
11+
4. `.config/metro.js` / `.config/metro.cjs` / `.config/metro.mjs` / `.config/metro.ts` / `.config/metro.cts` / `.config/metro.mts` / `.config/metro.json`
12+
5. The `metro` field in `package.json`
1113

1214
You can also give a custom file to the configuration by specifying `--config <path/to/config>` when calling the CLI.
1315

16+
:::warning Deprecated
17+
18+
YAML config files (`.yaml`, `.yml`) are **deprecated** and will be removed in a future version of Metro. Please migrate to a JavaScript, TypeScript, or JSON config file. When Metro loads a YAML config file, it will display a deprecation warning.
19+
20+
:::
21+
22+
:::info TypeScript Config Support
23+
24+
TypeScript config files are supported in Node.js 24.0.0+ or Node.js 22.6.0+ with the `--experimental-strip-types` flag. If your Node.js version doesn't support loading TypeScript natively, you'll see an error with instructions when attempting to load a TypeScript config file.
25+
26+
:::
27+
1428
:::note
1529

1630
When Metro is started via the React Native CLI, some defaults are different from those mentioned below.
@@ -20,10 +34,13 @@ See the [React Native repository](https://github.com/facebook/react-native/blob/
2034

2135
## Configuration Structure
2236

23-
The configuration is based on [our concepts](./Concepts.md), which means that for every module we have a separate config option. A common configuration structure in Metro looks like this:
37+
The configuration is based on [our concepts](./Concepts.md), which means that for every module we have a separate config option. A basic configuration structure in Metro looks like this:
2438

25-
```js
26-
module.exports = {
39+
```typescript
40+
// metro.config.mts
41+
import type {MetroConfig} from 'metro-config';
42+
43+
const config: MetroConfig = {
2744
/* general options */
2845

2946
resolver: {
@@ -45,10 +62,17 @@ module.exports = {
4562
}
4663
}
4764
};
65+
66+
export default config;
4867
```
4968

50-
### General Options
69+
:::note
5170

71+
See [Merging Configurations](#merging-configurations) below for more advanced forms.
72+
73+
:::
74+
75+
### General Options
5276

5377
#### `cacheStores`
5478

@@ -672,7 +696,7 @@ key?: string | Buffer, // Private key (contents, not path)
672696
requestCert?: boolean, // Whether to authenticate the remote peer by requesting a certificate
673697
```
674698
675-
Notice that when overriding the base config, object tls configs extend the base tls config, false overrides the base tls configs, and `null` and `undefined` are ignored.
699+
Notice that when overriding the base config, object `tls` configs extend the base `tls` config, `false` overrides the base `tls` configs, and `null` and `undefined` are ignored.
676700
677701
When running Metro with `Metro.runServer` with the `secureServerOptions` property Metro will likewise start an HTTPS server merging with the `config.server.tls` object if provided, overriding it.
678702
@@ -768,50 +792,23 @@ This allows overriding and removing default config parameters such as `platforms
768792
769793
#### Merging Example
770794
771-
```js
772-
// metro.config.js
773-
const { mergeConfig } = require('metro-config');
774-
775-
const configA = {
776-
/* general options */
777-
778-
resolver: {
779-
/* resolver options */
780-
},
781-
transformer: {
782-
/* transformer options */
783-
},
784-
serializer: {
785-
/* serializer options */
786-
},
787-
server: {
788-
/* server options */
789-
}
790-
};
791-
792-
const configB = {
793-
/* general options */
794-
795-
resolver: {
796-
/* resolver options */
797-
},
798-
transformer: {
799-
/* transformer options */
800-
},
801-
serializer: {
802-
/* serializer options */
803-
},
804-
server: {
805-
/* server options */
806-
}
807-
};
808-
809-
// Function forms may be used to access the previous configuration
810-
configCFn = (previousConfig /* result of mergeConfig(configA, configB) */) => {
811-
return {
812-
watchFolders: [...previousConfig.watchFolders, 'my-watch-folder'],
813-
}
814-
}
815-
816-
module.exports = mergeConfig(configA, configB, configCFn);
795+
```typescript
796+
// metro.config.ts
797+
import type {ConfigT} from 'metro-config';
798+
import {mergeConfig} from 'metro-config';
799+
800+
export default (defaults: ConfigT) =>
801+
mergeConfig(
802+
defaults,
803+
// Function form: extends the default additionalExts
804+
config => ({
805+
watcher: {additionalExts: [...config.watcher.additionalExts, 'mts', 'cts']},
806+
}),
807+
// Plain object form
808+
{transformer: {minifierPath: 'metro-minify-terser'}},
809+
// Function form: additionalExts already includes 'mts' and 'cts' from above
810+
config => ({
811+
watcher: {additionalExts: [...config.watcher.additionalExts, 'css']},
812+
}),
813+
);
817814
```

0 commit comments

Comments
 (0)