@react-native/assets-registry 0.72.0

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/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "@react-native/assets-registry",
3
+ "version": "0.72.0",
4
+ "description": "Asset support code for React Native.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git@github.com:facebook/react-native.git",
8
+ "directory": "packages/assets"
9
+ },
10
+ "license": "MIT"
11
+ }
@@ -0,0 +1,92 @@
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
+ * @flow strict
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import type {PackagerAsset} from './registry.js';
14
+
15
+ const androidScaleSuffix = {
16
+ '0.75': 'ldpi',
17
+ '1': 'mdpi',
18
+ '1.5': 'hdpi',
19
+ '2': 'xhdpi',
20
+ '3': 'xxhdpi',
21
+ '4': 'xxxhdpi',
22
+ };
23
+
24
+ const ANDROID_BASE_DENSITY = 160;
25
+
26
+ /**
27
+ * FIXME: using number to represent discrete scale numbers is fragile in essence because of
28
+ * floating point numbers imprecision.
29
+ */
30
+ function getAndroidAssetSuffix(scale: number): string {
31
+ if (scale.toString() in androidScaleSuffix) {
32
+ return androidScaleSuffix[scale.toString()];
33
+ }
34
+ // NOTE: Android Gradle Plugin does not fully support the nnndpi format.
35
+ // See https://issuetracker.google.com/issues/72884435
36
+ if (Number.isFinite(scale) && scale > 0) {
37
+ return Math.round(scale * ANDROID_BASE_DENSITY) + 'dpi';
38
+ }
39
+ throw new Error('no such scale ' + scale.toString());
40
+ }
41
+
42
+ // See https://developer.android.com/guide/topics/resources/drawable-resource.html
43
+ const drawableFileTypes = new Set([
44
+ 'gif',
45
+ 'jpeg',
46
+ 'jpg',
47
+ 'ktx',
48
+ 'png',
49
+ 'svg',
50
+ 'webp',
51
+ 'xml',
52
+ ]);
53
+
54
+ function getAndroidResourceFolderName(
55
+ asset: PackagerAsset,
56
+ scale: number,
57
+ ): string | $TEMPORARY$string<'raw'> {
58
+ if (!drawableFileTypes.has(asset.type)) {
59
+ return 'raw';
60
+ }
61
+ const suffix = getAndroidAssetSuffix(scale);
62
+ if (!suffix) {
63
+ throw new Error(
64
+ "Don't know which android drawable suffix to use for scale: " +
65
+ scale +
66
+ '\nAsset: ' +
67
+ JSON.stringify(asset, null, '\t') +
68
+ '\nPossible scales are:' +
69
+ JSON.stringify(androidScaleSuffix, null, '\t'),
70
+ );
71
+ }
72
+ return 'drawable-' + suffix;
73
+ }
74
+
75
+ function getAndroidResourceIdentifier(asset: PackagerAsset): string {
76
+ return (getBasePath(asset) + '/' + asset.name)
77
+ .toLowerCase()
78
+ .replace(/\//g, '_') // Encode folder structure in file name
79
+ .replace(/([^a-z0-9_])/g, '') // Remove illegal chars
80
+ .replace(/^assets_/, ''); // Remove "assets_" prefix
81
+ }
82
+
83
+ function getBasePath(asset: PackagerAsset): string {
84
+ const basePath = asset.httpServerLocation;
85
+ return basePath.startsWith('/') ? basePath.substr(1) : basePath;
86
+ }
87
+
88
+ module.exports = {
89
+ getAndroidResourceFolderName,
90
+ getAndroidResourceIdentifier,
91
+ getBasePath,
92
+ };
package/registry.js ADDED
@@ -0,0 +1,38 @@
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
+ export type PackagerAsset = {
14
+ +__packager_asset: boolean,
15
+ +fileSystemLocation: string,
16
+ +httpServerLocation: string,
17
+ +width: ?number,
18
+ +height: ?number,
19
+ +scales: Array<number>,
20
+ +hash: string,
21
+ +name: string,
22
+ +type: string,
23
+ ...
24
+ };
25
+
26
+ const assets: Array<PackagerAsset> = [];
27
+
28
+ function registerAsset(asset: PackagerAsset): number {
29
+ // `push` returns new array length, so the first asset will
30
+ // get id 1 (not 0) to make the value truthy
31
+ return assets.push(asset);
32
+ }
33
+
34
+ function getAssetByID(assetId: number): PackagerAsset {
35
+ return assets[assetId - 1];
36
+ }
37
+
38
+ module.exports = {registerAsset, getAssetByID};