2015-04-21 17:48:54 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule resolveAssetSource
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-04-22 23:31:13 +00:00
|
|
|
var PixelRatio = require('PixelRatio');
|
2015-04-21 17:48:54 +00:00
|
|
|
var SourceCode = require('NativeModules').SourceCode;
|
|
|
|
|
|
|
|
var _serverURL;
|
|
|
|
|
|
|
|
function getServerURL() {
|
|
|
|
if (_serverURL === undefined) {
|
|
|
|
var scriptURL = SourceCode.scriptURL;
|
2015-04-22 20:11:30 +00:00
|
|
|
var match = scriptURL && scriptURL.match(/^https?:\/\/.*?\//);
|
|
|
|
if (match) {
|
|
|
|
_serverURL = match[0];
|
2015-04-21 17:48:54 +00:00
|
|
|
} else {
|
|
|
|
_serverURL = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _serverURL;
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:31:13 +00:00
|
|
|
function pickScale(scales, deviceScale) {
|
|
|
|
// Packager guarantees that `scales` array is sorted
|
|
|
|
for (var i = 0; i < scales.length; i++) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-21 17:48:54 +00:00
|
|
|
function resolveAssetSource(source) {
|
2015-04-24 17:56:58 +00:00
|
|
|
if (!source || typeof source !== 'object') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-04-22 20:11:30 +00:00
|
|
|
if (!source.__packager_asset) {
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated assets are managed by Xcode for now,
|
|
|
|
// just returning image name as `uri`
|
|
|
|
// Examples:
|
|
|
|
// require('image!deprecatd_logo_example')
|
|
|
|
// require('./new-hotness-logo-example.png')
|
2015-04-21 17:48:54 +00:00
|
|
|
if (source.deprecated) {
|
|
|
|
return {
|
2015-04-22 20:11:30 +00:00
|
|
|
width: source.width,
|
|
|
|
height: source.height,
|
2015-04-21 17:48:54 +00:00
|
|
|
isStatic: true,
|
2015-04-22 20:11:30 +00:00
|
|
|
uri: source.name || source.uri, // TODO(frantic): remove uri
|
2015-04-21 17:48:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-04-22 20:11:30 +00:00
|
|
|
// TODO(frantic): currently httpServerLocation is used both as
|
|
|
|
// path in http URL and path within IPA. Should we have zipArchiveLocation?
|
|
|
|
var path = source.httpServerLocation;
|
|
|
|
if (path[0] === '/') {
|
|
|
|
path = path.substr(1);
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:31:13 +00:00
|
|
|
var scale = pickScale(source.scales, PixelRatio.get());
|
|
|
|
var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x';
|
|
|
|
|
|
|
|
var fileName = source.name + scaleSuffix + '.' + source.type;
|
2015-04-21 17:48:54 +00:00
|
|
|
var serverURL = getServerURL();
|
2015-04-22 20:11:30 +00:00
|
|
|
if (serverURL) {
|
|
|
|
return {
|
|
|
|
width: source.width,
|
|
|
|
height: source.height,
|
2015-04-22 23:31:13 +00:00
|
|
|
uri: serverURL + path + '/' + fileName +
|
2015-04-22 20:11:30 +00:00
|
|
|
'?hash=' + source.hash,
|
|
|
|
isStatic: false,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
width: source.width,
|
|
|
|
height: source.height,
|
2015-04-22 23:31:13 +00:00
|
|
|
uri: path + '/' + fileName,
|
2015-04-22 20:11:30 +00:00
|
|
|
isStatic: true,
|
|
|
|
};
|
2015-04-21 17:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = resolveAssetSource;
|
2015-04-22 23:31:13 +00:00
|
|
|
module.exports.pickScale = pickScale;
|