From 2e5e9fa88a89298fbf9d30a83dc7dfc3d736427e Mon Sep 17 00:00:00 2001 From: Christoph Nakazawa Date: Thu, 6 Dec 2018 20:06:47 -0800 Subject: [PATCH] Move asset related modules into `metro-buck` Summary: These files are some of the few standalone files from the `local-cli` that are used internally. This diff copies them into the one place where they are used. Note that I am leaving the old files in `local-cli`. Even if they are unused, moving them would break flow (require module verification). This diff also moves the `assetPathUtils` file into `Libraries/Image`, which is where it is used. This was previously part of D13337412 but I had to squash them to make buck happy. Reviewed By: TheSavior Differential Revision: D13337304 fbshipit-source-id: 2d501109ba7d4ba94ca7e8f2953258221947b90e --- Libraries/Image/AssetSourceResolver.js | 2 +- .../Image}/assetPathUtils.js | 2 +- .../filterPlatformAssetScales-test.js | 42 ----------- .../__tests__/getAssetDestPathAndroid-test.js | 75 ------------------- .../__tests__/getAssetDestPathIOS-test.js | 45 ----------- local-cli/bundle/getAssetDestPathAndroid.js | 2 +- 6 files changed, 3 insertions(+), 165 deletions(-) rename {local-cli/bundle => Libraries/Image}/assetPathUtils.js (96%) delete mode 100644 local-cli/bundle/__tests__/filterPlatformAssetScales-test.js delete mode 100644 local-cli/bundle/__tests__/getAssetDestPathAndroid-test.js delete mode 100644 local-cli/bundle/__tests__/getAssetDestPathIOS-test.js diff --git a/Libraries/Image/AssetSourceResolver.js b/Libraries/Image/AssetSourceResolver.js index ff8bc277a41..da2daa93968 100644 --- a/Libraries/Image/AssetSourceResolver.js +++ b/Libraries/Image/AssetSourceResolver.js @@ -22,7 +22,7 @@ import type {PackagerAsset} from 'AssetRegistry'; const PixelRatio = require('PixelRatio'); const Platform = require('Platform'); -const assetPathUtils = require('../../local-cli/bundle/assetPathUtils'); +const assetPathUtils = require('./assetPathUtils'); const invariant = require('invariant'); /** diff --git a/local-cli/bundle/assetPathUtils.js b/Libraries/Image/assetPathUtils.js similarity index 96% rename from local-cli/bundle/assetPathUtils.js rename to Libraries/Image/assetPathUtils.js index 9e98ba684a9..0b49343518d 100644 --- a/local-cli/bundle/assetPathUtils.js +++ b/Libraries/Image/assetPathUtils.js @@ -10,7 +10,7 @@ 'use strict'; -import type {PackagerAsset} from '../../Libraries/Image/AssetRegistry'; +import type {PackagerAsset} from './AssetRegistry'; /** * FIXME: using number to represent discrete scale numbers is fragile in essence because of diff --git a/local-cli/bundle/__tests__/filterPlatformAssetScales-test.js b/local-cli/bundle/__tests__/filterPlatformAssetScales-test.js deleted file mode 100644 index ca336d8fdcc..00000000000 --- a/local-cli/bundle/__tests__/filterPlatformAssetScales-test.js +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @emails oncall+javascript_foundation - */ - -'use strict'; - -jest.dontMock('../filterPlatformAssetScales').dontMock('../assetPathUtils'); - -const filterPlatformAssetScales = require('../filterPlatformAssetScales'); - -describe('filterPlatformAssetScales', () => { - it('removes everything but 2x and 3x for iOS', () => { - expect(filterPlatformAssetScales('ios', [1, 1.5, 2, 3, 4])).toEqual([ - 1, - 2, - 3, - ]); - expect(filterPlatformAssetScales('ios', [3, 4])).toEqual([3]); - }); - - it('keeps closest largest one if nothing matches', () => { - expect(filterPlatformAssetScales('ios', [0.5, 4, 100])).toEqual([4]); - expect(filterPlatformAssetScales('ios', [0.5, 100])).toEqual([100]); - expect(filterPlatformAssetScales('ios', [0.5])).toEqual([0.5]); - expect(filterPlatformAssetScales('ios', [])).toEqual([]); - }); - - it('keeps all scales for unknown platform', () => { - expect(filterPlatformAssetScales('freebsd', [1, 1.5, 2, 3.7])).toEqual([ - 1, - 1.5, - 2, - 3.7, - ]); - }); -}); diff --git a/local-cli/bundle/__tests__/getAssetDestPathAndroid-test.js b/local-cli/bundle/__tests__/getAssetDestPathAndroid-test.js deleted file mode 100644 index 792e2676cef..00000000000 --- a/local-cli/bundle/__tests__/getAssetDestPathAndroid-test.js +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @emails oncall+javascript_foundation - */ - -'use strict'; - -jest.dontMock('../getAssetDestPathAndroid').dontMock('../assetPathUtils'); - -const getAssetDestPathAndroid = require('../getAssetDestPathAndroid'); - -const path = require('path'); - -describe('getAssetDestPathAndroid', () => { - it('should use the right destination folder', () => { - const asset = { - name: 'icon', - type: 'png', - httpServerLocation: '/assets/test', - }; - - const expectDestPathForScaleToStartWith = (scale, path) => { - if (!getAssetDestPathAndroid(asset, scale).startsWith(path)) { - throw new Error( - `asset for scale ${scale} should start with path '${path}'`, - ); - } - }; - - expectDestPathForScaleToStartWith(1, 'drawable-mdpi'); - expectDestPathForScaleToStartWith(1.5, 'drawable-hdpi'); - expectDestPathForScaleToStartWith(2, 'drawable-xhdpi'); - expectDestPathForScaleToStartWith(3, 'drawable-xxhdpi'); - expectDestPathForScaleToStartWith(4, 'drawable-xxxhdpi'); - }); - - it('should lowercase path', () => { - const asset = { - name: 'Icon', - type: 'png', - httpServerLocation: '/assets/App/Test', - }; - - expect(getAssetDestPathAndroid(asset, 1)).toBe( - path.normalize('drawable-mdpi/app_test_icon.png'), - ); - }); - - it('should remove `assets/` prefix', () => { - const asset = { - name: 'icon', - type: 'png', - httpServerLocation: '/assets/RKJSModules/Apps/AndroidSample/Assets', - }; - - expect(getAssetDestPathAndroid(asset, 1).startsWith('assets_')).toBeFalsy(); - }); - - it('should put non-drawable resources to `raw/`', () => { - const asset = { - name: 'video', - type: 'mp4', - httpServerLocation: '/assets/app/test', - }; - - expect(getAssetDestPathAndroid(asset, 1)).toBe( - path.normalize('raw/app_test_video.mp4'), - ); - }); -}); diff --git a/local-cli/bundle/__tests__/getAssetDestPathIOS-test.js b/local-cli/bundle/__tests__/getAssetDestPathIOS-test.js deleted file mode 100644 index fb05ba3fb31..00000000000 --- a/local-cli/bundle/__tests__/getAssetDestPathIOS-test.js +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @emails oncall+javascript_foundation - */ - -'use strict'; - -jest.dontMock('../getAssetDestPathIOS'); - -const getAssetDestPathIOS = require('../getAssetDestPathIOS'); -const path = require('path'); - -describe('getAssetDestPathIOS', () => { - it('should build correct path', () => { - const asset = { - name: 'icon', - type: 'png', - httpServerLocation: '/assets/test', - }; - - expect(getAssetDestPathIOS(asset, 1)).toBe( - path.normalize('assets/test/icon.png'), - ); - }); - - it('should consider scale', () => { - const asset = { - name: 'icon', - type: 'png', - httpServerLocation: '/assets/test', - }; - - expect(getAssetDestPathIOS(asset, 2)).toBe( - path.normalize('assets/test/icon@2x.png'), - ); - expect(getAssetDestPathIOS(asset, 3)).toBe( - path.normalize('assets/test/icon@3x.png'), - ); - }); -}); diff --git a/local-cli/bundle/getAssetDestPathAndroid.js b/local-cli/bundle/getAssetDestPathAndroid.js index 36e54f94e29..49b92f2cf29 100644 --- a/local-cli/bundle/getAssetDestPathAndroid.js +++ b/local-cli/bundle/getAssetDestPathAndroid.js @@ -10,7 +10,7 @@ 'use strict'; -const assetPathUtils = require('./assetPathUtils'); +const assetPathUtils = require('../../Libraries/Image/assetPathUtils'); const path = require('path'); import type {PackagerAsset} from '../../Libraries/Image/AssetRegistry';