|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +/*:: |
| 14 | +// Conforms to the `PackagerAsset` type from `react-native`. |
| 15 | +export type PackagerAsset = Readonly<{ |
| 16 | + httpServerLocation: string, |
| 17 | + name: string, |
| 18 | + type: string, |
| 19 | + ... |
| 20 | +}>; |
| 21 | +*/ |
| 22 | + |
| 23 | +const androidScaleSuffix /*: {[string]: string} */ = { |
| 24 | + '0.75': 'ldpi', |
| 25 | + '1': 'mdpi', |
| 26 | + '1.5': 'hdpi', |
| 27 | + '2': 'xhdpi', |
| 28 | + '3': 'xxhdpi', |
| 29 | + '4': 'xxxhdpi', |
| 30 | +}; |
| 31 | + |
| 32 | +const ANDROID_BASE_DENSITY = 160; |
| 33 | + |
| 34 | +// FIXME: Using number to represent discrete scale numbers is fragile in |
| 35 | +// essence because of floating point number imprecision. |
| 36 | +function getAndroidAssetSuffix(scale /*: number */) /*: string */ { |
| 37 | + if (scale.toString() in androidScaleSuffix) { |
| 38 | + return androidScaleSuffix[scale.toString()]; |
| 39 | + } |
| 40 | + |
| 41 | + // NOTE: Android Gradle Plugin does not fully support the nnndpi format. |
| 42 | + // See https://issuetracker.google.com/issues/72884435 |
| 43 | + if (Number.isFinite(scale) && scale > 0) { |
| 44 | + return Math.round(scale * ANDROID_BASE_DENSITY) + 'dpi'; |
| 45 | + } |
| 46 | + |
| 47 | + throw new Error('no such scale ' + scale.toString()); |
| 48 | +} |
| 49 | + |
| 50 | +// See https://developer.android.com/guide/topics/resources/drawable-resource.html |
| 51 | +const drawableFileTypes /*: Set<string> */ = new Set([ |
| 52 | + 'gif', |
| 53 | + 'heic', |
| 54 | + 'heif', |
| 55 | + 'jpeg', |
| 56 | + 'jpg', |
| 57 | + 'ktx', |
| 58 | + 'png', |
| 59 | + 'webp', |
| 60 | + 'xml', |
| 61 | +]); |
| 62 | + |
| 63 | +function getAndroidResourceFolderName( |
| 64 | + asset /*: PackagerAsset */, |
| 65 | + scale /*: number */, |
| 66 | +) /*: string */ { |
| 67 | + if (!drawableFileTypes.has(asset.type)) { |
| 68 | + return 'raw'; |
| 69 | + } |
| 70 | + |
| 71 | + return 'drawable-' + getAndroidAssetSuffix(scale); |
| 72 | +} |
| 73 | + |
| 74 | +function getAndroidResourceIdentifier( |
| 75 | + asset /*: PackagerAsset */, |
| 76 | +) /*: string */ { |
| 77 | + return (getBasePath(asset) + '/' + asset.name) |
| 78 | + .toLowerCase() |
| 79 | + .replace(/\//g, '_') // Encode folder structure in file name |
| 80 | + .replace(/([^a-z0-9_])/g, '') // Remove illegal chars |
| 81 | + .replace(/^(?:assets|assetsunstable_path)_/, ''); // Remove "assets_" or "assetsunstable_path_" prefix |
| 82 | +} |
| 83 | + |
| 84 | +function getBasePath(asset /*: PackagerAsset */) /*: string */ { |
| 85 | + const basePath = asset.httpServerLocation; |
| 86 | + return basePath.startsWith('/') ? basePath.slice(1) : basePath; |
| 87 | +} |
| 88 | + |
| 89 | +module.exports = { |
| 90 | + drawableFileTypes, |
| 91 | + getAndroidResourceFolderName, |
| 92 | + getAndroidResourceIdentifier, |
| 93 | +}; |
0 commit comments