/
githubmirror
/
react-native
Обзор
Документация
Войти
/
githubmirror
/
react-native
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/asset-utils/src/AndroidPathUtils.js
93 строки
2 KB
Alex Hunt
Create @react-native/asset-utils package (#57368)
06 июл 2026, 20:32
06 июл 2026, 20:32
41d5218
Код
Авторство
О чём код?
/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @flow strict * @format */ 'use strict'; /*:: // Conforms to the `PackagerAsset` type from `react-native`. export type PackagerAsset = Readonly<{ httpServerLocation: string, name: string, type: string, ... }>; */ const androidScaleSuffix /*: {[string]: string} */ = { '0.75': 'ldpi', '1': 'mdpi', '1.5': 'hdpi', '2': 'xhdpi', '3': 'xxhdpi', '4': 'xxxhdpi', }; const ANDROID_BASE_DENSITY = 160; // FIXME: Using number to represent discrete scale numbers is fragile in // essence because of floating point number imprecision. function getAndroidAssetSuffix(scale /*: number */) /*: string */ { if (scale.toString() in androidScaleSuffix) { return androidScaleSuffix[scale.toString()]; } // NOTE: Android Gradle Plugin does not fully support the nnndpi format. // See https://issuetracker.google.com/issues/72884435 if (Number.isFinite(scale) && scale > 0) { return Math.round(scale * ANDROID_BASE_DENSITY) + 'dpi'; } throw new Error('no such scale ' + scale.toString()); } // See https://developer.android.com/guide/topics/resources/drawable-resource.html const drawableFileTypes /*: Set<string> */ = new Set([ 'gif', 'heic', 'heif', 'jpeg', 'jpg', 'ktx', 'png', 'webp', 'xml', ]); function getAndroidResourceFolderName( asset /*: PackagerAsset */, scale /*: number */, ) /*: string */ { if (!drawableFileTypes.has(asset.type)) { return 'raw'; } return 'drawable-' + getAndroidAssetSuffix(scale); } function getAndroidResourceIdentifier( asset /*: PackagerAsset */, ) /*: string */ { return (getBasePath(asset) + '/' + asset.name) .toLowerCase() .replace(/\//g, '_') // Encode folder structure in file name .replace(/([^a-z0-9_])/g, '') // Remove illegal chars .replace(/^(?:assets|assetsunstable_path)_/, ''); // Remove "assets_" or "assetsunstable_path_" prefix } function getBasePath(asset /*: PackagerAsset */) /*: string */ { const basePath = asset.httpServerLocation; return basePath.startsWith('/') ? basePath.slice(1) : basePath; } module.exports = { drawableFileTypes, getAndroidResourceFolderName, getAndroidResourceIdentifier, };