Add a naive WPO implementation

Summary: public

RFC: The minifier haven't been stripping dead-code, and it also can't kill unused
modules, so as a temporary solution this inlines `__DEV__`, kill dead branches
and kill dead modules. For now I'm just white-listing the dev variable, but we
could definitely do better than that, but as a temporary fix this should be
helpful.

I also intend to kill some dead variables, so we can kill unused requires,
although inline-requires can also fix it.

Reviewed By: vjeux

Differential Revision: D2605454

fb-gh-sync-id: 50acb9dcbded07a43080b93ac826a5ceda695936
This commit is contained in:
Tadeu Zagallo
2015-11-17 03:36:50 -08:00
committed by facebook-github-bot-5
parent bd1885b5d4
commit 0b46a0c13b
9 changed files with 297 additions and 48 deletions

View File

@@ -180,30 +180,6 @@ RCT_EXTERN NSArray<Class> *RCTGetModuleClasses(void);
RCTProfileEndAsyncEvent(0, @"init,download", cookie, @"JavaScript download", nil);
RCTPerformanceLoggerEnd(RCTPLScriptDownload);
// Only override the value of __DEV__ if running in debug mode, and if we
// haven't explicitly overridden the packager dev setting in the bundleURL
BOOL shouldOverrideDev = RCT_DEBUG && ([self.bundleURL isFileURL] ||
[self.bundleURL.absoluteString rangeOfString:@"dev="].location == NSNotFound);
// Force JS __DEV__ value to match RCT_DEBUG
if (shouldOverrideDev) {
NSString *sourceString = [[NSString alloc] initWithData:source encoding:NSUTF8StringEncoding];
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:].");
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);
};