Skip to content

Commit 4e0908a

Browse files
huntiemeta-codesync[bot]
authored andcommitted
Document assets-registry package, add TS definitions
Summary: 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). - Move source files to `src/` (`"exports"` map added; no breaking changes). Changelog: [Internal] Differential Revision: D108624321
1 parent a160cac commit 4e0908a

7 files changed

Lines changed: 74 additions & 21 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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010
},
1111
"homepage": "https://github.com/facebook/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/facebook/react-native/issues",
1916
"engines": {
2017
"node": "^22.13.0 || ^24.3.0 || >= 26.0.0"
2118
},
19+
"exports": {
20+
"./path-support": "./src/path-support.js",
21+
"./registry": "./src/registry.js",
22+
"./package.json": "./package.json"
23+
},
2224
"files": [
23-
"path-support.js",
24-
"registry.js",
25-
"README.md",
25+
"src",
2626
"!**/__docs__/**",
2727
"!**/__fixtures__/**",
2828
"!**/__mocks__/**",

packages/assets-registry/__tests__/path-support-test.js renamed to packages/assets-registry/src/__tests__/path-support-test.js

File renamed without changes.
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;
File renamed without changes.
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;
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@ function getAssetByID(assetId /*: number */) /*: PackagerAsset */ {
4040
return assets[assetId - 1];
4141
}
4242

43-
// eslint-disable-next-line @react-native/monorepo/no-commonjs-exports
44-
module.exports = {registerAsset, getAssetByID};
43+
module.exports = {
44+
registerAsset,
45+
getAssetByID,
46+
};

0 commit comments

Comments
 (0)