1- /**
2- * Configuration file of Metro.
3- */
41import path from 'path' ;
52import { ConfigT , InputConfigT , loadConfig } from 'metro-config' ;
63import type { Config } from '@react-native-community/cli-types' ;
74import { reactNativePlatformResolver } from './metroPlatformResolver' ;
85
9- const INTERNAL_CALLSITES_REGEX = new RegExp (
10- [
11- '/Libraries/Renderer/implementations/.+\\.js$' ,
12- '/Libraries/BatchedBridge/MessageQueue\\.js$' ,
13- '/Libraries/YellowBox/.+\\.js$' ,
14- '/Libraries/LogBox/.+\\.js$' ,
15- '/Libraries/Core/Timers/.+\\.js$' ,
16- '/Libraries/WebSocket/.+\\.js$' ,
17- '/Libraries/vendor/.+\\.js$' ,
18- '/node_modules/react-devtools-core/.+\\.js$' ,
19- '/node_modules/react-refresh/.+\\.js$' ,
20- '/node_modules/scheduler/.+\\.js$' ,
21- '/node_modules/event-target-shim/.+\\.js$' ,
22- '/node_modules/invariant/.+\\.js$' ,
23- '/node_modules/react-native/index.js$' ,
24- '/metro-runtime/.+\\.js$' ,
25- '^\\[native code\\]$' ,
26- ] . join ( '|' ) ,
27- ) ;
28-
296export type { Config } ;
307
318export type ConfigLoadingContext = Pick <
@@ -34,9 +11,9 @@ export type ConfigLoadingContext = Pick<
3411> ;
3512
3613/**
37- * Default configuration
14+ * Get the config options to override based on RN CLI inputs.
3815 */
39- export const getDefaultConfig = ( ctx : ConfigLoadingContext ) : InputConfigT => {
16+ function getOverrideConfig ( ctx : ConfigLoadingContext ) : InputConfigT {
4017 const outOfTreePlatforms = Object . keys ( ctx . platforms ) . filter (
4118 ( platform ) => ctx . platforms [ platform ] . npmPackageName ,
4219 ) ;
@@ -55,9 +32,7 @@ export const getDefaultConfig = (ctx: ConfigLoadingContext): InputConfigT => {
5532 { } ,
5633 ) ,
5734 ) ,
58- resolverMainFields : [ 'react-native' , 'browser' , 'main' ] ,
5935 platforms : [ ...Object . keys ( ctx . platforms ) , 'native' ] ,
60- unstable_conditionNames : [ 'import' , 'require' , 'react-native' ] ,
6136 } ,
6237 serializer : {
6338 // We can include multiple copies of InitializeCore here because metro will
@@ -73,33 +48,9 @@ export const getDefaultConfig = (ctx: ConfigLoadingContext): InputConfigT => {
7348 ) ,
7449 ) ,
7550 ] ,
76- getPolyfills : ( ) =>
77- require ( path . join ( ctx . reactNativePath , 'rn-get-polyfills' ) ) ( ) ,
78- } ,
79- server : {
80- port : Number ( process . env . RCT_METRO_PORT ) || 8081 ,
8151 } ,
82- symbolicator : {
83- customizeFrame : ( frame ) => {
84- const collapse = Boolean (
85- frame . file && INTERNAL_CALLSITES_REGEX . test ( frame . file ) ,
86- ) ;
87- return { collapse} ;
88- } ,
89- } ,
90- transformer : {
91- allowOptionalDependencies : true ,
92- babelTransformerPath : require . resolve (
93- 'metro-react-native-babel-transformer' ,
94- ) ,
95- assetRegistryPath : 'react-native/Libraries/Image/AssetRegistry' ,
96- asyncRequireModulePath : require . resolve (
97- 'metro-runtime/src/modules/asyncRequire' ,
98- ) ,
99- } ,
100- watchFolders : [ ] ,
10152 } ;
102- } ;
53+ }
10354
10455export interface ConfigOptionsT {
10556 maxWorkers ?: number ;
@@ -113,17 +64,18 @@ export interface ConfigOptionsT {
11364}
11465
11566/**
116- * Loads Metro Config and applies `options` on top of the resolved config.
67+ * Load Metro config.
11768 *
118- * This allows the CLI to always overwrite the file settings.
69+ * Allows the CLI to override certain defaults in the base `metro.config.js`
70+ * based on dynamic user options in `ctx`.
11971 */
12072export default function loadMetroConfig (
12173 ctx : ConfigLoadingContext ,
12274 options ?: ConfigOptionsT ,
12375) : Promise < ConfigT > {
124- const defaultConfig = { ... getDefaultConfig ( ctx ) } ;
76+ const overrideConfig = getOverrideConfig ( ctx ) ;
12577 if ( options && options . reporter ) {
126- defaultConfig . reporter = options . reporter ;
78+ overrideConfig . reporter = options . reporter ;
12779 }
128- return loadConfig ( { cwd : ctx . root , ...options } , defaultConfig ) ;
80+ return loadConfig ( { cwd : ctx . root , ...options } , overrideConfig ) ;
12981}
0 commit comments