From 8bb72e175fec3f53b1cd88a829caac3d03ddc75d Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Sat, 2 Nov 2019 16:05:22 +0100 Subject: [PATCH] Avoid failing in the code that counts the savings when fonttools isn't available --- lib/subfont.js | 25 +++++++++++++++++-------- test/subfont.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/lib/subfont.js b/lib/subfont.js index 89d1bbd6..0fefbd3f 100644 --- a/lib/subfont.js +++ b/lib/subfont.js @@ -250,7 +250,7 @@ module.exports = async function subfont( let maxUsedCodePoints = 0; let maxOriginalCodePoints = 0; for (const fontUsage of fontUsages) { - sumSmallestSubsetSize += fontUsage.smallestSubsetSize; + sumSmallestSubsetSize += fontUsage.smallestSubsetSize || 0; sumSmallestOriginalSize += fontUsage.smallestOriginalSize; maxUsedCodePoints = Math.max( fontUsage.codepoints.used.length, @@ -279,6 +279,21 @@ module.exports = async function subfont( const variantShortName = `${fontUsage.props['font-weight']}${ fontUsage.props['font-style'] === 'italic' ? 'i' : ' ' }`; + let status; + if ( + fontUsage.smallestOriginalSize !== undefined && + fontUsage.smallestSubsetSize !== undefined + ) { + status = `${prettyBytes(fontUsage.smallestOriginalSize)} (${ + fontUsage.smallestOriginalFormat + }) => ${prettyBytes(fontUsage.smallestSubsetSize)} (${ + fontUsage.smallestSubsetFormat + }`; + totalSavings += + fontUsage.smallestOriginalSize - fontUsage.smallestSubsetSize; + } else { + status = 'no subset font created'; + } console.log( ` ${variantShortName}: ${String( fontUsage.codepoints.used.length @@ -286,14 +301,8 @@ module.exports = async function subfont( fontUsage.codepoints.original.length ).padStart( String(maxOriginalCodePoints).length - )} codepoints used, ${prettyBytes(fontUsage.smallestOriginalSize)} (${ - fontUsage.smallestOriginalFormat - }) => ${prettyBytes(fontUsage.smallestSubsetSize)} (${ - fontUsage.smallestSubsetFormat - })` + )} codepoints used, ${status}` ); - totalSavings += - fontUsage.smallestOriginalSize - fontUsage.smallestSubsetSize; } } } diff --git a/test/subfont.js b/test/subfont.js index bf8daf72..4ad17758 100644 --- a/test/subfont.js +++ b/test/subfont.js @@ -5,6 +5,7 @@ const expect = require('unexpected') .use(require('unexpected-sinon')); const subfont = require('../lib/subfont'); const httpception = require('httpception'); +const proxyquire = require('proxyquire'); const pathModule = require('path'); const openSansBold = require('fs').readFileSync( pathModule.resolve( @@ -407,4 +408,35 @@ describe('subfont', function() { expect(mockConsole.error, 'was not called'); }); }); + + describe('without fonttools available', function() { + const subfontWithoutFontTools = proxyquire('../lib/subfont', { + '../lib/subsetFonts': proxyquire('../lib/subsetFonts', { + './subsetLocalFont': null + }) + }); + + // Regression test for pretty-bytes(NaN) error + it('should not fail', async function() { + const root = encodeURI( + `file://${pathModule.resolve( + __dirname, + '..', + 'testdata', + 'subsetFonts', + 'local-mixed' + )}` + ); + + await subfontWithoutFontTools( + { + root, + inputFiles: [`${root}/index.html`], + silent: true, + dryRun: true + }, + console + ); + }); + }); });