UNPKG

1.95 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8/**
9 * Copyright (c) Facebook, Inc. and its affiliates.
10 *
11 * This source code is licensed under the MIT license found in the
12 * LICENSE file in the root directory of this source tree.
13 *
14 */
15
16/**
17 * FIXME: using number to represent discrete scale numbers is fragile in essence because of
18 * floating point numbers imprecision.
19 */
20function getAndroidAssetSuffix(scale) {
21 switch (scale) {
22 case 0.75:
23 return 'ldpi';
24
25 case 1:
26 return 'mdpi';
27
28 case 1.5:
29 return 'hdpi';
30
31 case 2:
32 return 'xhdpi';
33
34 case 3:
35 return 'xxhdpi';
36
37 case 4:
38 return 'xxxhdpi';
39
40 default:
41 throw new Error('no such scale');
42 }
43} // See https://developer.android.com/guide/topics/resources/drawable-resource.html
44
45
46const drawableFileTypes = new Set(['gif', 'jpeg', 'jpg', 'png', 'svg', 'webp', 'xml']);
47
48function getAndroidResourceFolderName(asset, scale) {
49 if (!drawableFileTypes.has(asset.type)) {
50 return 'raw';
51 }
52
53 const suffix = getAndroidAssetSuffix(scale);
54
55 if (!suffix) {
56 throw new Error(`Don't know which android drawable suffix to use for asset: ${JSON.stringify(asset)}`);
57 }
58
59 const androidFolder = `drawable-${suffix}`;
60 return androidFolder;
61}
62
63function getAndroidResourceIdentifier(asset) {
64 const folderPath = getBasePath(asset);
65 return `${folderPath}/${asset.name}`.toLowerCase().replace(/\//g, '_') // Encode folder structure in file name
66 .replace(/([^a-z0-9_])/g, '') // Remove illegal chars
67 .replace(/^assets_/, ''); // Remove "assets_" prefix
68}
69
70function getBasePath(asset) {
71 let basePath = asset.httpServerLocation;
72
73 if (basePath[0] === '/') {
74 basePath = basePath.substr(1);
75 }
76
77 return basePath;
78}
79
80var _default = {
81 getAndroidAssetSuffix,
82 getAndroidResourceFolderName,
83 getAndroidResourceIdentifier,
84 getBasePath
85};
86exports.default = _default;
\No newline at end of file