From a28bef43bd20f46a222e8b5037ce054db28fe1b9 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Thu, 3 Dec 2015 01:28:56 -0800 Subject: [PATCH 01/17] Load assets from same folder as JSbundle (Android) --- Libraries/Image/resolveAssetSource.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index 638b72ce06e4..9f2e7ad4d23e 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -63,14 +63,23 @@ function getOfflinePath() { */ function getPathInArchive(asset) { if (Platform.OS === 'android') { - var assetDir = getBasePath(asset); - // E.g. 'assets_awesomemodule_icon' - // The Android resource system picks the correct scale. - return (assetDir + '/' + asset.name) - .toLowerCase() - .replace(/\//g, '_') // Encode folder structure in file name - .replace(/([^a-z0-9_])/g, '') // Remove illegal chars - .replace(/^assets_/, ''); // Remove "assets_" prefix + var scriptURL = SourceCode.scriptURL; + var match = scriptURL && scriptURL.match(/^\/.*\//); + if (match) { + // scriptURL has no scheme, + // E.g. /sdcard/path/to/file.jsbundle or /data/data/path/to/file.jsbundle + return 'file://' + match[0] + getScaledAssetPath(asset); + } else { + // scriptURL has a scheme, E.g. assets://file.jsbundle + var assetDir = getBasePath(asset); + // E.g. 'assets_awesomemodule_icon' + // The Android resource system picks the correct scale. + return (assetDir + '/' + asset.name) + .toLowerCase() + .replace(/\//g, '_') // Encode folder structure in file name + .replace(/([^a-z0-9_])/g, '') // Remove illegal chars + .replace(/^assets_/, ''); // Remove "assets_" prefix + } } else { // E.g. 'assets/AwesomeModule/icon@2x.png' return getOfflinePath() + getScaledAssetPath(asset); From 72c7265ba3d924a8ae5bd438ef623faf7d14ac20 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Thu, 3 Dec 2015 01:35:40 -0800 Subject: [PATCH 02/17] Update resolveAssetSource.js --- Libraries/Image/resolveAssetSource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index 9f2e7ad4d23e..d9ebc00e5707 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -68,7 +68,7 @@ function getPathInArchive(asset) { if (match) { // scriptURL has no scheme, // E.g. /sdcard/path/to/file.jsbundle or /data/data/path/to/file.jsbundle - return 'file://' + match[0] + getScaledAssetPath(asset); + return 'file://' + match[0] + asset.name; } else { // scriptURL has a scheme, E.g. assets://file.jsbundle var assetDir = getBasePath(asset); From 670ada020e6baa35f36bbe9293b22ff03ba1ba41 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Thu, 3 Dec 2015 01:39:54 -0800 Subject: [PATCH 03/17] Update resolveAssetSource.js --- Libraries/Image/resolveAssetSource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index d9ebc00e5707..dea4f79b3e6e 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -68,7 +68,7 @@ function getPathInArchive(asset) { if (match) { // scriptURL has no scheme, // E.g. /sdcard/path/to/file.jsbundle or /data/data/path/to/file.jsbundle - return 'file://' + match[0] + asset.name; + return 'file://' + match[0] + getBasePath(asset) + '/' + asset.name; } else { // scriptURL has a scheme, E.g. assets://file.jsbundle var assetDir = getBasePath(asset); From f345a15093dee8b883aa63f590661955d6a54844 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Thu, 3 Dec 2015 01:42:53 -0800 Subject: [PATCH 04/17] Update resolveAssetSource.js --- Libraries/Image/resolveAssetSource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index dea4f79b3e6e..9f2e7ad4d23e 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -68,7 +68,7 @@ function getPathInArchive(asset) { if (match) { // scriptURL has no scheme, // E.g. /sdcard/path/to/file.jsbundle or /data/data/path/to/file.jsbundle - return 'file://' + match[0] + getBasePath(asset) + '/' + asset.name; + return 'file://' + match[0] + getScaledAssetPath(asset); } else { // scriptURL has a scheme, E.g. assets://file.jsbundle var assetDir = getBasePath(asset); From 83e628d8f97dc1a4694040aef757c504cc9db049 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Thu, 3 Dec 2015 18:40:29 -0800 Subject: [PATCH 05/17] Update resolveAssetSource-test.js --- .../__tests__/resolveAssetSource-test.js | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/Libraries/Image/__tests__/resolveAssetSource-test.js b/Libraries/Image/__tests__/resolveAssetSource-test.js index 81fdb2b08dc9..4bab6ddcc03b 100644 --- a/Libraries/Image/__tests__/resolveAssetSource-test.js +++ b/Libraries/Image/__tests__/resolveAssetSource-test.js @@ -132,10 +132,10 @@ describe('resolveAssetSource', () => { }); }); - describe('bundle was loaded from file on Android', () => { + describe('bundle was loaded from assets on Android', () => { beforeEach(() => { NativeModules.SourceCode.scriptURL = - 'file:///Path/To/Simulator/main.bundle'; + 'assets://Path/To/Simulator/main.bundle'; Platform.OS = 'android'; }); @@ -159,6 +159,34 @@ describe('resolveAssetSource', () => { }); }); }); + + describe('bundle was loaded from file on Android', () => { + beforeEach(() => { + NativeModules.SourceCode.scriptURL = + '/sdcard/Path/To/Simulator/main.bundle'; + Platform.OS = 'android'; + }); + + it('uses pre-packed 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/assets/AwesomeModule/Subdir/!@Logo#1_€.png', + scale: 1, + }); + }); + }); }); From 8c7da3304bcff09432586cc23cfc5adf4ca8c11e Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 4 Dec 2015 01:02:19 -0800 Subject: [PATCH 06/17] Update resolveAssetSource.js --- Libraries/Image/resolveAssetSource.js | 39 ++++++++++++--------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index 9f2e7ad4d23e..ef599488edce 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -47,9 +47,9 @@ function getDevServerURL() { function getOfflinePath() { if (_offlinePath === undefined) { var scriptURL = SourceCode.scriptURL; - var match = scriptURL && scriptURL.match(/^file:\/\/(\/.*\/)/); + var match = scriptURL && scriptURL.match(/^(file:\/\/)?(\/.*\/)/); if (match) { - _offlinePath = match[1]; + _offlinePath = (Platform.OS === 'android' ? 'file://' : '') + match[2]; } else { _offlinePath = ''; } @@ -58,31 +58,26 @@ function getOfflinePath() { return _offlinePath; } +function getResourceIdentifier(asset) { + var assetDir = getBasePath(asset); + // E.g. 'assets_awesomemodule_icon' + // The Android resource system picks the correct scale. + return (assetDir + '/' + asset.name) + .toLowerCase() + .replace(/\//g, '_') // Encode folder structure in file name + .replace(/([^a-z0-9_])/g, '') // Remove illegal chars + .replace(/^assets_/, ''); // Remove "assets_" prefix +} + /** * Returns the path at which the asset can be found in the archive */ function getPathInArchive(asset) { - if (Platform.OS === 'android') { - var scriptURL = SourceCode.scriptURL; - var match = scriptURL && scriptURL.match(/^\/.*\//); - if (match) { - // scriptURL has no scheme, - // E.g. /sdcard/path/to/file.jsbundle or /data/data/path/to/file.jsbundle - return 'file://' + match[0] + getScaledAssetPath(asset); - } else { - // scriptURL has a scheme, E.g. assets://file.jsbundle - var assetDir = getBasePath(asset); - // E.g. 'assets_awesomemodule_icon' - // The Android resource system picks the correct scale. - return (assetDir + '/' + asset.name) - .toLowerCase() - .replace(/\//g, '_') // Encode folder structure in file name - .replace(/([^a-z0-9_])/g, '') // Remove illegal chars - .replace(/^assets_/, ''); // Remove "assets_" prefix - } + var offlinePath = getOfflinePath(); + if (Platform.OS === 'ios' || offlinePath) { + return offlinePath + getScaledAssetPath(asset); } else { - // E.g. 'assets/AwesomeModule/icon@2x.png' - return getOfflinePath() + getScaledAssetPath(asset); + return getResourceIdentifier(asset); } } From 8bec10339d322dc3766b2349bcd81d12fc705322 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 4 Dec 2015 19:18:32 -0800 Subject: [PATCH 07/17] Update test --- Libraries/Image/__tests__/resolveAssetSource-test.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Libraries/Image/__tests__/resolveAssetSource-test.js b/Libraries/Image/__tests__/resolveAssetSource-test.js index 4bab6ddcc03b..160ffe00c26e 100644 --- a/Libraries/Image/__tests__/resolveAssetSource-test.js +++ b/Libraries/Image/__tests__/resolveAssetSource-test.js @@ -10,7 +10,8 @@ jest .dontMock('AssetRegistry') - .dontMock('../resolveAssetSource'); + .dontMock('../resolveAssetSource') + .dontMock('../../../local-cli/bundle/getAssetDestPathAndroid'); var AssetRegistry = require('AssetRegistry'); var Platform = require('Platform'); @@ -182,7 +183,7 @@ describe('resolveAssetSource', () => { __packager_asset: true, width: 100, height: 200, - uri: 'file:///sdcard/Path/To/Simulator/assets/AwesomeModule/Subdir/!@Logo#1_€.png', + uri: 'file:///sdcard/Path/To/Simulator/drawable-mdpi/awesomemodule_subdir_logo1_.png', scale: 1, }); }); From 398f2c32374ce784e360221e0b59a05847c21dfb Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 4 Dec 2015 19:19:08 -0800 Subject: [PATCH 08/17] Resolve android path same way bundler does --- Libraries/Image/resolveAssetSource.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index ef599488edce..56529e606cb0 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -25,6 +25,7 @@ var AssetRegistry = require('AssetRegistry'); var PixelRatio = require('PixelRatio'); var Platform = require('Platform'); var SourceCode = require('NativeModules').SourceCode; +var getAssetDestPathAndroid = require('../../local-cli/bundle/getAssetDestPathAndroid'); var _serverURL, _offlinePath; @@ -77,6 +78,7 @@ function getPathInArchive(asset) { if (Platform.OS === 'ios' || offlinePath) { return offlinePath + getScaledAssetPath(asset); } else { + // In Android, image assets are belong to the drawables. return getResourceIdentifier(asset); } } @@ -108,9 +110,13 @@ function getBasePath(asset) { */ function getScaledAssetPath(asset) { var scale = pickScale(asset.scales, PixelRatio.get()); - var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x'; - var assetDir = getBasePath(asset); - return assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type; + if (Platform.OS === 'ios') { + var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x'; + var assetDir = getBasePath(asset); + return assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type; + } else { + return getAssetDestPathAndroid(asset, scale); + } } function pickScale(scales: Array, deviceScale: number): number { From 076649e858a09ebeb19ce638786fdef64c34bb95 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 4 Dec 2015 22:54:15 -0800 Subject: [PATCH 09/17] Add path to runtime dependency --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index d2249d02e799..c4382cb5e030 100644 --- a/package.json +++ b/package.json @@ -102,6 +102,7 @@ "node-fetch": "^1.3.3", "opn": "^3.0.2", "optimist": "^0.6.1", + "path": "^0.12.7", "progress": "^1.1.8", "promise": "^7.0.4", "react-haste": "^0.14.2", From 682db02b468d427f0828fd9e3fc694a3373e7f08 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Mon, 7 Dec 2015 10:01:49 -0800 Subject: [PATCH 10/17] reverse if-else logic --- Libraries/Image/resolveAssetSource.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index 56529e606cb0..b7ee6f43c604 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -75,11 +75,11 @@ function getResourceIdentifier(asset) { */ function getPathInArchive(asset) { var offlinePath = getOfflinePath(); - if (Platform.OS === 'ios' || offlinePath) { - return offlinePath + getScaledAssetPath(asset); - } else { + if (platform === 'android' && !offlinePath) { // In Android, image assets are belong to the drawables. return getResourceIdentifier(asset); + } else { + return offlinePath + getScaledAssetPath(asset); } } From cb808a5553416e09bf6dd78054ecf521b0d87a21 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Mon, 7 Dec 2015 11:33:14 -0800 Subject: [PATCH 11/17] refactor --- .../__tests__/resolveAssetSource-test.js | 2 +- Libraries/Image/resolveAssetSource.js | 36 +++--------- .../__tests__/getAssetDestPathAndroid-test.js | 4 +- local-cli/bundle/assetPathUtils.js | 57 +++++++++++++++++++ local-cli/bundle/getAssetDestPathAndroid.js | 29 +--------- 5 files changed, 71 insertions(+), 57 deletions(-) create mode 100644 local-cli/bundle/assetPathUtils.js diff --git a/Libraries/Image/__tests__/resolveAssetSource-test.js b/Libraries/Image/__tests__/resolveAssetSource-test.js index 160ffe00c26e..9ac46f57224c 100644 --- a/Libraries/Image/__tests__/resolveAssetSource-test.js +++ b/Libraries/Image/__tests__/resolveAssetSource-test.js @@ -11,7 +11,7 @@ jest .dontMock('AssetRegistry') .dontMock('../resolveAssetSource') - .dontMock('../../../local-cli/bundle/getAssetDestPathAndroid'); + .dontMock('../../../local-cli/bundle/assetPathUtils'); var AssetRegistry = require('AssetRegistry'); var Platform = require('Platform'); diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index b7ee6f43c604..613467a7466e 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -25,7 +25,7 @@ var AssetRegistry = require('AssetRegistry'); var PixelRatio = require('PixelRatio'); var Platform = require('Platform'); var SourceCode = require('NativeModules').SourceCode; -var getAssetDestPathAndroid = require('../../local-cli/bundle/getAssetDestPathAndroid'); +var assetPathUtils = require('../../local-cli/bundle/assetPathUtils'); var _serverURL, _offlinePath; @@ -59,25 +59,14 @@ function getOfflinePath() { return _offlinePath; } -function getResourceIdentifier(asset) { - var assetDir = getBasePath(asset); - // E.g. 'assets_awesomemodule_icon' - // The Android resource system picks the correct scale. - return (assetDir + '/' + asset.name) - .toLowerCase() - .replace(/\//g, '_') // Encode folder structure in file name - .replace(/([^a-z0-9_])/g, '') // Remove illegal chars - .replace(/^assets_/, ''); // Remove "assets_" prefix -} - /** * Returns the path at which the asset can be found in the archive */ function getPathInArchive(asset) { var offlinePath = getOfflinePath(); - if (platform === 'android' && !offlinePath) { + if (Platform.OS === 'android' && !offlinePath) { // In Android, image assets are belong to the drawables. - return getResourceIdentifier(asset); + return assetPathUtils.getAndroidResourceIdentifier(asset); } else { return offlinePath + getScaledAssetPath(asset); } @@ -92,19 +81,6 @@ function getPathOnDevserver(devServerUrl, asset) { '&hash=' + asset.hash; } -/** - * Returns a path like 'assets/AwesomeModule' - */ -function getBasePath(asset) { - // TODO(frantic): currently httpServerLocation is used both as - // path in http URL and path within IPA. Should we have zipArchiveLocation? - var path = asset.httpServerLocation; - if (path[0] === '/') { - path = path.substr(1); - } - return path; -} - /** * Returns a path like 'assets/AwesomeModule/icon@2x.png' */ @@ -112,10 +88,12 @@ function getScaledAssetPath(asset) { var scale = pickScale(asset.scales, PixelRatio.get()); if (Platform.OS === 'ios') { var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x'; - var assetDir = getBasePath(asset); + var assetDir = assetPathUtils.getBasePath(asset); return assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type; } else { - return getAssetDestPathAndroid(asset, scale); + const androidFolder = assetPathUtils.getAndroidDrawableFolderName(asset, scale); + const fileName = assetPathUtils.getAndroidResourceIdentifier(asset); + return androidFolder + '/' + fileName + '.' + asset.type; } } diff --git a/local-cli/bundle/__tests__/getAssetDestPathAndroid-test.js b/local-cli/bundle/__tests__/getAssetDestPathAndroid-test.js index 1613b5b3656a..3f58299549cc 100644 --- a/local-cli/bundle/__tests__/getAssetDestPathAndroid-test.js +++ b/local-cli/bundle/__tests__/getAssetDestPathAndroid-test.js @@ -8,7 +8,9 @@ */ 'use strict'; -jest.dontMock('../getAssetDestPathAndroid'); +jest + .dontMock('../getAssetDestPathAndroid') + .dontMock('../assetPathUtils'); const getAssetDestPathAndroid = require('../getAssetDestPathAndroid'); diff --git a/local-cli/bundle/assetPathUtils.js b/local-cli/bundle/assetPathUtils.js new file mode 100644 index 000000000000..52d3a032d314 --- /dev/null +++ b/local-cli/bundle/assetPathUtils.js @@ -0,0 +1,57 @@ +/** + * 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. + */ +'use strict'; + +function getAndroidAssetSuffix(scale) { + switch (scale) { + case 0.75: return 'ldpi'; + case 1: return 'mdpi'; + case 1.5: return 'hdpi'; + case 2: return 'xhdpi'; + case 3: return 'xxhdpi'; + case 4: return 'xxxhdpi'; + } +} + +function getAndroidDrawableFolderName(asset, scale) { + var suffix = getAndroidAssetSuffix(scale); + if (!suffix) { + throw new Error( + 'Don\'t know which android drawable suffix to use for asset: ' + + JSON.stringify(asset) + ); + } + const androidFolder = 'drawable-' + suffix; + return androidFolder; +} + +function getAndroidResourceIdentifier(asset) { + var folderPath = getBasePath(asset); + const resourceIdentifier = (folderPath + '/' + asset.name) + .toLowerCase() + .replace(/\//g, '_') // Encode folder structure in file name + .replace(/([^a-z0-9_])/g, '') // Remove illegal chars + .replace(/^assets_/, ''); // Remove "assets_" prefix + return resourceIdentifier; +} + +function getBasePath(asset) { + var basePath = asset.httpServerLocation; + if (basePath[0] === '/') { + basePath = basePath.substr(1); + } + return basePath; +} + +module.exports = { + getAndroidAssetSuffix: getAndroidAssetSuffix, + getAndroidDrawableFolderName: getAndroidDrawableFolderName, + getAndroidResourceIdentifier: getAndroidResourceIdentifier, + getBasePath: getBasePath +}; \ No newline at end of file diff --git a/local-cli/bundle/getAssetDestPathAndroid.js b/local-cli/bundle/getAssetDestPathAndroid.js index 3e039f54fd5e..f462ba5e81d1 100644 --- a/local-cli/bundle/getAssetDestPathAndroid.js +++ b/local-cli/bundle/getAssetDestPathAndroid.js @@ -9,34 +9,11 @@ 'use strict'; const path = require('path'); - -function getAndroidAssetSuffix(scale) { - switch (scale) { - case 0.75: return 'ldpi'; - case 1: return 'mdpi'; - case 1.5: return 'hdpi'; - case 2: return 'xhdpi'; - case 3: return 'xxhdpi'; - case 4: return 'xxxhdpi'; - } -} +const assetPathUtils = require('./assetPathUtils'); function getAssetDestPathAndroid(asset, scale) { - var suffix = getAndroidAssetSuffix(scale); - if (!suffix) { - throw new Error( - 'Don\'t know which android drawable suffix to use for asset: ' + - JSON.stringify(asset) - ); - } - const androidFolder = 'drawable-' + suffix; - // TODO: reuse this logic from https://fburl.com/151101135 - const fileName = (asset.httpServerLocation.substr(1) + '/' + asset.name) - .toLowerCase() - .replace(/\//g, '_') // Encode folder structure in file name - .replace(/([^a-z0-9_])/g, '') // Remove illegal chars - .replace(/^assets_/, ''); // Remove "assets_" prefix - + const androidFolder = assetPathUtils.getAndroidDrawableFolderName(asset, scale); + const fileName = assetPathUtils.getAndroidResourceIdentifier(asset); return path.join(androidFolder, fileName + '.' + asset.type); } From 2065ab316b2d8961f79d437f1b32b08bc9b8e740 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Mon, 7 Dec 2015 11:37:07 -0800 Subject: [PATCH 12/17] Path dependency no longer needed. --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index c4382cb5e030..d2249d02e799 100644 --- a/package.json +++ b/package.json @@ -102,7 +102,6 @@ "node-fetch": "^1.3.3", "opn": "^3.0.2", "optimist": "^0.6.1", - "path": "^0.12.7", "progress": "^1.1.8", "promise": "^7.0.4", "react-haste": "^0.14.2", From 5c72fa2f45f6c856267a536f0b1460be6da9ac30 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Tue, 8 Dec 2015 10:58:31 -0800 Subject: [PATCH 13/17] Add function comment --- Libraries/Image/resolveAssetSource.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index 613467a7466e..bcb22f72ccf9 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -82,7 +82,8 @@ function getPathOnDevserver(devServerUrl, asset) { } /** - * Returns a path like 'assets/AwesomeModule/icon@2x.png' + * Returns a path like 'assets/AwesomeModule/icon@2x.png' on iOS + * or 'drawable_ldpi/awesomemodule_icon.png' on Android */ function getScaledAssetPath(asset) { var scale = pickScale(asset.scales, PixelRatio.get()); From e43da34e08929071532cf86b0d566ff20eda9f28 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Sun, 13 Dec 2015 23:04:48 -0800 Subject: [PATCH 14/17] frantic feedback --- .../__tests__/resolveAssetSource-test.js | 2 +- Libraries/Image/resolveAssetSource.js | 39 ++++++++++++------- .../facebook/react/bridge/JSBundleLoader.java | 2 +- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Libraries/Image/__tests__/resolveAssetSource-test.js b/Libraries/Image/__tests__/resolveAssetSource-test.js index 9ac46f57224c..b406316a1a90 100644 --- a/Libraries/Image/__tests__/resolveAssetSource-test.js +++ b/Libraries/Image/__tests__/resolveAssetSource-test.js @@ -164,7 +164,7 @@ describe('resolveAssetSource', () => { describe('bundle was loaded from file on Android', () => { beforeEach(() => { NativeModules.SourceCode.scriptURL = - '/sdcard/Path/To/Simulator/main.bundle'; + 'file:///sdcard/Path/To/Simulator/main.bundle'; Platform.OS = 'android'; }); diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index bcb22f72ccf9..0da6dac7f682 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -48,9 +48,9 @@ function getDevServerURL() { function getOfflinePath() { if (_offlinePath === undefined) { var scriptURL = SourceCode.scriptURL; - var match = scriptURL && scriptURL.match(/^(file:\/\/)?(\/.*\/)/); + var match = scriptURL && scriptURL.match(/^file:\/\/(\/.*\/)/); if (match) { - _offlinePath = (Platform.OS === 'android' ? 'file://' : '') + match[2]; + _offlinePath = match[1]; } else { _offlinePath = ''; } @@ -64,10 +64,16 @@ function getOfflinePath() { */ function getPathInArchive(asset) { var offlinePath = getOfflinePath(); - if (Platform.OS === 'android' && !offlinePath) { - // In Android, image assets are belong to the drawables. + if (Platform.OS === 'android') { + if (offlinePath) { + // E.g. 'file:///sdcard/AwesomeModule/drawable-mdpi/icon.png' + return "file://" + offlinePath + getAssetPathInDrawableFolder(asset); + } + // E.g. 'assets_awesomemodule_icon' + // The Android resource system picks the correct scale. return assetPathUtils.getAndroidResourceIdentifier(asset); } else { + // E.g. '/assets/AwesomeModule/icon@2x.png' return offlinePath + getScaledAssetPath(asset); } } @@ -82,20 +88,23 @@ function getPathOnDevserver(devServerUrl, asset) { } /** - * Returns a path like 'assets/AwesomeModule/icon@2x.png' on iOS - * or 'drawable_ldpi/awesomemodule_icon.png' on Android + * Returns a path like 'assets/AwesomeModule/icon@2x.png' */ function getScaledAssetPath(asset) { var scale = pickScale(asset.scales, PixelRatio.get()); - if (Platform.OS === 'ios') { - var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x'; - var assetDir = assetPathUtils.getBasePath(asset); - return assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type; - } else { - const androidFolder = assetPathUtils.getAndroidDrawableFolderName(asset, scale); - const fileName = assetPathUtils.getAndroidResourceIdentifier(asset); - return androidFolder + '/' + fileName + '.' + asset.type; - } + var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x'; + var assetDir = assetPathUtils.getBasePath(asset); + return assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type; +} + +/** + * Returns a path like 'drawable-mdpi/icon.png' + */ +function getAssetPathInDrawableFolder(asset) { + var scale = pickScale(asset.scales, PixelRatio.get()); + const drawbleFolder = assetPathUtils.getAndroidDrawableFolderName(asset, scale); + const fileName = assetPathUtils.getAndroidResourceIdentifier(asset); + return drawbleFolder + '/' + fileName + '.' + asset.type; } function pickScale(scales: Array, deviceScale: number): number { diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java index c784cd00791f..56588a650b4a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java @@ -37,7 +37,7 @@ public void loadScript(ReactBridge bridge) { @Override public String getSourceUrl() { - return fileName; + return (fileName.startsWith("assets://") ? "" : "file://") + fileName; } }; } From 34b5d1bbfc872f862a60979241c0e0bcaf159cc1 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Sun, 13 Dec 2015 23:14:01 -0800 Subject: [PATCH 15/17] single quotes --- Libraries/Image/resolveAssetSource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index 0da6dac7f682..07f1a4d09ba1 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -67,7 +67,7 @@ function getPathInArchive(asset) { if (Platform.OS === 'android') { if (offlinePath) { // E.g. 'file:///sdcard/AwesomeModule/drawable-mdpi/icon.png' - return "file://" + offlinePath + getAssetPathInDrawableFolder(asset); + return 'file://' + offlinePath + getAssetPathInDrawableFolder(asset); } // E.g. 'assets_awesomemodule_icon' // The Android resource system picks the correct scale. From 882533d058c4908a4ae6f4083fb54a3f629bfeae Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 18 Dec 2015 11:33:19 -0800 Subject: [PATCH 16/17] fix nit --- local-cli/bundle/assetPathUtils.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/local-cli/bundle/assetPathUtils.js b/local-cli/bundle/assetPathUtils.js index 52d3a032d314..cf14b2c62cf0 100644 --- a/local-cli/bundle/assetPathUtils.js +++ b/local-cli/bundle/assetPathUtils.js @@ -33,12 +33,11 @@ function getAndroidDrawableFolderName(asset, scale) { function getAndroidResourceIdentifier(asset) { var folderPath = getBasePath(asset); - const resourceIdentifier = (folderPath + '/' + asset.name) + return (folderPath + '/' + asset.name) .toLowerCase() .replace(/\//g, '_') // Encode folder structure in file name .replace(/([^a-z0-9_])/g, '') // Remove illegal chars .replace(/^assets_/, ''); // Remove "assets_" prefix - return resourceIdentifier; } function getBasePath(asset) { From 4ddb13fff74a0c7af57f1ac3671608f3a0744889 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Wed, 23 Dec 2015 16:46:21 -0800 Subject: [PATCH 17/17] const -> var --- Libraries/Image/resolveAssetSource.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index 07f1a4d09ba1..6527e91df096 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -102,8 +102,8 @@ function getScaledAssetPath(asset) { */ function getAssetPathInDrawableFolder(asset) { var scale = pickScale(asset.scales, PixelRatio.get()); - const drawbleFolder = assetPathUtils.getAndroidDrawableFolderName(asset, scale); - const fileName = assetPathUtils.getAndroidResourceIdentifier(asset); + var drawbleFolder = assetPathUtils.getAndroidDrawableFolderName(asset, scale); + var fileName = assetPathUtils.getAndroidResourceIdentifier(asset); return drawbleFolder + '/' + fileName + '.' + asset.type; }