Skip to content

Commit fca3a39

Browse files
makovkastarfacebook-github-bot
authored andcommitted
Add native module for loading split JS bundles in development
Reviewed By: mdvacca, cpojer Differential Revision: D22001709 fbshipit-source-id: 4e378fd6ae90268e7db9092a71628205b9f7c37d
1 parent 2e2c881 commit fca3a39

15 files changed

+272
-46
lines changed

ReactAndroid/src/main/java/com/facebook/react/BUCK

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ rn_android_library(
3535
react_native_target("java/com/facebook/react/module/model:model"),
3636
react_native_target("java/com/facebook/react/modules/appregistry:appregistry"),
3737
react_native_target("java/com/facebook/react/modules/appearance:appearance"),
38+
react_native_target("java/com/facebook/react/modules/bundleloader:bundleloader"),
3839
react_native_target("java/com/facebook/react/modules/debug:debug"),
3940
react_native_target("java/com/facebook/react/modules/fabric:fabric"),
4041
react_native_target("java/com/facebook/react/modules/debug:interfaces"),

ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.facebook.react.module.annotations.ReactModuleList;
2222
import com.facebook.react.module.model.ReactModuleInfo;
2323
import com.facebook.react.module.model.ReactModuleInfoProvider;
24+
import com.facebook.react.modules.bundleloader.NativeDevSplitBundleLoaderModule;
2425
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
2526
import com.facebook.react.modules.core.DeviceEventManagerModule;
2627
import com.facebook.react.modules.core.ExceptionsManagerModule;
@@ -56,6 +57,7 @@
5657
SourceCodeModule.class,
5758
TimingModule.class,
5859
UIManagerModule.class,
60+
NativeDevSplitBundleLoaderModule.class,
5961
})
6062
public class CoreModulesPackage extends TurboReactPackage implements ReactPackageLogger {
6163

@@ -101,7 +103,8 @@ public ReactModuleInfoProvider getReactModuleInfoProvider() {
101103
HeadlessJsTaskSupportModule.class,
102104
SourceCodeModule.class,
103105
TimingModule.class,
104-
UIManagerModule.class
106+
UIManagerModule.class,
107+
NativeDevSplitBundleLoaderModule.class,
105108
};
106109

107110
final Map<String, ReactModuleInfo> reactModuleInfoMap = new HashMap<>();
@@ -158,6 +161,9 @@ public NativeModule getModule(String name, ReactApplicationContext reactContext)
158161
return createUIManager(reactContext);
159162
case DeviceInfoModule.NAME:
160163
return new DeviceInfoModule(reactContext);
164+
case NativeDevSplitBundleLoaderModule.NAME:
165+
return new NativeDevSplitBundleLoaderModule(
166+
reactContext, mReactInstanceManager.getDevSupportManager());
161167
default:
162168
throw new IllegalArgumentException(
163169
"In CoreModulesPackage, could not find Native module for " + name);

ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java

+5
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ public void loadScriptFromFile(String fileName, String sourceURL, boolean loadSy
240240
jniLoadScriptFromFile(fileName, sourceURL, loadSynchronously);
241241
}
242242

243+
@Override
244+
public void loadSplitBundleFromFile(String fileName, String sourceURL) {
245+
jniLoadScriptFromFile(fileName, sourceURL, false);
246+
}
247+
243248
private native void jniSetSourceURL(String sourceURL);
244249

245250
private native void jniRegisterSegment(int segmentId, String path);

ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java

+19
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,25 @@ public String loadScript(JSBundleLoaderDelegate delegate) {
7373
};
7474
}
7575

76+
/**
77+
* Same as {{@link JSBundleLoader#createCachedBundleFromNetworkLoader(String, String)}}, but for
78+
* split bundles in development.
79+
*/
80+
public static JSBundleLoader createCachedSplitBundleFromNetworkLoader(
81+
final String sourceURL, final String cachedFileLocation) {
82+
return new JSBundleLoader() {
83+
@Override
84+
public String loadScript(JSBundleLoaderDelegate delegate) {
85+
try {
86+
delegate.loadSplitBundleFromFile(cachedFileLocation, sourceURL);
87+
return sourceURL;
88+
} catch (Exception e) {
89+
throw DebugServerException.makeGeneric(sourceURL, e.getMessage(), e);
90+
}
91+
}
92+
};
93+
}
94+
7695
/**
7796
* This loader is used when proxy debugging is enabled. In that case there is no point in fetching
7897
* the bundle from device as remote executor will have to do it anyway.

ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoaderDelegate.java

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public interface JSBundleLoaderDelegate {
3333
*/
3434
void loadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously);
3535

