assetPathUtils.js 1.9 KB

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