-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Move AssetRegistry implementation into main package, expose as public API (#57369) #57369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
huntie
wants to merge
2
commits into
react:main
Choose a base branch
from
huntie:export-D109019622
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+454
−193
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # @react-native/asset-utils | ||
|
|
||
| [![npm]](https://www.npmjs.com/package/@react-native/asset-utils) [![npm downloads]](https://www.npmjs.com/package/@react-native/asset-utils) | ||
|
|
||
| [npm]: https://img.shields.io/npm/v/@react-native/asset-utils.svg?color=blue | ||
| [npm downloads]: https://img.shields.io/npm/dm/@react-native/asset-utils.svg | ||
|
|
||
| Android resource-path helpers used when copying React Native assets into `drawable-*` / `raw` folders. Consumed by bundling and build tooling; most apps never import this directly. | ||
|
|
||
| ## API | ||
|
|
||
| ```js | ||
| import { | ||
| getAndroidResourceFolderName, | ||
| getAndroidResourceIdentifier, | ||
| } from '@react-native/asset-utils'; | ||
| ``` | ||
|
|
||
| | Export | Signature | Notes | | ||
| |---|---|---| | ||
| | `getAndroidResourceFolderName` | `(asset: PackagerAsset, scale: number) => string` | e.g. `drawable-xhdpi`; non-drawable types resolve to `raw` | | ||
| | `getAndroidResourceIdentifier` | `(asset: PackagerAsset) => string` | Sanitised resource name | | ||
| | `drawableFileTypes` | `Set<string>` | Asset types that map to a `drawable-*` folder | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| { | ||
| "name": "@react-native/asset-utils", | ||
| "version": "0.87.0-main", | ||
| "description": "Asset path utilities for React Native.", | ||
| "license": "MIT", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/react/react-native.git", | ||
| "directory": "packages/asset-utils" | ||
| }, | ||
| "homepage": "https://github.com/react/react-native/tree/HEAD/packages/asset-utils#readme", | ||
| "keywords": [ | ||
| "react-native" | ||
| ], | ||
| "bugs": "https://github.com/react/react-native/issues", | ||
| "engines": { | ||
| "node": "^22.13.0 || ^24.3.0 || >= 26.0.0" | ||
| }, | ||
| "exports": { | ||
| ".": "./src/index.js", | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "files": [ | ||
| "src", | ||
| "README.md", | ||
| "!**/__docs__/**", | ||
| "!**/__fixtures__/**", | ||
| "!**/__mocks__/**", | ||
| "!**/__tests__/**" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * 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. | ||
| * | ||
| * @format | ||
| */ | ||
|
|
||
| export type PackagerAsset = Readonly<{ | ||
| httpServerLocation: string; | ||
| name: string; | ||
| type: string; | ||
| }>; | ||
|
|
||
| export function getAndroidResourceFolderName( | ||
| asset: PackagerAsset, | ||
| scale: number, | ||
| ): string; | ||
|
|
||
| export function getAndroidResourceIdentifier(asset: PackagerAsset): string; | ||
|
|
||
| export const drawableFileTypes: Set<string>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /** | ||
| * 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, | ||
| }; |
72 changes: 72 additions & 0 deletions
72
packages/asset-utils/src/__tests__/AndroidPathUtils-test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /** | ||
| * 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-local | ||
| * @format | ||
| */ | ||
|
|
||
| import {getAndroidResourceFolderName} from '../AndroidPathUtils'; | ||
|
|
||
| const DRAWABLE_ASSET = { | ||
| httpServerLocation: '/assets/', | ||
| name: 'foo', | ||
| type: 'png', | ||
| }; | ||
|
|
||
| const NON_DRAWABLE_ASSET = { | ||
| httpServerLocation: '/assets/', | ||
| name: 'foo', | ||
| type: 'txt', | ||
| }; | ||
|
|
||
| describe('getAndroidResourceFolderName', () => { | ||
| test('supports the six primary density buckets', () => { | ||
| expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 0.75)).toBe( | ||
| 'drawable-ldpi', | ||
| ); | ||
| expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 1)).toBe( | ||
| 'drawable-mdpi', | ||
| ); | ||
| expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 1.5)).toBe( | ||
| 'drawable-hdpi', | ||
| ); | ||
| expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 2)).toBe( | ||
| 'drawable-xhdpi', | ||
| ); | ||
| expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 3)).toBe( | ||
| 'drawable-xxhdpi', | ||
| ); | ||
| expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 4)).toBe( | ||
| 'drawable-xxxhdpi', | ||
| ); | ||
| }); | ||
|
|
||
| test('supports nonstandard densities', () => { | ||
| expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 1.25)).toBe( | ||
| 'drawable-200dpi', | ||
| ); | ||
| expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 1.66)).toBe( | ||
| 'drawable-266dpi', | ||
| ); | ||
| expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 1.33)).toBe( | ||
| 'drawable-213dpi', | ||
| ); // ~tvdpi | ||
| }); | ||
|
|
||
| test('throws if the density cannot be processed', () => { | ||
| expect(() => getAndroidResourceFolderName(DRAWABLE_ASSET, -1)).toThrow(); | ||
| expect(() => getAndroidResourceFolderName(DRAWABLE_ASSET, 0)).toThrow(); | ||
| expect(() => | ||
| getAndroidResourceFolderName(DRAWABLE_ASSET, Infinity), | ||
| ).toThrow(); | ||
| }); | ||
|
|
||
| test('returns "raw" for non-drawables', () => { | ||
| expect(getAndroidResourceFolderName(NON_DRAWABLE_ASSET, 0.75)).toBe('raw'); | ||
| expect(getAndroidResourceFolderName(NON_DRAWABLE_ASSET, 1)).toBe('raw'); | ||
| expect(getAndroidResourceFolderName(NON_DRAWABLE_ASSET, 1.25)).toBe('raw'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /** | ||
| * 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'; | ||
|
|
||
| /*:: | ||
| export type {PackagerAsset} from './AndroidPathUtils'; | ||
| */ | ||
|
|
||
| module.exports = require('./AndroidPathUtils'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Key change 1: Singleton module is located in the typically deduplicated
react-nativepackage (both conventionally in the ecosystem and specifically requested in this case by Expo).