UNPKG

1.92 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 *
10 */
11'use strict';
12
13// This file is modified based on assetPathUtil.js from react-native.
14// @see https://github.com/facebook/react-native/blob/master/local-cli/bundle/assetPathUtils.js
15
16function getAssetPathInDrawableFolder(asset) {
17 var paths = [];
18 asset.scales.forEach(function (scale) {
19 var drawableFolder = getAndroidDrawableFolderName(asset, scale);
20 var fileName = getAndroidResourceIdentifier(asset);
21 paths.push(drawableFolder + '/' + fileName + '.' + asset.type);
22 });
23 return paths;
24}
25
26function getAndroidAssetSuffix(scale) {
27 switch (scale) {
28 case 0.75:
29 return 'ldpi';
30 case 1:
31 return 'mdpi';
32 case 1.5:
33 return 'hdpi';
34 case 2:
35 return 'xhdpi';
36 case 3:
37 return 'xxhdpi';
38 case 4:
39 return 'xxxhdpi';
40 }
41}
42
43function getAndroidDrawableFolderName(asset, scale) {
44 var suffix = getAndroidAssetSuffix(scale);
45 if (!suffix) {
46 throw new Error('Don\'t know which android drawable suffix to use for asset: ' + JSON.stringify(asset));
47 }
48 return 'drawable-' + suffix;
49}
50
51function getAndroidResourceIdentifier(asset) {
52 var folderPath = getBasePath(asset);
53 return (folderPath + '/' + asset.name).toLowerCase().replace(/\//g, '_') // Encode folder structure in file name
54 .replace(/([^a-z0-9_])/g, '') // Remove illegal chars
55 .replace(/^assets_/, ''); // Remove "assets_" prefix
56}
57
58function getBasePath(asset) {
59 var basePath = asset.httpServerLocation;
60 if (basePath[0] === '/') {
61 basePath = basePath.substr(1);
62 }
63 return basePath;
64}
65
66module.exports = {
67 getAssetPathInDrawableFolder: getAssetPathInDrawableFolder
68};
\No newline at end of file