From 87245b2d40a865290fbeb4d8f5474fb8b5c1b891 Mon Sep 17 00:00:00 2001 From: Zahan Malkani Date: Sat, 5 Mar 2016 22:42:14 -0800 Subject: [PATCH] Support the scriptURLs observed on Android for asset source resolver Reviewed By: frantic Differential Revision: D3005791 fb-gh-sync-id: 99acb350c8c80ebe22687294a0069891686885fc shipit-source-id: 99acb350c8c80ebe22687294a0069891686885fc --- .../__tests__/resolveAssetSource-test.js | 30 ++++++++++++++++++- Libraries/Image/resolveAssetSource.js | 20 +++++++++---- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/Libraries/Image/__tests__/resolveAssetSource-test.js b/Libraries/Image/__tests__/resolveAssetSource-test.js index b406316a1..ae7ce3c5e 100644 --- a/Libraries/Image/__tests__/resolveAssetSource-test.js +++ b/Libraries/Image/__tests__/resolveAssetSource-test.js @@ -160,7 +160,7 @@ describe('resolveAssetSource', () => { }); }); }); - + describe('bundle was loaded from file on Android', () => { beforeEach(() => { NativeModules.SourceCode.scriptURL = @@ -189,6 +189,34 @@ describe('resolveAssetSource', () => { }); }); + describe('bundle was loaded from raw file on Android', () => { + beforeEach(() => { + NativeModules.SourceCode.scriptURL = + '/sdcard/Path/To/Simulator/main.bundle'; + Platform.OS = 'android'; + }); + + it('uses sideloaded image', () => { + expectResolvesAsset({ + __packager_asset: true, + fileSystemLocation: '/root/app/module/a', + httpServerLocation: '/assets/AwesomeModule/Subdir', + width: 100, + height: 200, + scales: [1], + hash: '5b6f00f', + name: '!@Logo#1_€', + type: 'png', + }, { + __packager_asset: true, + width: 100, + height: 200, + uri: 'file:///sdcard/Path/To/Simulator/drawable-mdpi/awesomemodule_subdir_logo1_.png', + scale: 1, + }); + }); + }); + }); describe('resolveAssetSource.pickScale', () => { diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index 6527e91df..e42b2d1da 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -47,12 +47,22 @@ function getDevServerURL() { function getOfflinePath() { if (_offlinePath === undefined) { - var scriptURL = SourceCode.scriptURL; - var match = scriptURL && scriptURL.match(/^file:\/\/(\/.*\/)/); - if (match) { - _offlinePath = match[1]; - } else { + const scriptURL = SourceCode.scriptURL; + if (!scriptURL) { + // scriptURL is falsy, we have nothing to go on here _offlinePath = ''; + return _offlinePath; + } + if (scriptURL.startsWith('assets://')) { + // running from within assets, no offline path to use + _offlinePath = ''; + return _offlinePath; + } + if (scriptURL.startsWith('file://')) { + // cut off the protocol + _offlinePath = scriptURL.substring(7, scriptURL.lastIndexOf('/') + 1); + } else { + _offlinePath = scriptURL.substring(0, scriptURL.lastIndexOf('/') + 1); } }