Skip to content

Commit ca9e8e2

Browse files
huntiemeta-codesync[bot]
authored andcommitted
Expose build entry point for AssetRegistry (#57232)
Summary: Pull Request resolved: #57232 **Problem** Following D108750303, we now expose an `AssetsRegistry` API from `'react-native'`. This is great, as we need to replace a deep `/Libraries/...` import in Metro configs (`transformer.assetRegistryPath`): https://www.internalfb.com/code/fbsource/[31661d52e803d77356725429f44331135601b634]/xplat/js/react-native-github/packages/metro-config/src/index.flow.js?lines=87 **Unfortunately**, we can't quite reuse the new `AssetRegistry` API from the index module, as we'd break Metro's API contract (generate an inline require) — we need a dedicated wrapper module that can be read without destructuring. **This diff** Adds a new `'react-native/asset-registry'` secondary entry point, intended/documented for this Metro config use case — and deprecates `react-native/assets-registry/registry`. - Solves the above problem (migrated in our own `react-native/metro-config` use). - Remains consistent with D108750302 (we intend to remove `react-native/assets-registry` at a later date). - As a bonus, fixes the longstanding naming inconsistency between `react-native/assets-registry` and `'react-native/Libraries/Image/AssetRegistry'` — `AssetRegistry` (singular) is now canonical. Changelog: - [General][Breaking] - `react-native/Libraries/Image/AssetRegistry` is removed. Please use the `AssetRegistry` API (apps/library code) and/or the `react-native/asset-registry` entrypoint (Metro/build configs). - [General][Deprecated] - **assets-registry**: `react-native/assets-registry/registry` is deprecated. Please use the `AssetRegistry` API (apps/library code) and/or the `react-native/asset-registry` entrypoint (Metro/build configs). Reviewed By: javache Differential Revision: D108750303
1 parent a633c67 commit ca9e8e2

7 files changed

Lines changed: 62 additions & 19 deletions

File tree

packages/assets-registry/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ Most apps never import this directly — assets are handled through `<Image>`.
1111

1212
## API
1313

14-
### `@react-native/assets-registry/registry`
15-
16-
> [!Note]
17-
> Aliases to [`AssetRegistry`](https://reactnative.dev/docs/assetregistry) (since 0.87). Prefer importing directly from the `'react-native'` package in libraries.
14+
### `@react-native/assets-registry/registry` (DEPRECATED)
15+
16+
> [!Warning]
17+
> **Deprecated**: Aliases to [`AssetRegistry`](https://reactnative.dev/docs/assetregistry) (since 0.87).
18+
>
19+
> Please use:
20+
> - `import { AssetRegistry } from 'react-native';` (apps/library code)
21+
> - `'react-native/asset-registry'` (entrypoint for Metro/build configs)
1822
1923
| Export | Signature | Notes |
2024
|---|---|---|

packages/assets-registry/registry.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
* @format
88
*/
99

10+
/**
11+
* @deprecated Use `import type {AssetDestPathResolver} from 'react-native'` instead.
12+
*/
1013
export type AssetDestPathResolver = 'android' | 'generic';
1114

15+
/**
16+
* @deprecated Use `import type {PackagerAsset} from 'react-native'` instead.
17+
*/
1218
export type PackagerAsset = {
1319
readonly __packager_asset: boolean;
1420
readonly fileSystemLocation: string;
@@ -22,6 +28,12 @@ export type PackagerAsset = {
2228
readonly resolver?: AssetDestPathResolver | undefined;
2329
};
2430

31+
/**
32+
* @deprecated Use `import {AssetRegistry} from 'react-native'` instead.
33+
*/
2534
export function registerAsset(asset: PackagerAsset): number;
2635

36+
/**
37+
* @deprecated Use `import {AssetRegistry} from 'react-native'` instead.
38+
*/
2739
export function getAssetByID(assetId: number): PackagerAsset;

packages/eslint-plugin-react-native/no-deep-imports.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = {
3232
if (
3333
!isDeepReactNativeImport(node.source) ||
3434
isInitializeCoreImport(node.source) ||
35+
isSecondaryEntryPoint(node.source) ||
3536
isFbInternalImport(node.source)
3637
) {
3738
return;
@@ -88,6 +89,7 @@ module.exports = {
8889
if (
8990
!isDeepRequire(node) ||
9091
isInitializeCoreImport(node.arguments[0]) ||
92+
isSecondaryEntryPoint(node.arguments[0]) ||
9193
isFbInternalImport(node.arguments[0])
9294
) {
9395
return;
@@ -173,6 +175,14 @@ module.exports = {
173175
return source.value === 'react-native/Libraries/Core/InitializeCore';
174176
}
175177

178+
function isSecondaryEntryPoint(source) {
179+
if (source.type !== 'Literal' || typeof source.value !== 'string') {
180+
return false;
181+
}
182+
183+
return source.value === 'react-native/asset-registry';
184+
}
185+
176186
function isFbInternalImport(source) {
177187
if (source.type !== 'Literal' || typeof source.value !== 'string') {
178188
return false;

packages/metro-config/src/index.flow.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function getDefaultConfig(projectRoot: string): ConfigT {
8484
},
8585
transformer: {
8686
allowOptionalDependencies: true,
87-
assetRegistryPath: 'react-native/Libraries/Image/AssetRegistry',
87+
assetRegistryPath: 'react-native/asset-registry',
8888
asyncRequireModulePath: require.resolve(
8989
'metro-runtime/src/modules/asyncRequire',
9090
),

packages/react-native/Libraries/Image/AssetRegistry.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/react-native/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
"react-native-strict-api": null,
5757
"default": "./types/*.d.ts"
5858
},
59+
"./asset-registry": {
60+
"types": null,
61+
"default": "./src/asset-registry.js"
62+
},
5963
"./jest-preset": "./jest-preset.js",
6064
"./rn-get-polyfills": "./rn-get-polyfills.js",
6165
"./src/fb_internal/*": "./src/fb_internal/*",
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+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
// ----------------------------------------------------------------------------
12+
// Secondary react-native/asset-registry entry point.
13+
//
14+
// This is an untyped secondary entry point intended to be referenced from
15+
// Metro's `transformer.assetRegistryPath` config option. This entry point may
16+
// also be preferred in server-side code.
17+
//
18+
// Apps/libraries should use `import {AssetRegistry} from 'react-native'`.
19+
// ----------------------------------------------------------------------------
20+
21+
import {AssetRegistry} from './private/assets/AssetRegistry';
22+
23+
/* eslint-disable @react-native/monorepo/no-commonjs-exports */
24+
module.exports = {
25+
registerAsset: AssetRegistry.registerAsset,
26+
getAssetByID: AssetRegistry.getAssetByID,
27+
};

0 commit comments

Comments
 (0)