@@ -152,6 +152,7 @@ function extractLibrariesFromJSON(configFile, dependencyPath) {
152
152
const config = configFile . codegenConfig ;
153
153
return [
154
154
{
155
+ libraryName : configFile . name ,
155
156
config,
156
157
libraryPath : dependencyPath ,
157
158
} ,
@@ -251,19 +252,23 @@ function findExternalLibraries(pkgJson, projectRoot) {
251
252
} ) ;
252
253
}
253
254
254
- function findLibrariesFromReactNativeConfig ( projectRoot ) {
255
+ function readRNConfigJSFile ( projectRoot ) {
255
256
const rnConfigFileName = 'react-native.config.js' ;
256
257
257
- console . log (
258
- `\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in ${ rnConfigFileName } ` ,
259
- ) ;
260
-
261
258
const rnConfigFilePath = path . resolve ( projectRoot , rnConfigFileName ) ;
262
259
263
260
if ( ! fs . existsSync ( rnConfigFilePath ) ) {
264
261
return [ ] ;
265
262
}
266
- const rnConfig = require ( rnConfigFilePath ) ;
263
+ return require ( rnConfigFilePath ) ;
264
+ }
265
+
266
+ function findLibrariesFromReactNativeConfig ( projectRoot ) {
267
+ console . log (
268
+ `\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in react-native.config.js` ,
269
+ ) ;
270
+
271
+ const rnConfig = readRNConfigJSFile ( projectRoot ) ;
267
272
268
273
if ( rnConfig . dependencies == null ) {
269
274
return [ ] ;
@@ -289,6 +294,33 @@ function findLibrariesFromReactNativeConfig(projectRoot) {
289
294
} ) ;
290
295
}
291
296
297
+ // Function to look for libraries explicitly unlinked from the app
298
+ // through the react-native.config.js file.
299
+ // If this happens, it might be that the app does not need
300
+ // to generate code for that library as it won't be used by that platform
301
+ // @return { [libraryName: string]: [platform: string] }
302
+ function findNotLinkedLibraries ( projectRoot ) {
303
+ const rnConfig = readRNConfigJSFile ( projectRoot ) ;
304
+
305
+ if ( rnConfig . dependencies == null ) {
306
+ return { } ;
307
+ }
308
+
309
+ let notLinkedLibraries = { } ;
310
+
311
+ Object . keys ( rnConfig . dependencies ) . forEach ( name => {
312
+ const dependency = rnConfig . dependencies [ name ] ;
313
+ let notLinkedPlatforms = [ ] ;
314
+ Object . keys ( dependency . platforms ) . forEach ( platform => {
315
+ if ( dependency . platforms [ platform ] == null ) {
316
+ notLinkedPlatforms . push ( platform ) ;
317
+ }
318
+ } ) ;
319
+ notLinkedLibraries [ name ] = notLinkedPlatforms
320
+ } ) ;
321
+ return notLinkedLibraries ;
322
+ }
323
+
292
324
function findProjectRootLibraries ( pkgJson , projectRoot ) {
293
325
console . log ( '[Codegen] Searching for codegen-enabled libraries in the app.' ) ;
294
326
@@ -694,6 +726,8 @@ function execute(projectRoot, targetPlatform, baseOutputPath) {
694
726
let platforms =
695
727
targetPlatform === 'all' ? supportedPlatforms : [ targetPlatform ] ;
696
728
729
+ const notLinkedLibraries = findNotLinkedLibraries ( projectRoot ) ;
730
+
697
731
for ( const platform of platforms ) {
698
732
const outputPath = computeOutputPath (
699
733
projectRoot ,
@@ -702,7 +736,15 @@ function execute(projectRoot, targetPlatform, baseOutputPath) {
702
736
platform ,
703
737
) ;
704
738
705
- const schemaInfos = generateSchemaInfos ( libraries ) ;
739
+ const schemaInfos = generateSchemaInfos ( libraries . filter ( library => {
740
+ const unlinkedPlatforms = notLinkedLibraries [ library . libraryName ] ;
741
+ if ( unlinkedPlatforms && unlinkedPlatforms . includes ( platform ) ) {
742
+ console . log ( `[Codegen - ${ library . libraryName } ] Skipping Codegen on ${ platform } ` ) ;
743
+ return false ;
744
+ }
745
+ return true ;
746
+ } ) ) ;
747
+
706
748
generateNativeCode (
707
749
outputPath ,
708
750
schemaInfos . filter ( schemaInfo =>
0 commit comments