consistent use of URL for resolving asset source
Reviewed By: frantic Differential Revision: D5759789 fbshipit-source-id: daf33b6b66c4bff7f43213ee49286eafb6775f00
This commit is contained in:
parent
cb8a116d05
commit
b89d6c8e04
|
@ -51,13 +51,13 @@ class AssetSourceResolver {
|
|||
|
||||
serverUrl: ?string;
|
||||
// where the bundle is being run from
|
||||
bundlePath: ?string;
|
||||
bundleUrl: ?string;
|
||||
// the asset to resolve
|
||||
asset: PackagerAsset;
|
||||
|
||||
constructor(serverUrl: ?string, bundlePath: ?string, asset: PackagerAsset) {
|
||||
constructor(serverUrl: ?string, bundleUrl: ?string, asset: PackagerAsset) {
|
||||
this.serverUrl = serverUrl;
|
||||
this.bundlePath = bundlePath;
|
||||
this.bundleUrl = bundleUrl;
|
||||
this.asset = asset;
|
||||
}
|
||||
|
||||
|
@ -66,7 +66,7 @@ class AssetSourceResolver {
|
|||
}
|
||||
|
||||
isLoadedFromFileSystem(): boolean {
|
||||
return !!this.bundlePath;
|
||||
return !!this.bundleUrl;
|
||||
}
|
||||
|
||||
defaultAsset(): ResolvedAssetSource {
|
||||
|
@ -79,7 +79,7 @@ class AssetSourceResolver {
|
|||
this.drawableFolderInBundle() :
|
||||
this.resourceIdentifierWithoutScale();
|
||||
} else {
|
||||
return this.scaledAssetPathInBundle();
|
||||
return this.scaledAssetURLInBundle();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,10 +105,10 @@ class AssetSourceResolver {
|
|||
|
||||
/**
|
||||
* Resolves to where the bundle is running from, with a scaled asset filename
|
||||
* E.g. '/sdcard/bundle/assets/AwesomeModule/icon@2x.png'
|
||||
* E.g. 'file:///sdcard/bundle/assets/AwesomeModule/icon@2x.png'
|
||||
*/
|
||||
scaledAssetPathInBundle(): ResolvedAssetSource {
|
||||
const path = this.bundlePath || '';
|
||||
scaledAssetURLInBundle(): ResolvedAssetSource {
|
||||
const path = this.bundleUrl || 'file://';
|
||||
return this.fromSource(path + getScaledAssetPath(this.asset));
|
||||
}
|
||||
|
||||
|
@ -129,9 +129,9 @@ class AssetSourceResolver {
|
|||
* E.g. 'file:///sdcard/AwesomeModule/drawable-mdpi/icon.png'
|
||||
*/
|
||||
drawableFolderInBundle(): ResolvedAssetSource {
|
||||
const path = this.bundlePath || '';
|
||||
const path = this.bundleUrl || 'file://';
|
||||
return this.fromSource(
|
||||
'file://' + path + getAssetPathInDrawableFolder(this.asset)
|
||||
path + getAssetPathInDrawableFolder(this.asset)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -122,7 +122,7 @@ describe('resolveAssetSource', () => {
|
|||
__packager_asset: true,
|
||||
width: 100,
|
||||
height: 200,
|
||||
uri: '/Path/To/Sample.app/assets/module/a/logo.png',
|
||||
uri: 'file:///Path/To/Sample.app/assets/module/a/logo.png',
|
||||
scale: 1,
|
||||
});
|
||||
});
|
||||
|
|
|
@ -19,7 +19,7 @@ const NativeModules = require('NativeModules');
|
|||
|
||||
import type { ResolvedAssetSource } from 'AssetSourceResolver';
|
||||
|
||||
let _customSourceTransformer, _serverURL, _bundleSourcePath;
|
||||
let _customSourceTransformer, _serverURL, _bundleSourceURL;
|
||||
|
||||
function getDevServerURL(): ?string {
|
||||
if (_serverURL === undefined) {
|
||||
|
@ -36,28 +36,28 @@ function getDevServerURL(): ?string {
|
|||
return _serverURL;
|
||||
}
|
||||
|
||||
function getBundleSourcePath(): ?string {
|
||||
if (_bundleSourcePath === undefined) {
|
||||
function getBundleSourceURL(): ?string {
|
||||
if (_bundleSourceURL === undefined) {
|
||||
const scriptURL = NativeModules.SourceCode.scriptURL;
|
||||
if (!scriptURL) {
|
||||
// scriptURL is falsy, we have nothing to go on here
|
||||
_bundleSourcePath = null;
|
||||
return _bundleSourcePath;
|
||||
_bundleSourceURL = null;
|
||||
return _bundleSourceURL;
|
||||
}
|
||||
if (scriptURL.startsWith('assets://')) {
|
||||
// running from within assets, no offline path to use
|
||||
_bundleSourcePath = null;
|
||||
return _bundleSourcePath;
|
||||
// android: running from within assets, no offline path to use
|
||||
_bundleSourceURL = null;
|
||||
return _bundleSourceURL;
|
||||
}
|
||||
if (scriptURL.startsWith('file://')) {
|
||||
// cut off the protocol
|
||||
_bundleSourcePath = scriptURL.substring(7, scriptURL.lastIndexOf('/') + 1);
|
||||
} else {
|
||||
_bundleSourcePath = scriptURL.substring(0, scriptURL.lastIndexOf('/') + 1);
|
||||
_bundleSourceURL = scriptURL.substring(0, scriptURL.lastIndexOf('/') + 1);
|
||||
if (!scriptURL.startsWith('file://')) {
|
||||
// Add file protocol in case we have an absolute file path and not a URL.
|
||||
// This shouldn't really be necessary. scriptURL should be a URL.
|
||||
_bundleSourceURL = 'file://' + _bundleSourceURL;
|
||||
}
|
||||
}
|
||||
|
||||
return _bundleSourcePath;
|
||||
return _bundleSourceURL;
|
||||
}
|
||||
|
||||
function setCustomSourceTransformer(
|
||||
|
@ -80,7 +80,7 @@ function resolveAssetSource(source: any): ?ResolvedAssetSource {
|
|||
return null;
|
||||
}
|
||||
|
||||
const resolver = new AssetSourceResolver(getDevServerURL(), getBundleSourcePath(), asset);
|
||||
const resolver = new AssetSourceResolver(getDevServerURL(), getBundleSourceURL(), asset);
|
||||
if (_customSourceTransformer) {
|
||||
return _customSourceTransformer(resolver);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue