-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Issue #80 inserting every fontUsage (including unused font) for multiple html target #81
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -728,26 +728,29 @@ async function subsetFonts( | |
} | ||
|
||
if (!subsetPerPage) { | ||
const globalFontUsage = {}; | ||
|
||
// Gather all texts | ||
for (const htmlAssetTextWithProps of htmlAssetTextsWithProps) { | ||
for (const fontUsage of htmlAssetTextWithProps.fontUsages) { | ||
if (!globalFontUsage[fontUsage.fontUrl]) { | ||
globalFontUsage[fontUsage.fontUrl] = []; | ||
const fontUsagesConcat = _.flatMap( | ||
htmlAssetTextsWithProps, | ||
htmlAssetTextWithProps => htmlAssetTextWithProps.fontUsages | ||
); | ||
const globalFontUsageMap = fontUsagesConcat.reduce( | ||
(fontUsageMap, fontUsage) => { | ||
const newFontUsage = fontUsage; | ||
if (fontUsage.fontUrl in fontUsageMap) { | ||
newFontUsage.text = _.uniq( | ||
fontUsageMap[fontUsage.fontUrl].text + fontUsage.text | ||
) | ||
.sort() | ||
.join(''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, mental note that we should find a better way achieve the "unique across pages" thing, sorting the output of require('lodash').uniq('🤗').sort().join('')
'🤗'
require('lodash').uniq('🤗🤞').sort().join('')
'🤗�'
require('lodash').uniq('🤗🤞👊').sort().join('')
'���' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't have the same problem. Not very readable though: [...new Set([...'🤗🤞👊'])].sort().join('') There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! I think we should go with that, then. Not readable is better than broken :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implemented your fix here: #87 |
||
} | ||
return { ...fontUsageMap, [fontUsage.fontUrl]: newFontUsage }; | ||
}, | ||
{} | ||
); | ||
const globalFontUsage = Object.values(globalFontUsageMap); | ||
|
||
globalFontUsage[fontUsage.fontUrl].push(fontUsage.text); | ||
} | ||
} | ||
|
||
// Merge subset values, unique glyphs, sort | ||
// Gather all texts | ||
for (const htmlAssetTextWithProps of htmlAssetTextsWithProps) { | ||
for (const fontUsage of htmlAssetTextWithProps.fontUsages) { | ||
fontUsage.text = _.uniq(globalFontUsage[fontUsage.fontUrl].join('')) | ||
.sort() | ||
.join(''); | ||
} | ||
htmlAssetTextWithProps.fontUsages = globalFontUsage; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4250,9 +4250,9 @@ describe('subsetFonts', function() { | |
}); | ||
}); | ||
|
||
describe('with a page that does need subsetting and one that does', function() { | ||
describe('with a page that does NOT need subsetting and one that does', function() { | ||
// https://gitter.im/assetgraph/assetgraph?at=5dbb6438a3f0b17849c488cf | ||
it('should not short circuit because the first page does not need any subset fonts', async function() { | ||
it('should not short circuit with explicit subsetPerPage:true option because the first page does not need any subset fonts', async function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probably deserves to be its own test case alongside the existing one. However, if I copy your test over to master, it also passes? Can you come up with an actual regression test? |
||
const assetGraph = new AssetGraph({ | ||
root: pathModule.resolve( | ||
__dirname, | ||
|
@@ -4263,11 +4263,15 @@ describe('subsetFonts', function() { | |
await assetGraph.populate(); | ||
const { fontInfo } = await subsetFonts(assetGraph, { | ||
inlineFonts: false, | ||
omitFallbacks: true | ||
omitFallbacks: true, | ||
subsetPerPage: true | ||
}); | ||
|
||
expect(fontInfo, 'to satisfy', [ | ||
{ htmlAsset: /\/index-1\.html$/, fontUsages: [] }, | ||
{ | ||
htmlAsset: /\/index-1\.html$/, | ||
fontUsages: [] | ||
}, | ||
{ | ||
htmlAsset: /\/index-2\.html$/, | ||
fontUsages: [ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind turning this
reduce
construct into a regularfor
...of
loop? I find those much easier to reason about.