diff --git a/packages/metro-config/src/defaults/__tests__/exclusionList-test.js b/packages/metro-config/src/defaults/__tests__/exclusionList-test.js index 6d3528951d..63978c6771 100644 --- a/packages/metro-config/src/defaults/__tests__/exclusionList-test.js +++ b/packages/metro-config/src/defaults/__tests__/exclusionList-test.js @@ -30,6 +30,18 @@ describe('exclusionList', () => { path.sep = originalSeparator; }); + const asPlatformPath = (filepath: string) => + filepath.replaceAll('/', path.sep); + + const nativeBuildOutputPaths = [ + '.cxx', + 'android/.gradle', + 'android/build', + 'android/app/build', + 'ios/build', + 'ios/DerivedData', + ]; + test('proves we can write to path.sep for setting up the tests', () => { setPathSeperator('/'); expect(require('node:path').sep).toBe('/'); @@ -71,6 +83,25 @@ describe('exclusionList', () => { ); expect('/foo/bar').toMatch(exclusionList([/.*[\/\\]foo[\/\\]bar/])); }); + + test.each(nativeBuildOutputPaths)( + 'excludes React Native native build output %s', + buildOutputPath => { + const exclusion = exclusionList(); + + expect(asPlatformPath(buildOutputPath)).toMatch(exclusion); + expect(asPlatformPath(`${buildOutputPath}/generated/file.o`)).toMatch( + exclusion, + ); + }, + ); + + test('does not exclude adjacent source paths', () => { + const exclusion = exclusionList(); + + expect('src/android/app.js').not.toMatch(exclusion); + expect('src/ios/DerivedDataExample.js').not.toMatch(exclusion); + }); }); describe('simulate windows enviornment', () => { @@ -107,5 +138,26 @@ describe('exclusionList', () => { ); expect('\\foo\\bar').toMatch(exclusionList([/.*[\/\\]foo[\/\\]bar/])); }); + + test.each(nativeBuildOutputPaths)( + 'excludes React Native native build output %s', + buildOutputPath => { + const exclusion = exclusionList(); + + expect(asPlatformPath(buildOutputPath)).toMatch(exclusion); + expect(asPlatformPath(`${buildOutputPath}/generated/file.o`)).toMatch( + exclusion, + ); + }, + ); + + test('does not exclude adjacent source paths', () => { + const exclusion = exclusionList(); + + expect(asPlatformPath('src/android/app.js')).not.toMatch(exclusion); + expect(asPlatformPath('src/ios/DerivedDataExample.js')).not.toMatch( + exclusion, + ); + }); }); }); diff --git a/packages/metro-config/src/defaults/exclusionList.js b/packages/metro-config/src/defaults/exclusionList.js index 8a3356388a..7cf0f2ccf8 100644 --- a/packages/metro-config/src/defaults/exclusionList.js +++ b/packages/metro-config/src/defaults/exclusionList.js @@ -11,7 +11,15 @@ import path from 'node:path'; -const list = [/\/__tests__\/.*/]; +const list = [ + /\/__tests__\/.*/, + /(^|\/)\.cxx($|\/.*)/, + /(^|\/)android\/\.gradle($|\/.*)/, + /(^|\/)android\/build($|\/.*)/, + /(^|\/)android\/app\/build($|\/.*)/, + /(^|\/)ios\/build($|\/.*)/, + /(^|\/)ios\/DerivedData($|\/.*)/, +]; function escapeRegExp(pattern: RegExp | string) { if (pattern instanceof RegExp) {