diff --git a/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js b/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js index 2a5aec0ce09..c4848e53396 100644 --- a/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js +++ b/packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js @@ -738,3 +738,52 @@ describe('generateSchemaInfos', () => { } }); }); + +describe('execute with codegen', () => { + beforeEach(() => { + jest.resetModules(); + }); + + it('skips the JS sources of libraries that include generated code', () => { + const mockCombineSchemasInFileList = jest.fn( + (_files: ReadonlyArray, _platform: string) => ({}), + ); + const mockGenerateNativeCode = jest.fn(); + const mockLibraries = [ + {config: {name: 'LibA', jsSrcsDir: 'src'}, libraryPath: '/tmp/libA'}, + { + config: {name: 'LibB', jsSrcsDir: 'src', includesGeneratedCode: true}, + libraryPath: '/tmp/libB', + }, + ]; + jest.mock('../codegen-utils', () => ({ + getCombineJSToSchema: () => ({ + combineSchemasInFileList: mockCombineSchemasInFileList, + }), + })); + jest.mock('tinyglobby', () => ({globSync: () => []})); + jest.mock('../generate-artifacts-executor/generateNativeCode', () => ({ + generateNativeCode: mockGenerateNativeCode, + })); + jest.mock('../generate-artifacts-executor/utils', () => ({ + ...jest.requireActual('../generate-artifacts-executor/utils'), + buildCodegenIfNeeded: () => {}, + cleanupEmptyFilesAndFolders: () => {}, + findCodegenEnabledLibraries: () => mockLibraries, + readPkgJsonInDirectory: () => ({}), + readReactNativeConfig: () => ({}), + })); + + const {execute: executeCodegen} = require('../generate-artifacts-executor'); + + executeCodegen('/tmp/app', 'android', '/tmp/app', 'app'); + + expect(mockCombineSchemasInFileList).toHaveBeenCalledTimes(1); + expect(mockCombineSchemasInFileList.mock.calls[0][0]).toEqual([ + path.join('/tmp/libA', 'src'), + ]); + expect(mockGenerateNativeCode.mock.calls[0][1]).toEqual([ + expect.objectContaining({library: mockLibraries[0]}), + ]); + }); +}); diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js index 553af035a48..cd1ce4a4bae 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/index.js @@ -139,12 +139,15 @@ function execute( : path.join(outputPath, 'ReactCodegen'); if (runReactNativeCodegen) { - const schemaInfos = generateSchemaInfos(libraries, platform); + const schemaInfos = generateSchemaInfos( + libraries.filter(library => + mustGenerateNativeCode(projectRoot, library), + ), + platform, + ); generateNativeCode( reactCodegenOutputPath, - schemaInfos.filter(schemaInfo => - mustGenerateNativeCode(projectRoot, schemaInfo), - ), + schemaInfos, pkgJsonIncludesGeneratedCode(pkgJson), platform, forceOutputPath, @@ -276,14 +279,14 @@ function defaultOutputPathForIOS(baseOutputPath /*: string */) { function mustGenerateNativeCode( includeLibraryPath /*: string */, - schemaInfo /*: $FlowFixMe */, + library /*: $FlowFixMe */, ) { // If library's 'codegenConfig' sets 'includesGeneratedCode' to 'true', // then we assume that native code is shipped with the library, // and we don't need to generate it. return ( - schemaInfo.library.libraryPath === includeLibraryPath || - !schemaInfo.library.config.includesGeneratedCode + library.libraryPath === includeLibraryPath || + !library.config.includesGeneratedCode ); }