UNPKG

2.03 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 * @flow
10 */
11'use strict';
12
13import type { Asset } from './parser';
14
15// This file is modified based on assetPathUtil.js from react-native.
16// @see https://github.com/facebook/react-native/blob/master/local-cli/bundle/assetPathUtils.js
17
18function getAssetPathInDrawableFolder(asset : Asset): Array<string> {
19 const paths : Array<string> = [];
20 asset.scales.forEach(scale => {
21 const drawableFolder = getAndroidDrawableFolderName(asset, scale);
22 const fileName = getAndroidResourceIdentifier(asset);
23 paths.push(drawableFolder + '/' + fileName + '.' + asset.type);
24 });
25 return paths;
26}
27
28function getAndroidAssetSuffix(scale : number) {
29 switch (scale) {
30 case 0.75: return 'ldpi';
31 case 1: return 'mdpi';
32 case 1.5: return 'hdpi';
33 case 2: return 'xhdpi';
34 case 3: return 'xxhdpi';
35 case 4: return 'xxxhdpi';
36 }
37}
38
39function getAndroidDrawableFolderName(asset : Asset, scale : number) {
40 const suffix = getAndroidAssetSuffix(scale);
41 if (!suffix) {
42 throw new Error(
43 'Don\'t know which android drawable suffix to use for asset: ' +
44 JSON.stringify(asset)
45 );
46 }
47 return 'drawable-' + suffix;
48}
49
50function getAndroidResourceIdentifier(asset : Asset) {
51 const folderPath = getBasePath(asset);
52 return (folderPath + '/' + asset.name)
53 .toLowerCase()
54 .replace(/\//g, '_') // Encode folder structure in file name
55 .replace(/([^a-z0-9_])/g, '') // Remove illegal chars
56 .replace(/^assets_/, ''); // Remove "assets_" prefix
57}
58
59function getBasePath(asset : Asset) {
60 let basePath = asset.httpServerLocation;
61 if (basePath[0] === '/') {
62 basePath = basePath.substr(1);
63 }
64 return basePath;
65}
66
67module.exports = {
68 getAssetPathInDrawableFolder
69};
\No newline at end of file