2016-03-16 03:19:38 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-03-16 03:19:38 +00:00
|
|
|
*
|
|
|
|
* @flow
|
2018-01-15 03:32:26 +00:00
|
|
|
* @format
|
2016-03-16 03:19:38 +00:00
|
|
|
*/
|
2016-11-21 01:49:21 +00:00
|
|
|
'use strict';
|
2016-03-16 03:19:38 +00:00
|
|
|
|
2018-02-13 02:43:21 +00:00
|
|
|
export type ResolvedAssetSource = {|
|
|
|
|
+__packager_asset: boolean,
|
|
|
|
+width: ?number,
|
|
|
|
+height: ?number,
|
|
|
|
+uri: string,
|
|
|
|
+scale: number,
|
|
|
|
|};
|
2016-03-16 03:19:38 +00:00
|
|
|
|
2018-01-15 03:32:26 +00:00
|
|
|
import type {PackagerAsset} from 'AssetRegistry';
|
2016-03-16 03:19:38 +00:00
|
|
|
|
|
|
|
const PixelRatio = require('PixelRatio');
|
|
|
|
const Platform = require('Platform');
|
|
|
|
|
|
|
|
const assetPathUtils = require('../../local-cli/bundle/assetPathUtils');
|
2016-03-16 13:56:18 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2016-03-16 03:19:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a path like 'assets/AwesomeModule/icon@2x.png'
|
|
|
|
*/
|
|
|
|
function getScaledAssetPath(asset): string {
|
2018-05-10 22:44:52 +00:00
|
|
|
const scale = AssetSourceResolver.pickScale(asset.scales, PixelRatio.get());
|
|
|
|
const scaleSuffix = scale === 1 ? '' : '@' + scale + 'x';
|
|
|
|
const assetDir = assetPathUtils.getBasePath(asset);
|
2016-03-16 03:19:38 +00:00
|
|
|
return assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a path like 'drawable-mdpi/icon.png'
|
|
|
|
*/
|
|
|
|
function getAssetPathInDrawableFolder(asset): string {
|
2018-05-10 22:44:52 +00:00
|
|
|
const scale = AssetSourceResolver.pickScale(asset.scales, PixelRatio.get());
|
2018-05-11 02:06:46 +00:00
|
|
|
const drawbleFolder = assetPathUtils.getAndroidResourceFolderName(
|
|
|
|
asset,
|
|
|
|
scale,
|
|
|
|
);
|
2018-05-10 22:44:52 +00:00
|
|
|
const fileName = assetPathUtils.getAndroidResourceIdentifier(asset);
|
2016-03-16 03:19:38 +00:00
|
|
|
return drawbleFolder + '/' + fileName + '.' + asset.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AssetSourceResolver {
|
|
|
|
serverUrl: ?string;
|
2017-11-08 01:43:35 +00:00
|
|
|
// where the jsbundle is being run from
|
|
|
|
jsbundleUrl: ?string;
|
2016-03-16 03:19:38 +00:00
|
|
|
// the asset to resolve
|
|
|
|
asset: PackagerAsset;
|
|
|
|
|
2018-01-15 03:32:26 +00:00
|
|
|
constructor(serverUrl: ?string, jsbundleUrl: ?string, asset: PackagerAsset) {
|
2016-03-16 03:19:38 +00:00
|
|
|
this.serverUrl = serverUrl;
|
2017-11-08 01:43:35 +00:00
|
|
|
this.jsbundleUrl = jsbundleUrl;
|
2016-03-16 03:19:38 +00:00
|
|
|
this.asset = asset;
|
|
|
|
}
|
|
|
|
|
|
|
|
isLoadedFromServer(): boolean {
|
|
|
|
return !!this.serverUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
isLoadedFromFileSystem(): boolean {
|
2017-11-13 06:48:56 +00:00
|
|
|
return !!(this.jsbundleUrl && this.jsbundleUrl.startsWith('file://'));
|
2016-03-16 03:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defaultAsset(): ResolvedAssetSource {
|
|
|
|
if (this.isLoadedFromServer()) {
|
|
|
|
return this.assetServerURL();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Platform.OS === 'android') {
|
2018-01-15 03:32:26 +00:00
|
|
|
return this.isLoadedFromFileSystem()
|
|
|
|
? this.drawableFolderInBundle()
|
|
|
|
: this.resourceIdentifierWithoutScale();
|
2016-03-16 03:19:38 +00:00
|
|
|
} else {
|
2017-11-08 20:00:40 +00:00
|
|
|
return this.scaledAssetURLNearBundle();
|
2016-03-16 03:19:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an absolute URL which can be used to fetch the asset
|
|
|
|
* from the devserver
|
|
|
|
*/
|
|
|
|
assetServerURL(): ResolvedAssetSource {
|
|
|
|
invariant(!!this.serverUrl, 'need server to load from');
|
|
|
|
return this.fromSource(
|
2018-01-15 03:32:26 +00:00
|
|
|
this.serverUrl +
|
|
|
|
getScaledAssetPath(this.asset) +
|
|
|
|
'?platform=' +
|
|
|
|
Platform.OS +
|
|
|
|
'&hash=' +
|
|
|
|
this.asset.hash,
|
2016-03-16 03:19:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves to just the scaled asset filename
|
|
|
|
* E.g. 'assets/AwesomeModule/icon@2x.png'
|
|
|
|
*/
|
|
|
|
scaledAssetPath(): ResolvedAssetSource {
|
|
|
|
return this.fromSource(getScaledAssetPath(this.asset));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-08 20:00:40 +00:00
|
|
|
* Resolves to where the bundle is running from, with a scaled asset filename
|
2017-09-08 17:41:36 +00:00
|
|
|
* E.g. 'file:///sdcard/bundle/assets/AwesomeModule/icon@2x.png'
|
2016-03-16 03:19:38 +00:00
|
|
|
*/
|
2017-11-08 20:00:40 +00:00
|
|
|
scaledAssetURLNearBundle(): ResolvedAssetSource {
|
2017-11-08 01:43:35 +00:00
|
|
|
const path = this.jsbundleUrl || 'file://';
|
2016-03-16 03:19:38 +00:00
|
|
|
return this.fromSource(path + getScaledAssetPath(this.asset));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default location of assets bundled with the app, located by
|
|
|
|
* resource identifier
|
|
|
|
* The Android resource system picks the correct scale.
|
|
|
|
* E.g. 'assets_awesomemodule_icon'
|
|
|
|
*/
|
|
|
|
resourceIdentifierWithoutScale(): ResolvedAssetSource {
|
2018-01-15 03:32:26 +00:00
|
|
|
invariant(
|
|
|
|
Platform.OS === 'android',
|
|
|
|
'resource identifiers work on Android',
|
|
|
|
);
|
|
|
|
return this.fromSource(
|
|
|
|
assetPathUtils.getAndroidResourceIdentifier(this.asset),
|
|
|
|
);
|
2016-03-16 03:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the jsbundle is running from a sideload location, this resolves assets
|
|
|
|
* relative to its location
|
|
|
|
* E.g. 'file:///sdcard/AwesomeModule/drawable-mdpi/icon.png'
|
|
|
|
*/
|
|
|
|
drawableFolderInBundle(): ResolvedAssetSource {
|
2017-11-08 01:43:35 +00:00
|
|
|
const path = this.jsbundleUrl || 'file://';
|
2018-01-15 03:32:26 +00:00
|
|
|
return this.fromSource(path + getAssetPathInDrawableFolder(this.asset));
|
2016-03-16 03:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fromSource(source: string): ResolvedAssetSource {
|
|
|
|
return {
|
|
|
|
__packager_asset: true,
|
|
|
|
width: this.asset.width,
|
|
|
|
height: this.asset.height,
|
|
|
|
uri: source,
|
|
|
|
scale: AssetSourceResolver.pickScale(this.asset.scales, PixelRatio.get()),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static pickScale(scales: Array<number>, deviceScale: number): number {
|
|
|
|
// Packager guarantees that `scales` array is sorted
|
2018-05-10 22:44:52 +00:00
|
|
|
for (let i = 0; i < scales.length; i++) {
|
2016-03-16 03:19:38 +00:00
|
|
|
if (scales[i] >= deviceScale) {
|
|
|
|
return scales[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If nothing matches, device scale is larger than any available
|
|
|
|
// scales, so we return the biggest one. Unless the array is empty,
|
|
|
|
// in which case we default to 1
|
|
|
|
return scales[scales.length - 1] || 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-15 03:32:26 +00:00
|
|
|
module.exports = AssetSourceResolver;
|