Skip to content

Commit 45b13a1

Browse files
huntiemeta-codesync[bot]
authored andcommitted
Document assets-registry package, add TS definitions (#57231)
Summary: Pull Request resolved: #57231 Quality pass on the `assets-registry` package ahead of the next diff. - Add user-facing notes to package README. - Add adjacent `.d.ts` definitions (manually/AI-maintained is fine at this scale). Changelog: [Internal] Reviewed By: cortinico Differential Revision: D108624321 fbshipit-source-id: 237dfd6f37183f03109dc32ac2ca9b0f0e939961
1 parent dcbd860 commit 45b13a1

4 files changed

Lines changed: 66 additions & 16 deletions

File tree

packages/assets-registry/README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
# @react-native/assets-registry
22

3-
[![Version][version-badge]][package]
3+
![npm package](https://img.shields.io/npm/v/@react-native/assets-registry?color=brightgreen&label=npm%20package)
44

5-
## Installation
5+
Runtime registry that maps asset IDs generated in a Metro bundle to asset metadata. It backs `<Image>`, `Image.resolveAssetSource()`, and any code that resolves `require('./img.png')` on native.
66

7-
```
8-
yarn add --dev @react-native/assets-registry
9-
```
7+
Most apps never import this directly — assets are handled through `<Image>`.
108

11-
*Note: We're using `yarn` to install deps. Feel free to change commands to use `npm` 3+ and `npx` if you like*
9+
## API
1210

13-
[version-badge]: https://img.shields.io/npm/v/@react-native/assets-registry?style=flat-square
14-
[package]: https://www.npmjs.com/package/@react-native/assets-registry
11+
### `@react-native/assets-registry/registry`
1512

16-
## Testing
13+
| Export | Signature | Notes |
14+
|---|---|---|
15+
| `registerAsset` | `(asset: PackagerAsset) => number` | Stores the asset; returns a numeric ID |
16+
| `getAssetByID` | `(assetId: number) => PackagerAsset` | Looks an asset back up by ID |
1717

18-
To run the tests in this package, run the following commands from the React Native root folder:
18+
### `@react-native/assets-registry/path-support`
1919

20-
1. `yarn` to install the dependencies. You just need to run this once
21-
2. `yarn jest packages/assets-registry`.
20+
Android resource-path helpers, used when copying assets into `drawable-*` folders.
21+
22+
| Export | Signature | Notes |
23+
|---|---|---|
24+
| `getAndroidResourceFolderName` | `(asset: PackagerAsset, scale: number) => string` | e.g. `drawable-xhdpi`; non-drawable types resolve to `raw` |
25+
| `getAndroidResourceIdentifier` | `(asset: PackagerAsset) => string` | Sanitised resource name |
26+
| `getBasePath` | `(asset: PackagerAsset) => string` | `httpServerLocation` without the leading slash |

packages/assets-registry/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@
1010
},
1111
"homepage": "https://github.com/react/react-native/tree/HEAD/packages/assets-registry#readme",
1212
"keywords": [
13-
"assets",
14-
"registry",
15-
"react-native",
16-
"support"
13+
"react-native"
1714
],
1815
"bugs": "https://github.com/react/react-native/issues",
1916
"engines": {
2017
"node": "^22.13.0 || ^24.3.0 || >= 26.0.0"
2118
},
2219
"files": [
2320
"path-support.js",
21+
"path-support.d.ts",
2422
"registry.js",
23+
"registry.d.ts",
2524
"README.md",
2625
"!**/__docs__/**",
2726
"!**/__fixtures__/**",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
* @format
8+
*/
9+
10+
import type {PackagerAsset} from './registry';
11+
12+
export function getAndroidResourceFolderName(
13+
asset: PackagerAsset,
14+
scale: number,
15+
): string;
16+
17+
export function getAndroidResourceIdentifier(asset: PackagerAsset): string;
18+
19+
export function getBasePath(asset: PackagerAsset): string;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
* @format
8+
*/
9+
10+
export type AssetDestPathResolver = 'android' | 'generic';
11+
12+
export type PackagerAsset = {
13+
readonly __packager_asset: boolean;
14+
readonly fileSystemLocation: string;
15+
readonly httpServerLocation: string;
16+
readonly width: number | null | undefined;
17+
readonly height: number | null | undefined;
18+
readonly scales: Array<number>;
19+
readonly hash: string;
20+
readonly name: string;
21+
readonly type: string;
22+
readonly resolver?: AssetDestPathResolver | undefined;
23+
};
24+
25+
export function registerAsset(asset: PackagerAsset): number;
26+
27+
export function getAssetByID(assetId: number): PackagerAsset;

0 commit comments

Comments
 (0)