1
+ import type { CosmiconfigResult } from 'cosmiconfig' ;
1
2
import { cosmiconfig , cosmiconfigSync } from 'cosmiconfig' ;
2
3
import { JoiError } from './errors' ;
3
4
import * as schema from './schema' ;
4
- import {
5
+ import type {
5
6
UserConfig ,
6
7
UserDependencyConfig ,
7
8
} from '@react-native-community/cli-types' ;
@@ -10,14 +11,27 @@ import chalk from 'chalk';
10
11
11
12
/**
12
13
* Places to look for the configuration file.
14
+ * Note that we need different sets for CJS and ESM because the synchronous
15
+ * version cannot contain `.mjs` files. Doing so will cause "Error: Missing
16
+ * loader for extension" during runtime.
13
17
*/
14
- const searchPlaces = [
18
+ const searchPlacesForCJS = [
15
19
'react-native.config.js' ,
16
20
'react-native.config.cjs' ,
17
- 'react-native.config.mjs' ,
18
21
'react-native.config.ts' ,
19
- 'react-native.config.mjs' ,
20
22
] ;
23
+ const searchPlaces = [ ...searchPlacesForCJS , 'react-native.config.mjs' ] ;
24
+
25
+ function parseUserConfig ( searchResult : CosmiconfigResult ) : UserConfig {
26
+ const config = searchResult ? searchResult . config : undefined ;
27
+ const result = schema . projectConfig . validate ( config ) ;
28
+
29
+ if ( result . error ) {
30
+ throw new JoiError ( result . error ) ;
31
+ }
32
+
33
+ return result . value as UserConfig ;
34
+ }
21
35
22
36
/**
23
37
* Reads a project configuration as defined by the user in the current
@@ -32,15 +46,7 @@ export async function readConfigFromDiskAsync(
32
46
} ) ;
33
47
34
48
const searchResult = await explorer . search ( rootFolder ) ;
35
-
36
- const config = searchResult ? searchResult . config : undefined ;
37
- const result = schema . projectConfig . validate ( config ) ;
38
-
39
- if ( result . error ) {
40
- throw new JoiError ( result . error ) ;
41
- }
42
-
43
- return result . value as UserConfig ;
49
+ return parseUserConfig ( searchResult ) ;
44
50
}
45
51
46
52
/**
@@ -55,31 +61,13 @@ export function readConfigFromDisk(rootFolder: string): UserConfig {
55
61
} ) ;
56
62
57
63
const searchResult = explorer . search ( rootFolder ) ;
58
-
59
- const config = searchResult ? searchResult . config : undefined ;
60
- const result = schema . projectConfig . validate ( config ) ;
61
-
62
- if ( result . error ) {
63
- throw new JoiError ( result . error ) ;
64
- }
65
-
66
- return result . value as UserConfig ;
64
+ return parseUserConfig ( searchResult ) ;
67
65
}
68
66
69
- /**
70
- * Reads a dependency configuration as defined by the developer
71
- * inside `node_modules`.
72
- */
73
- export async function readDependencyConfigFromDiskAsync (
74
- rootFolder : string ,
67
+ function parseDependencyConfig (
75
68
dependencyName : string ,
76
- ) : Promise < UserDependencyConfig > {
77
- const explorer = cosmiconfig ( 'react-native' , {
78
- stopDir : rootFolder ,
79
- searchPlaces,
80
- } ) ;
81
-
82
- const searchResult = await explorer . search ( rootFolder ) ;
69
+ searchResult : CosmiconfigResult ,
70
+ ) : UserDependencyConfig {
83
71
const config = searchResult ? searchResult . config : emptyDependencyConfig ;
84
72
85
73
const result = schema . dependencyConfig . validate ( config , { abortEarly : false } ) ;
@@ -93,14 +81,31 @@ export async function readDependencyConfigFromDiskAsync(
93
81
) } contains invalid configuration: ${ chalk . bold (
94
82
validationError . message ,
95
83
) } .
96
-
84
+
97
85
Please verify it's properly linked using "npx react-native config" command and contact the package maintainers about this.` ) ,
98
86
) ;
99
87
}
100
88
101
89
return result . value as UserDependencyConfig ;
102
90
}
103
91
92
+ /**
93
+ * Reads a dependency configuration as defined by the developer
94
+ * inside `node_modules`.
95
+ */
96
+ export async function readDependencyConfigFromDiskAsync (
97
+ rootFolder : string ,
98
+ dependencyName : string ,
99
+ ) : Promise < UserDependencyConfig > {
100
+ const explorer = cosmiconfig ( 'react-native' , {
101
+ stopDir : rootFolder ,
102
+ searchPlaces,
103
+ } ) ;
104
+
105
+ const searchResult = await explorer . search ( rootFolder ) ;
106
+ return parseDependencyConfig ( dependencyName , searchResult ) ;
107
+ }
108
+
104
109
/**
105
110
* Reads a dependency configuration as defined by the developer
106
111
* inside `node_modules` synchronously.
@@ -112,29 +117,11 @@ export function readDependencyConfigFromDisk(
112
117
) : UserDependencyConfig {
113
118
const explorer = cosmiconfigSync ( 'react-native' , {
114
119
stopDir : rootFolder ,
115
- searchPlaces,
120
+ searchPlaces : searchPlacesForCJS ,
116
121
} ) ;
117
122
118
123
const searchResult = explorer . search ( rootFolder ) ;
119
- const config = searchResult ? searchResult . config : emptyDependencyConfig ;
120
-
121
- const result = schema . dependencyConfig . validate ( config , { abortEarly : false } ) ;
122
-
123
- if ( result . error ) {
124
- const validationError = new JoiError ( result . error ) ;
125
- logger . warn (
126
- inlineString ( `
127
- Package ${ chalk . bold (
128
- dependencyName ,
129
- ) } contains invalid configuration: ${ chalk . bold (
130
- validationError . message ,
131
- ) } .
132
-
133
- Please verify it's properly linked using "npx react-native config" command and contact the package maintainers about this.` ) ,
134
- ) ;
135
- }
136
-
137
- return result . value as UserDependencyConfig ;
124
+ return parseDependencyConfig ( dependencyName , searchResult ) ;
138
125
}
139
126
140
127
const emptyDependencyConfig = {
0 commit comments