mirror of
https://github.com/status-im/react-native.git
synced 2025-01-25 00:39:03 +00:00
4242bd9c83
Summary: This is to fix a bug that prevents bundling of projects that contain spaces (or other special characters) in their names. #### Reproduction steps before the fix 1. Create a project with a space in the name: ![screen shot 2015-04-16 at 17 23 46](https://cloud.githubusercontent.com/assets/1121616/7176887/63af36de-e45d-11e4-9aa9-40586560b716.png) 2. Follow the steps in `OPTION 2` for running from a bundled file, i.e. create the `main.bundle` file, add it to the project if is not there already, and uncomment `jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];` 3. Run the application. This is what happens: ![screen shot 2015-04-16 at 17 27 48](https://cloud.githubusercontent.com/assets/1121616/7176955/f139764a-e45d-11e4-8dc8-3c13aab70828.png) To prove that it has to do with a space in the name, refactor the project name to not contain a space: ![screen shot 2015-04-16 at 17 28 27](https://cloud.githubusercontent.com/assets/1121616/7176966/056b6c9a Closes https://github.com/facebook/react-native/pull/876 Github Author: Herman Schaaf <hermanschaaf@gmail.com> Test Plan: Imported from GitHub, without a `Test Plan:` line.
118 lines
4.1 KiB
Objective-C
Executable File
118 lines
4.1 KiB
Objective-C
Executable File
/**
|
|
* 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 "RCTJavaScriptLoader.h"
|
|
|
|
#import "RCTBridge.h"
|
|
#import "RCTConvert.h"
|
|
#import "RCTSourceCode.h"
|
|
#import "RCTUtils.h"
|
|
|
|
@implementation RCTJavaScriptLoader
|
|
{
|
|
__weak RCTBridge *_bridge;
|
|
}
|
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
|
{
|
|
if ((self = [super init])) {
|
|
_bridge = bridge;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)loadBundleAtURL:(NSURL *)scriptURL onComplete:(void (^)(NSError *))onComplete
|
|
{
|
|
NSURL *originalURL = scriptURL;
|
|
if (!scriptURL.scheme || [scriptURL isFileURL]) {
|
|
scriptURL = [RCTConvert NSURL:scriptURL.path];
|
|
}
|
|
|
|
if (scriptURL == nil) {
|
|
NSError *error = [NSError errorWithDomain:@"JavaScriptLoader" code:1 userInfo:@{
|
|
NSLocalizedDescriptionKey: originalURL ? [NSString stringWithFormat:@"Script URL '%@' could not be found.", originalURL] : @"No script URL provided."
|
|
}];
|
|
onComplete(error);
|
|
return;
|
|
}
|
|
|
|
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:scriptURL completionHandler:
|
|
^(NSData *data, NSURLResponse *response, NSError *error) {
|
|
|
|
// Handle general request errors
|
|
if (error) {
|
|
if ([[error domain] isEqualToString:NSURLErrorDomain]) {
|
|
NSString *desc = [@"Could not connect to development server. Ensure node server is running and available on the same network - run 'npm start' from react-native root\n\nURL: " stringByAppendingString:[scriptURL absoluteString]];
|
|
NSDictionary *userInfo = @{
|
|
NSLocalizedDescriptionKey: desc,
|
|
NSLocalizedFailureReasonErrorKey: [error localizedDescription],
|
|
NSUnderlyingErrorKey: error,
|
|
};
|
|
error = [NSError errorWithDomain:@"JSServer"
|
|
code:error.code
|
|
userInfo:userInfo];
|
|
}
|
|
onComplete(error);
|
|
return;
|
|
}
|
|
|
|
// Parse response as text
|
|
NSStringEncoding encoding = NSUTF8StringEncoding;
|
|
if (response.textEncodingName != nil) {
|
|
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName);
|
|
if (cfEncoding != kCFStringEncodingInvalidId) {
|
|
encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
|
|
}
|
|
}
|
|
NSString *rawText = [[NSString alloc] initWithData:data encoding:encoding];
|
|
|
|
// Handle HTTP errors
|
|
if ([response isKindOfClass:[NSHTTPURLResponse class]] && [(NSHTTPURLResponse *)response statusCode] != 200) {
|
|
NSDictionary *userInfo;
|
|
NSDictionary *errorDetails = RCTJSONParse(rawText, nil);
|
|
if ([errorDetails isKindOfClass:[NSDictionary class]] &&
|
|
[errorDetails[@"errors"] isKindOfClass:[NSArray class]]) {
|
|
NSMutableArray *fakeStack = [[NSMutableArray alloc] init];
|
|
for (NSDictionary *err in errorDetails[@"errors"]) {
|
|
[fakeStack addObject: @{
|
|
@"methodName": err[@"description"] ?: @"",
|
|
@"file": err[@"filename"] ?: @"",
|
|
@"lineNumber": err[@"lineNumber"] ?: @0
|
|
}];
|
|
}
|
|
userInfo = @{
|
|
NSLocalizedDescriptionKey: errorDetails[@"message"] ?: @"No message provided",
|
|
@"stack": fakeStack,
|
|
};
|
|
} else {
|
|
userInfo = @{NSLocalizedDescriptionKey: rawText};
|
|
}
|
|
error = [NSError errorWithDomain:@"JSServer"
|
|
code:[(NSHTTPURLResponse *)response statusCode]
|
|
userInfo:userInfo];
|
|
|
|
onComplete(error);
|
|
return;
|
|
}
|
|
RCTSourceCode *sourceCodeModule = _bridge.modules[RCTBridgeModuleNameForClass([RCTSourceCode class])];
|
|
sourceCodeModule.scriptURL = scriptURL;
|
|
sourceCodeModule.scriptText = rawText;
|
|
|
|
[_bridge enqueueApplicationScript:rawText url:scriptURL onComplete:^(NSError *scriptError) {
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
onComplete(scriptError);
|
|
});
|
|
}];
|
|
}];
|
|
|
|
[task resume];
|
|
}
|
|
|
|
@end
|