react-native/React/Base/RCTBundleURLProcessor.m

74 lines
2.1 KiB
Mathematica
Raw Normal View History

Hot Loading E2E basic flow Summary: public Implement all the necessary glue code for several diffs submitted before to get Hot Loading work end to end: - Simplify `HMRClient`: we don't need to make it stateful allowing to enable and disable it because both when we enable and disable the interface we need to reload the bundle. - On the native side we introduced a singleton to process the bundle URL. This new class might alter the url to include the `hot` attribute. I'm not 100% sure this is the best way to implement this but we cannot use `CTLSettings` for this as it's are not available on oss and I didn't want to contaminate `RCTBridge` with something specific to hot loading. Also, we could potentially use this processor for other things in the future. Please let me know if you don't like this approach or you have a better idea :). - Use this processor to alter the default bundle URL and request a `hot` bundle when hot loading is enabled. Also make sure to enable the HMR interface when the client activates it on the dev menu. - Add packager `hot` option. - Include gaeron's `react-transform` on Facebook's JS transformer. The current implementation couples a bit React Native to this feature because `react-transform-hmr` is required on `InitializeJavaScriptAppEngine`. Ideally, the packager should accept an additional list of requires and include them on the bundle among all their dependencies. Note this is not the same as the option `runBeforeMainModule` as that one only adds a require to the provided module but doesn't include all the dependencies that module amy have that the entry point doesn't. I'll address this in a follow up task to enable asap hot loading (9536142) I had to remove 2 `.babelrc` files from `react-proxy` and `react-deep-force-update`. There's an internal task for fixing the underlaying issue to avoid doing this horrible hack (t9515889). Reviewed By: vjeux Differential Revision: D2790806 fb-gh-sync-id: d4b78a2acfa071d6b3accc2e6716ef5611ad4fda
2015-12-29 00:43:21 +00:00
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <Foundation/Foundation.h>
#import "RCTBundleURLProcessor.h"
@implementation RCTBundleURLProcessor
NSDictionary *_qsAttributes;
+ (id)sharedProcessor
{
static RCTBundleURLProcessor *sharedProcessor = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedProcessor = [self new];
});
return sharedProcessor;
}
- (instancetype)init
{
// dictionary with additional query string attributes that will get appended
// to the bundle URL
_qsAttributes = [NSMutableDictionary new];
return self;
}
- (NSString *)getQueryStringValue:(NSString *)attribute
{
return [_qsAttributes valueForKey:attribute];
}
- (void)setQueryStringValue:(NSString *)value forAttribute:(NSString *)attribute
{
[_qsAttributes setValue:value forKey:attribute];
}
- (NSURL *)process:(NSURL *)url
{
if (url.isFileURL || [_qsAttributes count] == 0) {
return url;
}
// append either `?` or `&` depending on whether there are query string
// attibutes or not.
NSString *urlString = url.absoluteString;
if ([urlString rangeOfString:@"?"].location == NSNotFound) {
urlString = [urlString stringByAppendingString:@"?"];
} else {
urlString = [urlString stringByAppendingString:@"&"];
}
// array with new query string attributes
NSMutableArray *parts = [NSMutableArray new];
for (id attribute in _qsAttributes) {
if ([urlString rangeOfString:[NSString stringWithFormat:@"%@=", attribute]].location != NSNotFound) {
[NSException raise:@"Cannot override attribute" format:@"Attribute %@ is already present in url: %@", attribute, url.absoluteString];
}
[parts addObject:[NSString stringWithFormat:@"%@=%@", attribute, _qsAttributes[attribute]]];
}
return [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", urlString, [parts componentsJoinedByString:@"&"]]];
}
@end