mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-01 14:25:08 +08:00
Allow developers to load JavaScript bundle from any source.
Summary: This patch adds two pieces of functionality:
- Exposes `JSBundleLoader` to allow a developer to load JavaScript bundles as they choose.
- Adds `ReactBridge.loadScripFromFile` method which loads a JavaScript bundle from an arbitrary file path.
Example usage:
```
JSBundleLoader jsBundleLoader = new JSBundleLoader() {
Override
public void loadScript(ReactBridge reactBridge) {
reactBridge.loadScriptFromFile("/sdcard/Download/index.android.bundle");
}
};
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setJSBundleLoader(jsBundleLoader)
.setJSMainModuleName("") /* necessary due to TODO(6803830) */
.addPackage(new MainReactPackage())
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
```
cc ide
Closes https://github.com/facebook/react-native/pull/3189
Reviewed By: svcscm
Differential Revision: D2535819
Pulled By: mkonicek
fb-gh-sync-id: f319299dbe29bab3b7e91f94249c14b270d9fec3
This commit is contained in:
committed by
facebook-github-bot-7
parent
b59f250214
commit
3a743ef228
@@ -9,7 +9,7 @@
|
||||
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import android.content.res.AssetManager;
|
||||
import android.content.Context;
|
||||
|
||||
/**
|
||||
* A class that stores JS bundle information and allows {@link CatalystInstance} to load a correct
|
||||
@@ -22,18 +22,22 @@ public abstract class JSBundleLoader {
|
||||
* should be used. JS bundle will be read from assets directory in native code to save on passing
|
||||
* large strings from java to native memory.
|
||||
*/
|
||||
public static JSBundleLoader createAssetLoader(
|
||||
final AssetManager assetManager,
|
||||
final String assetFileName) {
|
||||
public static JSBundleLoader createFileLoader(
|
||||
final Context context,
|
||||
final String fileName) {
|
||||
return new JSBundleLoader() {
|
||||
@Override
|
||||
public void loadScript(ReactBridge bridge) {
|
||||
bridge.loadScriptFromAssets(assetManager, assetFileName);
|
||||
if (fileName.startsWith("assets://")) {
|
||||
bridge.loadScriptFromAssets(context.getAssets(), fileName.replaceFirst("assets://", ""));
|
||||
} else {
|
||||
bridge.loadScriptFromFile(fileName, fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSourceUrl() {
|
||||
return "file:///android_asset/" + assetFileName;
|
||||
return fileName;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -51,7 +55,7 @@ public abstract class JSBundleLoader {
|
||||
return new JSBundleLoader() {
|
||||
@Override
|
||||
public void loadScript(ReactBridge bridge) {
|
||||
bridge.loadScriptFromNetworkCached(sourceURL, cachedFileLocation);
|
||||
bridge.loadScriptFromFile(cachedFileLocation, sourceURL);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,7 +74,7 @@ public abstract class JSBundleLoader {
|
||||
return new JSBundleLoader() {
|
||||
@Override
|
||||
public void loadScript(ReactBridge bridge) {
|
||||
bridge.loadScriptFromNetworkCached(sourceURL, null);
|
||||
bridge.loadScriptFromFile(null, sourceURL);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user