|
1 |
| -import { BuildContext } from '../util/interfaces'; |
2 |
| -import { join } from 'path'; |
3 |
| -import { Logger } from '../logger/logger'; |
4 |
| -import { unlinkAsync } from '../util/helpers'; |
5 |
| -import * as glob from 'glob'; |
6 |
| - |
7 |
| - |
8 |
| -export function removeUnusedFonts(context: BuildContext) { |
9 |
| - // For webapps, we pretty much need all fonts to be available because |
10 |
| - // the web server deployment never knows which browser/platform is |
11 |
| - // opening the app. Additionally, webapps will request fonts on-demand, |
12 |
| - // so having them all sit in the www/assets/fonts directory doesn’t |
13 |
| - // hurt anything if it’s never being requested. |
14 |
| - |
15 |
| - // However, with Cordova, the entire directory gets bundled and |
16 |
| - // shipped in the ipa/apk, but we also know exactly which platform |
17 |
| - // is opening the webapp. For this reason we can safely delete font |
18 |
| - // files we know would never be opened by the platform. So app-scripts |
19 |
| - // will continue to copy all font files over, but the cordova build |
20 |
| - // process would delete those we know are useless and just taking up |
21 |
| - // space. End goal is that the Cordova ipa/apk filesize is smaller. |
| 1 | +import { extname, join } from 'path'; |
22 | 2 |
|
23 |
| - // Font Format Support: |
24 |
| - // ttf: http://caniuse.com/#feat=ttf |
25 |
| - // woff: http://caniuse.com/#feat=woff |
26 |
| - // woff2: http://caniuse.com/#feat=woff2 |
27 |
| - |
28 |
| - if (context.target === 'cordova') { |
29 |
| - const fontsRemoved: string[] = []; |
30 |
| - // all cordova builds should remove .eot, .svg, .ttf, and .scss files |
31 |
| - fontsRemoved.push('*.eot'); |
32 |
| - fontsRemoved.push('*.ttf'); |
33 |
| - fontsRemoved.push('*.svg'); |
34 |
| - fontsRemoved.push('*.scss'); |
| 3 | +import { Logger } from '../logger/logger'; |
| 4 | +import * as Constants from '../util/constants'; |
| 5 | +import { getStringPropertyValue, readDirAsync, unlinkAsync } from '../util/helpers'; |
| 6 | +import { BuildContext } from '../util/interfaces'; |
35 | 7 |
|
36 |
| - // all cordova builds should remove Noto-Sans |
37 |
| - // Only windows would use Noto-Sans, and it already comes with |
38 |
| - // a system font so it wouldn't need our own copy. |
39 |
| - fontsRemoved.push('noto-sans*'); |
40 | 8 |
|
41 |
| - if (context.platform === 'android') { |
42 |
| - // Remove all Roboto fonts. Android already comes with Roboto system |
43 |
| - // fonts so shipping our own is unnecessary. Including roboto fonts |
44 |
| - // is only useful for PWAs and during development. |
45 |
| - fontsRemoved.push('roboto*'); |
| 9 | +// For webapps, we pretty much need all fonts to be available because |
| 10 | +// the web server deployment never knows which browser/platform is |
| 11 | +// opening the app. Additionally, webapps will request fonts on-demand, |
| 12 | +// so having them all sit in the www/assets/fonts directory doesn’t |
| 13 | +// hurt anything if it’s never being requested. |
| 14 | + |
| 15 | +// However, with Cordova, the entire directory gets bundled and |
| 16 | +// shipped in the ipa/apk, but we also know exactly which platform |
| 17 | +// is opening the webapp. For this reason we can safely delete font |
| 18 | +// files we know would never be opened by the platform. So app-scripts |
| 19 | +// will continue to copy all font files over, but the cordova build |
| 20 | +// process would delete those we know are useless and just taking up |
| 21 | +// space. End goal is that the Cordova ipa/apk filesize is smaller. |
| 22 | + |
| 23 | +// Font Format Support: |
| 24 | +// ttf: http://caniuse.com/#feat=ttf |
| 25 | +// woff: http://caniuse.com/#feat=woff |
| 26 | +// woff2: http://caniuse.com/#feat=woff2 |
| 27 | +export function removeUnusedFonts(context: BuildContext): Promise<any> { |
| 28 | + const fontDir = getStringPropertyValue(Constants.ENV_VAR_FONTS_DIR); |
| 29 | + return readDirAsync(fontDir).then((fileNames: string[]) => { |
| 30 | + fileNames = fileNames.sort(); |
| 31 | + const toPurge = getFontFileNamesToPurge(context.target, context.platform, fileNames); |
| 32 | + const fullPaths = toPurge.map(fileName => join(fontDir, fileName)); |
| 33 | + const promises = fullPaths.map(fullPath => unlinkAsync(fullPath)); |
| 34 | + return Promise.all(promises); |
| 35 | + }); |
| 36 | +} |
46 | 37 |
|
47 |
| - } else if (context.platform === 'ios') { |
48 |
| - // Keep Roboto for now. Apps built for iOS may still use Material Design, |
49 |
| - // so in that case Roboto should be available. Later we can improve the |
50 |
| - // CLI to be smarter and read the user’s ionic config. Also, the roboto |
51 |
| - // fonts themselves are pretty small. |
| 38 | +export function getFontFileNamesToPurge(target: string, platform: string, fileNames: string[]): string[] { |
| 39 | + if (target !== Constants.CORDOVA) { |
| 40 | + return []; |
| 41 | + } |
| 42 | + const filesToDelete = new Set<string>(); |
| 43 | + for (const fileName of fileNames) { |
| 44 | + if (platform === 'android') { |
| 45 | + // remove noto-sans, roboto, and non-woff ionicons |
| 46 | + if (fileName.startsWith('noto-sans') || fileName.startsWith('roboto') || (isIonicons(fileName) && !isWoof(fileName))) { |
| 47 | + filesToDelete.add(fileName); |
| 48 | + } |
| 49 | + } else if (platform === 'ios') { |
| 50 | + // remove noto-sans, non-woff ionicons |
| 51 | + if (fileName.startsWith('noto-sans') || (fileName.startsWith('roboto') && !isWoof(fileName)) || (isIonicons(fileName) && !isWoof(fileName))) { |
| 52 | + filesToDelete.add(fileName); |
| 53 | + } |
52 | 54 | }
|
| 55 | + // for now don't bother deleting anything for windows, need to get some info first |
53 | 56 |
|
54 |
| - let filesToDelete: string[] = []; |
55 |
| - |
56 |
| - let promises = fontsRemoved.map(pattern => { |
57 |
| - return new Promise(resolve => { |
58 |
| - let searchPattern = join(context.wwwDir, 'assets', 'fonts', pattern); |
59 |
| - |
60 |
| - glob(searchPattern, (err, files) => { |
61 |
| - if (err) { |
62 |
| - Logger.error(`removeUnusedFonts: ${err}`); |
63 |
| - |
64 |
| - } else { |
65 |
| - files.forEach(f => { |
66 |
| - if (filesToDelete.indexOf(f) === -1) { |
67 |
| - filesToDelete.push(f); |
68 |
| - } |
69 |
| - }); |
70 |
| - } |
71 |
| - |
72 |
| - resolve(); |
73 |
| - }); |
74 |
| - |
75 |
| - }); |
76 |
| - }); |
77 |
| - |
78 |
| - return Promise.all(promises).then(() => { |
79 |
| - return unlinkAsync(filesToDelete).then(() => { |
80 |
| - if (filesToDelete.length) { |
81 |
| - Logger.info(`removed unused font files`); |
82 |
| - return true; |
83 |
| - } |
84 |
| - return false; |
85 |
| - }); |
86 |
| - }); |
87 | 57 | }
|
| 58 | + return Array.from(filesToDelete); |
| 59 | +} |
| 60 | + |
| 61 | +function isIonicons(fileName: string) { |
| 62 | + return fileName.startsWith('ionicons'); |
| 63 | +} |
88 | 64 |
|
89 |
| - // nothing to do here, carry on |
90 |
| - return Promise.resolve(); |
| 65 | +// woof woof |
| 66 | +function isWoof(fileName: string) { |
| 67 | + return extname(fileName) === '.woff' || extname(fileName) === '.woff2'; |
91 | 68 | }
|
0 commit comments