1010 */
1111'use strict' ;
1212
13+ var PixelRatio = require ( 'PixelRatio' ) ;
1314var SourceCode = require ( 'NativeModules' ) . SourceCode ;
1415
1516var _serverURL ;
@@ -28,6 +29,20 @@ function getServerURL() {
2829 return _serverURL ;
2930}
3031
32+ function pickScale ( scales , deviceScale ) {
33+ // Packager guarantees that `scales` array is sorted
34+ for ( var i = 0 ; i < scales . length ; i ++ ) {
35+ if ( scales [ i ] >= deviceScale ) {
36+ return scales [ i ] ;
37+ }
38+ }
39+
40+ // If nothing matches, device scale is larger than any available
41+ // scales, so we return the biggest one. Unless the array is empty,
42+ // in which case we default to 1
43+ return scales [ scales . length - 1 ] || 1 ;
44+ }
45+
3146// TODO(frantic):
3247// * Pick best scale and append @Nx to file path
3348// * We are currently using httpServerLocation for both http and in-app bundle
@@ -57,20 +72,24 @@ function resolveAssetSource(source) {
5772 path = path . substr ( 1 ) ;
5873 }
5974
75+ var scale = pickScale ( source . scales , PixelRatio . get ( ) ) ;
76+ var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x' ;
77+
78+ var fileName = source . name + scaleSuffix + '.' + source . type ;
6079 var serverURL = getServerURL ( ) ;
6180 if ( serverURL ) {
6281 return {
6382 width : source . width ,
6483 height : source . height ,
65- uri : serverURL + path + '/' + source . name + '.' + source . type +
84+ uri : serverURL + path + '/' + fileName +
6685 '?hash=' + source . hash ,
6786 isStatic : false ,
6887 } ;
6988 } else {
7089 return {
7190 width : source . width ,
7291 height : source . height ,
73- uri : path + '/' + source . name + '.' + source . type ,
92+ uri : path + '/' + fileName ,
7493 isStatic : true ,
7594 } ;
7695 }
@@ -79,3 +98,4 @@ function resolveAssetSource(source) {
7998}
8099
81100module . exports = resolveAssetSource ;
101+ module . exports . pickScale = pickScale ;
0 commit comments