Skip to content

Commit

Permalink
RCTDevSplitBundleLoader: Add ability to enable modulesOnly and `run…
Browse files Browse the repository at this point in the history
…Module`

Summary: Changelog: [Internal]

Reviewed By: jimmy623

Differential Revision: D21882786

fbshipit-source-id: 1f90c62c3a385f1b12caef4bcc01e5bf53f0f882
  • Loading branch information
cpojer authored and facebook-github-bot committed Jun 5, 2020
1 parent d676558 commit 8821d27
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
24 changes: 16 additions & 8 deletions RNTester/RNTesterUnitTests/RCTBundleURLProviderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,22 @@

static NSURL *localhostBundleURL()
{
return [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8081/%@.bundle?platform=ios&dev=true&minify=false&app=com.apple.dt.xctest.tool", testFile]];
return [NSURL
URLWithString:
[NSString
stringWithFormat:
@"http://localhost:8081/%@.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runMdoule=true&app=com.apple.dt.xctest.tool",
testFile]];
}

static NSURL *ipBundleURL()
{
return [NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.1:8081/%@.bundle?platform=ios&dev=true&minify=false&app=com.apple.dt.xctest.tool", testFile]];
return [NSURL
URLWithString:
[NSString
stringWithFormat:
@"http://192.168.1.1:8081/%@.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runMdoule=true&app=com.apple.dt.xctest.tool",
testFile]];
}

@implementation NSBundle (RCTBundleURLProviderTests)
Expand All @@ -50,16 +60,14 @@ - (void)setUp
{
[super setUp];

RCTSwapInstanceMethods([NSBundle class],
@selector(URLForResource:withExtension:),
@selector(RCT_URLForResource:withExtension:));
RCTSwapInstanceMethods(
[NSBundle class], @selector(URLForResource:withExtension:), @selector(RCT_URLForResource:withExtension:));
}

- (void)tearDown
{
RCTSwapInstanceMethods([NSBundle class],
@selector(URLForResource:withExtension:),
@selector(RCT_URLForResource:withExtension:));
RCTSwapInstanceMethods(
[NSBundle class], @selector(URLForResource:withExtension:), @selector(RCT_URLForResource:withExtension:));

[super tearDown];
}
Expand Down
26 changes: 23 additions & 3 deletions React/Base/RCTBundleURLProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ extern const NSUInteger kRCTBundleURLProviderDefaultPort;
*/
- (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot fallbackURLProvider:(NSURL * (^)(void))fallbackURLProvider;

/**
* Returns the jsBundleURL for a given split bundle entrypoint in development
*/
- (NSURL *)jsBundleURLForSplitBundleRoot:(NSString *)bundleRoot;

/**
* Returns the jsBundleURL for a given bundle entrypoint and
* the fallback offline JS bundle if the packager is not running.
Expand Down Expand Up @@ -89,15 +94,30 @@ extern const NSUInteger kRCTBundleURLProviderDefaultPort;
+ (instancetype)sharedSettings;

/**
Given a hostname for the packager and a bundle root, returns the URL to the js bundle. Generally you should use the
instance method -jsBundleURLForBundleRoot:fallbackResource: which includes logic to guess if the packager is running
and fall back to a pre-packaged bundle if it is not.
* Given a hostname for the packager and a bundle root, returns the URL to the js bundle. Generally you should use the
* instance method -jsBundleURLForBundleRoot:fallbackResource: which includes logic to guess if the packager is running
* and fall back to a pre-packaged bundle if it is not.
*
* The options here mirror some of Metro's Bundling Options:
* - enableDev: Whether to keep or remove `__DEV__` blocks from the bundle.
* - enableMinification: Enables or disables minification. Usually production bundles are minified and development
* bundles are not.
* - modulesOnly: When true, will only send module definitions without polyfills and without the require-runtime.
* - runModule: When true, will run the main module after defining all modules. This is used in the main bundle but not
* in split bundles.
*/
+ (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot
packagerHost:(NSString *)packagerHost
enableDev:(BOOL)enableDev
enableMinification:(BOOL)enableMinification;

+ (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot
packagerHost:(NSString *)packagerHost
enableDev:(BOOL)enableDev
enableMinification:(BOOL)enableMinification
modulesOnly:(BOOL)modulesOnly
runModule:(BOOL)runModule;

/**
* Given a hostname for the packager and a resource path (including "/"), return the URL to the resource.
* In general, please use the instance method to decide if the packager is running and fallback to the pre-packaged
Expand Down
32 changes: 30 additions & 2 deletions React/Base/RCTBundleURLProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ - (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot fallbackURLProvider:(
}
}

- (NSURL *)jsBundleURLForSplitBundleRoot:(NSString *)bundleRoot
{
return [RCTBundleURLProvider jsBundleURLForBundleRoot:bundleRoot
packagerHost:[self packagerServerHost]
enableDev:[self enableDev]
enableMinification:[self enableMinification]
modulesOnly:YES
runModule:NO];
}

- (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot
fallbackResource:(NSString *)resourceName
fallbackExtension:(NSString *)extension
Expand Down Expand Up @@ -186,12 +196,30 @@ + (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot
packagerHost:(NSString *)packagerHost
enableDev:(BOOL)enableDev
enableMinification:(BOOL)enableMinification

{
return [self jsBundleURLForBundleRoot:bundleRoot
packagerHost:packagerHost
enableDev:enableDev
enableMinification:enableMinification
modulesOnly:NO
runModule:YES];
}

+ (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot
packagerHost:(NSString *)packagerHost
enableDev:(BOOL)enableDev
enableMinification:(BOOL)enableMinification
modulesOnly:(BOOL)modulesOnly
runModule:(BOOL)runModule
{
NSString *path = [NSString stringWithFormat:@"/%@.bundle", bundleRoot];
// When we support only iOS 8 and above, use queryItems for a better API.
NSString *query = [NSString stringWithFormat:@"platform=ios&dev=%@&minify=%@",
NSString *query = [NSString stringWithFormat:@"platform=ios&dev=%@&minify=%@&modulesOnly=%@&runMdoule=%@",
enableDev ? @"true" : @"false",
enableMinification ? @"true" : @"false"];
enableMinification ? @"true" : @"false",
modulesOnly ? @"true" : @"false",
runModule ? @"true" : @"false"];
NSString *bundleID = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleIdentifierKey];
if (bundleID) {
query = [NSString stringWithFormat:@"%@&app=%@", query, bundleID];
Expand Down

0 comments on commit 8821d27

Please sign in to comment.