Skip to content

Commit bad7183

Browse files
committed
Fix measureIOS release build broken after RN 0.62
1 parent eb16d14 commit bad7183

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

src/commands/common/ObjcPatcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export default class ObjcPatcher {
1717
return this._content.indexOf(this._patchSig) >= 0;
1818
}
1919

20-
addImport(file: string): ObjcPatcher {
21-
const lastImportBegin = this._content.lastIndexOf('\n#import');
20+
addImport(file: string, searchPattern = '\n#import'): ObjcPatcher {
21+
const lastImportBegin = this._content.lastIndexOf(searchPattern);
2222
const lastImportEnd = this._content.indexOf('\n', lastImportBegin + 1);
2323
const headPart = this._content.substring(0, lastImportEnd);
2424
const tailPart = this._content.substring(lastImportEnd);

src/commands/common/__tests__/objcPatcher.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,50 @@ describe('ObjcPatcher', () => {
4242
expect(fs.writeFileSync.mock.calls[0][1]).toBe(afterPatch);
4343
});
4444

45+
test('addImport() will append imports for specified searchPattern', () => {
46+
const beforePatch = `\
47+
#import <UIKit/UIKit.h>
48+
#import <React/RCTRootView.h>
49+
50+
#if DEBUG
51+
#import <FlipperKit/FlipperClient.h>
52+
#endif
53+
54+
@implementation AppDelegate
55+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
56+
{
57+
return NO;
58+
}
59+
`;
60+
61+
const afterPatch = `\
62+
/* Patched by ObjcPatcher: foo */
63+
#import <UIKit/UIKit.h>
64+
#import <React/RCTRootView.h>
65+
#import <React/Foo.h>
66+
67+
#if DEBUG
68+
#import <FlipperKit/FlipperClient.h>
69+
#endif
70+
71+
@implementation AppDelegate
72+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
73+
{
74+
return NO;
75+
}
76+
`;
77+
fs.readFileSync = jest.fn().mockReturnValueOnce(beforePatch);
78+
79+
fs.writeFileSync = jest.fn();
80+
81+
const patcher = new ObjcPatcher('/foo', 'foo');
82+
patcher
83+
.addImport('<React/Foo.h>', '\n#import <React/RCTRootView.h>')
84+
.write('/bar');
85+
expect(fs.writeFileSync.mock.calls[0][0]).toBe('/bar');
86+
expect(fs.writeFileSync.mock.calls[0][1]).toBe(afterPatch);
87+
});
88+
4589
test('isPatched() returns false if the file has not been patched', () => {
4690
const beforePatch = `\
4791
#import <UIKit/UIKit.h>

src/commands/measureIOS.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,12 @@ static vm_size_t RCTGetResidentMemorySize(void)
7979
continue;
8080
}
8181
patcher
82-
.addImport('<mach/mach.h>')
82+
.addImport('<mach/mach.h>', '\n#import <React/RCTRootView.h>')
8383
.addFunction(addFunctionGetRssMemory)
84-
.addImport('<React/RCTPerformanceLogger.h>')
84+
.addImport(
85+
'<React/RCTPerformanceLogger.h>',
86+
'\n#import <React/RCTRootView.h>',
87+
)
8588
.replace(
8689
searchPatternWithinDidFinishLaunchingWithOptions,
8790
`$1\n${addCodeMeasureAfterFiveSeconds}`,

0 commit comments

Comments
 (0)