From 8e9549fb2ba3b4a25f94482d3b432aa0d77250f4 Mon Sep 17 00:00:00 2001 From: Chris Hopman Date: Thu, 22 Dec 2016 15:57:53 -0800 Subject: [PATCH] make the RN packager output more deterministic Reviewed By: davidaurelio Differential Revision: D4357109 fbshipit-source-id: bc9ef04d81223299fe7708aeb0fe654647330ab7 --- .../src/Bundler/__tests__/Bundler-test.js | 24 +++++++++---------- react-packager/src/Bundler/index.js | 14 +++++------ 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/react-packager/src/Bundler/__tests__/Bundler-test.js b/react-packager/src/Bundler/__tests__/Bundler-test.js index 039ff176..dfb2ddcb 100644 --- a/react-packager/src/Bundler/__tests__/Bundler-test.js +++ b/react-packager/src/Bundler/__tests__/Bundler-test.js @@ -134,7 +134,7 @@ describe('Bundler', function() { it('create a bundle', function() { assetServer.getAssetData.mockImplementation(() => { - return { + return Promise.resolve({ scales: [1,2,3], files: [ '/root/img/img.png', @@ -144,7 +144,7 @@ describe('Bundler', function() { hash: 'i am a hash', name: 'img', type: 'png', - }; + }); }); return bundler.bundle({ @@ -170,8 +170,8 @@ describe('Bundler', function() { __packager_asset: true, fileSystemLocation: '/root/img', httpServerLocation: '/assets/img', - width: 25, - height: 50, + width: 50, + height: 100, scales: [1, 2, 3], files: [ '/root/img/img.png', @@ -217,7 +217,7 @@ describe('Bundler', function() { name: 'img', type: 'png', }; - assetServer.getAssetData.mockImplementation(() => mockAsset); + assetServer.getAssetData.mockImplementation(() => Promise.resolve(mockAsset)); return bundler.bundle({ entryFile: '/root/foo.js', @@ -230,8 +230,8 @@ describe('Bundler', function() { __packager_asset: true, fileSystemLocation: '/root/img', httpServerLocation: '/assets/img', - width: 25, - height: 50, + width: 50, + height: 100, scales: [1, 2, 3], files: [ '/root/img/img.png', @@ -242,7 +242,7 @@ describe('Bundler', function() { name: 'img', type: 'png', extraReverseHash: 'hsah a ma i', - extraPixelCount: 1250, + extraPixelCount: 5000, }]); }); }); @@ -281,7 +281,7 @@ describe('Bundler', function() { beforeEach(() => { assetServer.getAssetData.mockImplementation(function(relPath) { if (relPath === 'img/new_image.png') { - return { + return Promise.resolve({ scales: [1,2,3], files: [ '/root/img/new_image.png', @@ -291,9 +291,9 @@ describe('Bundler', function() { hash: 'i am a hash', name: 'img', type: 'png', - }; + }); } else if (relPath === 'img/new_image2.png') { - return { + return Promise.resolve({ scales: [1,2,3], files: [ '/root/img/new_image2.png', @@ -303,7 +303,7 @@ describe('Bundler', function() { hash: 'i am a hash', name: 'img', type: 'png', - }; + }); } throw new Error('unknown image ' + relPath); diff --git a/react-packager/src/Bundler/index.js b/react-packager/src/Bundler/index.js index a9e8c927..396888ef 100644 --- a/react-packager/src/Bundler/index.js +++ b/react-packager/src/Bundler/index.js @@ -673,20 +673,18 @@ class Bundler { 'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff' ].indexOf(extname(module.path).slice(1)) !== -1; - return Promise.all([ - isImage ? sizeOf(module.path) : null, - this._assetServer.getAssetData(relPath, platform), - ]).then((res) => { + return this._assetServer.getAssetData(relPath, platform).then((assetData) => { + return Promise.all([isImage ? sizeOf(assetData.files[0]) : null, assetData]); + }).then((res) => { const dimensions = res[0]; const assetData = res[1]; + const scale = assetData.scales[0]; const asset = { __packager_asset: true, fileSystemLocation: pathDirname(module.path), httpServerLocation: assetUrlPath, - /* $FlowFixMe: `resolution` is assets-only */ - width: dimensions ? dimensions.width / module.resolution : undefined, - /* $FlowFixMe: `resolution` is assets-only */ - height: dimensions ? dimensions.height / module.resolution : undefined, + width: dimensions ? dimensions.width / scale : undefined, + height: dimensions ? dimensions.height / scale : undefined, scales: assetData.scales, files: assetData.files, hash: assetData.hash,