@react-native/asset-utils 0.0.1 → 0.87.0-nightly-20260707-b67b7ba62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # @react-native/asset-utils
2
+
3
+ [![npm]](https://www.npmjs.com/package/@react-native/asset-utils) [![npm downloads]](https://www.npmjs.com/package/@react-native/asset-utils)
4
+
5
+ [npm]: https://img.shields.io/npm/v/@react-native/asset-utils.svg?color=blue
6
+ [npm downloads]: https://img.shields.io/npm/dm/@react-native/asset-utils.svg
7
+
8
+ 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.
9
+
10
+ ## API
11
+
12
+ ```js
13
+ import {
14
+ getAndroidResourceFolderName,
15
+ getAndroidResourceIdentifier,
16
+ } from '@react-native/asset-utils';
17
+ ```
18
+
19
+ | Export | Signature | Notes |
20
+ |---|---|---|
21
+ | `getAndroidResourceFolderName` | `(asset: PackagerAsset, scale: number) => string` | e.g. `drawable-xhdpi`; non-drawable types resolve to `raw` |
22
+ | `getAndroidResourceIdentifier` | `(asset: PackagerAsset) => string` | Sanitised resource name |
23
+ | `drawableFileTypes` | `Set<string>` | Asset types that map to a `drawable-*` folder |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-native/asset-utils",
3
- "version": "0.0.1",
3
+ "version": "0.87.0-nightly-20260707-b67b7ba62",
4
4
  "description": "Asset path utilities for React Native.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -12,5 +12,20 @@
12
12
  "keywords": [
13
13
  "react-native"
14
14
  ],
15
- "bugs": "https://github.com/react/react-native/issues"
15
+ "bugs": "https://github.com/react/react-native/issues",
16
+ "engines": {
17
+ "node": "^22.13.0 || ^24.3.0 || >= 26.0.0"
18
+ },
19
+ "exports": {
20
+ ".": "./src/index.js",
21
+ "./package.json": "./package.json"
22
+ },
23
+ "files": [
24
+ "src",
25
+ "README.md",
26
+ "!**/__docs__/**",
27
+ "!**/__fixtures__/**",
28
+ "!**/__mocks__/**",
29
+ "!**/__tests__/**"
30
+ ]
16
31
  }
@@ -0,0 +1,23 @@
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 PackagerAsset = Readonly<{
11
+ httpServerLocation: string;
12
+ name: string;
13
+ type: string;
14
+ }>;
15
+
16
+ export function getAndroidResourceFolderName(
17
+ asset: PackagerAsset,
18
+ scale: number,
19
+ ): string;
20
+
21
+ export function getAndroidResourceIdentifier(asset: PackagerAsset): string;
22
+
23
+ export const drawableFileTypes: Set<string>;
@@ -0,0 +1,93 @@
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
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ /*::
14
+ // Conforms to the `PackagerAsset` type from `react-native`.
15
+ export type PackagerAsset = Readonly<{
16
+ httpServerLocation: string,
17
+ name: string,
18
+ type: string,
19
+ ...
20
+ }>;
21
+ */
22
+
23
+ const androidScaleSuffix /*: {[string]: string} */ = {
24
+ '0.75': 'ldpi',
25
+ '1': 'mdpi',
26
+ '1.5': 'hdpi',
27
+ '2': 'xhdpi',
28
+ '3': 'xxhdpi',
29
+ '4': 'xxxhdpi',
30
+ };
31
+
32
+ const ANDROID_BASE_DENSITY = 160;
33
+
34
+ // FIXME: Using number to represent discrete scale numbers is fragile in
35
+ // essence because of floating point number imprecision.
36
+ function getAndroidAssetSuffix(scale /*: number */) /*: string */ {
37
+ if (scale.toString() in androidScaleSuffix) {
38
+ return androidScaleSuffix[scale.toString()];
39
+ }
40
+
41
+ // NOTE: Android Gradle Plugin does not fully support the nnndpi format.
42
+ // See https://issuetracker.google.com/issues/72884435
43
+ if (Number.isFinite(scale) && scale > 0) {
44
+ return Math.round(scale * ANDROID_BASE_DENSITY) + 'dpi';
45
+ }
46
+
47
+ throw new Error('no such scale ' + scale.toString());
48
+ }
49
+
50
+ // See https://developer.android.com/guide/topics/resources/drawable-resource.html
51
+ const drawableFileTypes /*: Set<string> */ = new Set([
52
+ 'gif',
53
+ 'heic',
54
+ 'heif',
55
+ 'jpeg',
56
+ 'jpg',
57
+ 'ktx',
58
+ 'png',
59
+ 'webp',
60
+ 'xml',
61
+ ]);
62
+
63
+ function getAndroidResourceFolderName(
64
+ asset /*: PackagerAsset */,
65
+ scale /*: number */,
66
+ ) /*: string */ {
67
+ if (!drawableFileTypes.has(asset.type)) {
68
+ return 'raw';
69
+ }
70
+
71
+ return 'drawable-' + getAndroidAssetSuffix(scale);
72
+ }
73
+
74
+ function getAndroidResourceIdentifier(
75
+ asset /*: PackagerAsset */,
76
+ ) /*: string */ {
77
+ return (getBasePath(asset) + '/' + asset.name)
78
+ .toLowerCase()
79
+ .replace(/\//g, '_') // Encode folder structure in file name
80
+ .replace(/([^a-z0-9_])/g, '') // Remove illegal chars
81
+ .replace(/^(?:assets|assetsunstable_path)_/, ''); // Remove "assets_" or "assetsunstable_path_" prefix
82
+ }
83
+
84
+ function getBasePath(asset /*: PackagerAsset */) /*: string */ {
85
+ const basePath = asset.httpServerLocation;
86
+ return basePath.startsWith('/') ? basePath.slice(1) : basePath;
87
+ }
88
+
89
+ module.exports = {
90
+ drawableFileTypes,
91
+ getAndroidResourceFolderName,
92
+ getAndroidResourceIdentifier,
93
+ };
package/src/index.d.ts ADDED
@@ -0,0 +1,10 @@
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 * from './AndroidPathUtils';
package/src/index.js ADDED
@@ -0,0 +1,17 @@
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
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ /*::
14
+ export type {PackagerAsset} from './AndroidPathUtils';
15
+ */
16
+
17
+ module.exports = require('./AndroidPathUtils');