36+
/**
37+
* Load a split JS bundle from the filesystem. See {@link
38+
* JSBundleLoader#createCachedSplitBundleFromNetworkLoader(String, String)}.
39+
*/
40+
void loadSplitBundleFromFile(String fileName, String sourceURL);
41+
3642
/**
3743
* This API is used in situations where the JS bundle is being executed not on the device, but on
3844
* a host machine. In that case, we must provide two source URLs for the JS bundle: One to be used

ReactAndroid/src/main/java/com/facebook/react/common/DebugServerException.java

+9
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,25 @@ public static DebugServerException makeGeneric(
4040
return new DebugServerException(reason + message + extra, t);
4141
}
4242

43+
private final String mOriginalMessage;
44+
4345
private DebugServerException(String description, String fileName, int lineNumber, int column) {
4446
super(description + "\n at " + fileName + ":" + lineNumber + ":" + column);
47+
mOriginalMessage = description;
4548
}
4649

4750
public DebugServerException(String description) {
4851
super(description);
52+
mOriginalMessage = description;
4953
}
5054

5155
public DebugServerException(String detailMessage, Throwable throwable) {
5256
super(detailMessage, throwable);
57+
mOriginalMessage = detailMessage;
58+
}
59+
60+
public String getOriginalMessage() {
61+
return mOriginalMessage;
5362
}
5463

5564
/**

ReactAndroid/src/main/java/com/facebook/react/devsupport/BundleDownloader.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ public void downloadBundleFromURL(
107107
Request.Builder requestBuilder) {
108108

109109
final Request request =
110-
requestBuilder
111-
.url(formatBundleUrl(bundleURL))
112-
.addHeader("Accept", "multipart/mixed")
113-
.build();
110+
requestBuilder.url(bundleURL).addHeader("Accept", "multipart/mixed").build();
114111
mDownloadBundleFromURLCall = Assertions.assertNotNull(mClient.newCall(request));
115112
mDownloadBundleFromURLCall.enqueue(
116113
new Callback() {
@@ -165,10 +162,6 @@ public void onResponse(Call call, final Response response) throws IOException {
165162
});
166163
}
167164

168-
private String formatBundleUrl(String bundleURL) {
169-
return bundleURL;
170-
}
171-
172165
private void processMultipartResponse(
173166
final String url,
174167
final Response response,

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,26 @@ private boolean getJSMinifyMode() {
419419
}
420420

421421
private String createBundleURL(String mainModuleID, BundleType type, String host) {
422+
return createBundleURL(mainModuleID, type, host, false, true);
423+
}
424+
425+
private String createSplitBundleURL(String mainModuleID, String host) {
426+
return createBundleURL(mainModuleID, BundleType.BUNDLE, host, true, false);
427+
}
428+
429+
private String createBundleURL(
430+
String mainModuleID, BundleType type, String host, boolean modulesOnly, boolean runModule) {
422431
return String.format(
423432
Locale.US,
424-
"http://%s/%s.%s?platform=android&dev=%s&minify=%s&app=%s",
433+
"http://%s/%s.%s?platform=android&dev=%s&minify=%s&app=%s&modulesOnly=%s&runModule=%s",
425434
host,
426435
mainModuleID,
427436
type.typeID(),
428437
getDevMode(),
429438
getJSMinifyMode(),
430-
mPackageName);
439+
mPackageName,
440+
modulesOnly ? "true" : "false",
441+
runModule ? "true" : "false");
431442
}
432443

433444
private String createBundleURL(String mainModuleID, BundleType type) {
@@ -454,6 +465,11 @@ public String getDevServerBundleURL(final String jsModulePath) {
454465
mSettings.getPackagerConnectionSettings().getDebugServerHost());
455466
}
456467

468+
public String getDevServerSplitBundleURL(String jsModulePath) {
469+
return createSplitBundleURL(
470+
jsModulePath, mSettings.getPackagerConnectionSettings().getDebugServerHost());
471+
}
472+
457473
public void isPackagerRunning(final PackagerStatusCallback callback) {
458474
String statusURL =
459475
createPackagerStatusURL(mSettings.getPackagerConnectionSettings().getDebugServerHost());

0 commit comments

Comments
 (0)