From 5f0c5b231eb53ac249ec71189721588168305fe6 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Fri, 23 Jun 2017 16:35:40 -0700 Subject: [PATCH] Allow to resolve assets with explicit platform extension Summary: Given a file `test.android.png`, this allows it to be found as `test.android` for arbitrary platforms. Reviewed By: bestander Differential Revision: D5305807 fbshipit-source-id: d869a1da0cc502f853314790385734897f269d6a --- .../src/node-haste/AssetResolutionCache.js | 9 +++++++- .../__tests__/AssetResolutionCache-test.js | 22 +++++++++++++++++-- .../AssetResolutionCache-test.js.snap | 18 --------------- 3 files changed, 28 insertions(+), 21 deletions(-) delete mode 100644 packages/metro-bundler/src/node-haste/__tests__/__snapshots__/AssetResolutionCache-test.js.snap diff --git a/packages/metro-bundler/src/node-haste/AssetResolutionCache.js b/packages/metro-bundler/src/node-haste/AssetResolutionCache.js index 79fa8381..68b6db9c 100644 --- a/packages/metro-bundler/src/node-haste/AssetResolutionCache.js +++ b/packages/metro-bundler/src/node-haste/AssetResolutionCache.js @@ -102,9 +102,16 @@ class AssetResolutionCache { continue; } getWithDefaultArray(results, assetData.assetName).push({ - platform: assetData.platform, fileName, + platform: assetData.platform, }); + if (assetData.platform) { + const assetNameWithPlatform = `${assetData.name}.${assetData.platform}.${assetData.type}`; + getWithDefaultArray(results, assetNameWithPlatform).push({ + fileName, + platform: null, + }); + } } return results; }; diff --git a/packages/metro-bundler/src/node-haste/__tests__/AssetResolutionCache-test.js b/packages/metro-bundler/src/node-haste/__tests__/AssetResolutionCache-test.js index 89d61e29..cdfb28c7 100644 --- a/packages/metro-bundler/src/node-haste/__tests__/AssetResolutionCache-test.js +++ b/packages/metro-bundler/src/node-haste/__tests__/AssetResolutionCache-test.js @@ -38,7 +38,11 @@ describe('AssetResolutionCache', () => { it('finds the correct assets', () => { const results = cache.resolve('/assets', 'test.png', 'ios'); - expect(results).toMatchSnapshot(); + expect(results).toEqual([ + 'test@2x.ios.png', + 'test@1x.ios.png', + 'test@1.5x.ios.png', + ]); }); it('correctly clears out', () => { @@ -46,6 +50,20 @@ describe('AssetResolutionCache', () => { fileNames.push('test@3x.ios.png'); cache.clear(); const results = cache.resolve('/assets', 'test.png', 'ios'); - expect(results).toMatchSnapshot(); + expect(results).toEqual([ + 'test@2x.ios.png', + 'test@1x.ios.png', + 'test@1.5x.ios.png', + 'test@3x.ios.png', + ]); + }); + + it('allows resolving assets with platform extension', () => { + const results = cache.resolve('/assets', 'test.ios.png', 'android'); + expect(results).toEqual([ + 'test@2x.ios.png', + 'test@1x.ios.png', + 'test@1.5x.ios.png', + ]); }); }); diff --git a/packages/metro-bundler/src/node-haste/__tests__/__snapshots__/AssetResolutionCache-test.js.snap b/packages/metro-bundler/src/node-haste/__tests__/__snapshots__/AssetResolutionCache-test.js.snap deleted file mode 100644 index 1f670871..00000000 --- a/packages/metro-bundler/src/node-haste/__tests__/__snapshots__/AssetResolutionCache-test.js.snap +++ /dev/null @@ -1,18 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`AssetResolutionCache correctly clears out 1`] = ` -Array [ - "test@2x.ios.png", - "test@1x.ios.png", - "test@1.5x.ios.png", - "test@3x.ios.png", -] -`; - -exports[`AssetResolutionCache finds the correct assets 1`] = ` -Array [ - "test@2x.ios.png", - "test@1x.ios.png", - "test@1.5x.ios.png", -] -`;