|
4 | 4 | * This source code is licensed under the MIT license found in the |
5 | 5 | * LICENSE file in the root directory of this source tree. |
6 | 6 | * |
7 | | - * @format |
8 | 7 | * @flow strict |
| 8 | + * @format |
| 9 | + * @oncall react_native |
9 | 10 | */ |
10 | 11 |
|
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.slice(1) : basePath; |
| 12 | +// Compatibility module for tools which don't support the "exports" field: |
| 13 | +// - Flow (type exports) |
| 14 | +// - Metro (runtime exports) |
| 15 | + |
| 16 | +/*:: |
| 17 | +export * from './src/path-support.flow'; |
| 18 | +*/ |
| 19 | + |
| 20 | +try { |
| 21 | + // $FlowIgnore[cannot-resolve-module] |
| 22 | + // $FlowIgnore[invalid-export] |
| 23 | + // $FlowIgnore[module-type-conflict] |
| 24 | + module.exports = require('./dist/path-support.flow'); |
| 25 | +} catch (e) { |
| 26 | + // $FlowIgnore[invalid-export] |
| 27 | + // $FlowIgnore[module-type-conflict] |
| 28 | + module.exports = require('./src/path-support'); |
86 | 29 | } |
87 | | - |
88 | | -module.exports = { |
89 | | - getAndroidResourceFolderName, |
90 | | - getAndroidResourceIdentifier, |
91 | | - getBasePath, |
92 | | -}; |
0 commit comments