Fixed dev mode override to work with unminified JS

Summary: public

The dev mode override feature was built with the assumption that bunlded JS would be minified, and broke with unminified JS. This fixes that by using a more robust regex-based search.

Reviewed By: tadeuzagallo

Differential Revision: D2581240

fb-gh-sync-id: 4d4b45eb8573ceb956b7259550d80a9807f83d59
This commit is contained in:
Nick Lockwood 2015-10-27 04:12:46 -07:00 committed by facebook-github-bot-0
parent 385ac02aeb
commit 500ffa9b76
1 changed files with 11 additions and 4 deletions

View File

@ -190,13 +190,20 @@ RCT_EXTERN NSArray *RCTGetModuleClasses(void);
// Force JS __DEV__ value to match RCT_DEBUG
if (shouldOverrideDev) {
NSString *sourceString = [[NSString alloc] initWithData:source encoding:NSUTF8StringEncoding];
NSRange range = [sourceString rangeOfString:@"__DEV__="];
NSRange range = [sourceString rangeOfString:@"\\b__DEV__\\s*?=\\s*?(!1|!0|false|true)"
options:NSRegularExpressionSearch];
RCTAssert(range.location != NSNotFound, @"It looks like the implementation"
"of __DEV__ has changed. Update -[RCTBatchedBridge loadSource:].");
NSRange valueRange = {range.location + range.length, 2};
if ([[sourceString substringWithRange:valueRange] isEqualToString:@"!1"]) {
source = [[sourceString stringByReplacingCharactersInRange:valueRange withString:@" 1"] dataUsingEncoding:NSUTF8StringEncoding];
NSString *valueString = [sourceString substringWithRange:range];
if ([valueString rangeOfString:@"!1"].length) {
valueString = [valueString stringByReplacingOccurrencesOfString:@"!1" withString:@"!0"];
} else if ([valueString rangeOfString:@"false"].length) {
valueString = [valueString stringByReplacingOccurrencesOfString:@"false" withString:@"true"];
}
source = [[sourceString stringByReplacingCharactersInRange:range withString:valueString]
dataUsingEncoding:NSUTF8StringEncoding];
}
_onSourceLoad(error, source);