12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var path_1 = require("path");
  4. var Constants = require("../util/constants");
  5. var helpers_1 = require("../util/helpers");
  6. // For webapps, we pretty much need all fonts to be available because
  7. // the web server deployment never knows which browser/platform is
  8. // opening the app. Additionally, webapps will request fonts on-demand,
  9. // so having them all sit in the www/assets/fonts directory doesn’t
  10. // hurt anything if it’s never being requested.
  11. // However, with Cordova, the entire directory gets bundled and
  12. // shipped in the ipa/apk, but we also know exactly which platform
  13. // is opening the webapp. For this reason we can safely delete font
  14. // files we know would never be opened by the platform. So app-scripts
  15. // will continue to copy all font files over, but the cordova build
  16. // process would delete those we know are useless and just taking up
  17. // space. End goal is that the Cordova ipa/apk filesize is smaller.
  18. // Font Format Support:
  19. // ttf: http://caniuse.com/#feat=ttf
  20. // woff: http://caniuse.com/#feat=woff
  21. // woff2: http://caniuse.com/#feat=woff2
  22. function removeUnusedFonts(context) {
  23. var fontDir = helpers_1.getStringPropertyValue(Constants.ENV_VAR_FONTS_DIR);
  24. return helpers_1.readDirAsync(fontDir).then(function (fileNames) {
  25. fileNames = fileNames.sort();
  26. var toPurge = getFontFileNamesToPurge(context.target, context.platform, fileNames);
  27. var fullPaths = toPurge.map(function (fileName) { return path_1.join(fontDir, fileName); });
  28. var promises = fullPaths.map(function (fullPath) { return helpers_1.unlinkAsync(fullPath); });
  29. return Promise.all(promises);
  30. });
  31. }
  32. exports.removeUnusedFonts = removeUnusedFonts;
  33. function getFontFileNamesToPurge(target, platform, fileNames) {
  34. if (target !== Constants.CORDOVA) {
  35. return [];
  36. }
  37. var filesToDelete = new Set();
  38. for (var _i = 0, fileNames_1 = fileNames; _i < fileNames_1.length; _i++) {
  39. var fileName = fileNames_1[_i];
  40. if (platform === 'android') {
  41. // remove noto-sans, roboto, and non-woff ionicons
  42. if (fileName.startsWith('noto-sans') || fileName.startsWith('roboto') || (isIonicons(fileName) && !isWoof(fileName))) {
  43. filesToDelete.add(fileName);
  44. }
  45. }
  46. else if (platform === 'ios') {
  47. // remove noto-sans, non-woff ionicons
  48. if (fileName.startsWith('noto-sans') || (fileName.startsWith('roboto') && !isWoof(fileName)) || (isIonicons(fileName) && !isWoof(fileName))) {
  49. filesToDelete.add(fileName);
  50. }
  51. }
  52. // for now don't bother deleting anything for windows, need to get some info first
  53. }
  54. return Array.from(filesToDelete);
  55. }
  56. exports.getFontFileNamesToPurge = getFontFileNamesToPurge;
  57. function isIonicons(fileName) {
  58. return fileName.startsWith('ionicons');
  59. }
  60. // woof woof
  61. function isWoof(fileName) {
  62. return path_1.extname(fileName) === '.woff' || path_1.extname(fileName) === '.woff2';
  63. }