Skip to content

Commit

Permalink
Move some code into a collectTextsByPage function
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Aug 5, 2022
1 parent 38bf36e commit 1dff381
Showing 1 changed file with 68 additions and 52 deletions.
120 changes: 68 additions & 52 deletions lib/subsetFonts.js
Expand Up @@ -664,62 +664,14 @@ These glyphs are used on your site, but they don't exist in the font you applied
}
}

async function subsetFonts(
async function collectTextsByPage(
assetGraph,
{
formats = ['woff2', 'woff'],
subsetPath = 'subfont/',
omitFallbacks = false,
inlineCss,
fontDisplay,
hrefType = 'rootRelative',
onlyInfo,
dynamic,
console = global.console,
text,
} = {}
htmlOrSvgAssets,
{ text, console, dynamic = false } = {}
) {
if (!validFontDisplayValues.includes(fontDisplay)) {
fontDisplay = undefined;
}

const htmlOrSvgAssetTextsWithProps = [];
const subsetUrl = urltools.ensureTrailingSlash(assetGraph.root + subsetPath);

await assetGraph.applySourceMaps({ type: 'Css' });

await assetGraph.populate({
followRelations: {
$or: [
{
to: {
url: { $regex: googleFontsCssUrlRegex },
},
},
{
type: 'CssFontFaceSrc',
from: {
url: { $regex: googleFontsCssUrlRegex },
},
},
],
},
});

// Collect texts by page

const memoizedGetCssRulesByProperty = memoizeSync(getCssRulesByProperty);
const htmlOrSvgAssets = assetGraph.findAssets({
$or: [
{
type: 'Html',
isInline: false,
},
{
type: 'Svg',
},
],
});
const traversalRelationQuery = {
$or: [
{
Expand All @@ -735,7 +687,6 @@ async function subsetFonts(
};

const fontFaceDeclarationsByHtmlOrSvgAsset = new Map();
const potentiallyOrphanedAssets = new Set();

const headlessBrowser = dynamic && new HeadlessBrowser({ console });
const globalTextByProps = [];
Expand Down Expand Up @@ -860,7 +811,72 @@ async function subsetFonts(
}
}
}
return { htmlOrSvgAssetTextsWithProps, fontFaceDeclarationsByHtmlOrSvgAsset };
}

async function subsetFonts(
assetGraph,
{
formats = ['woff2', 'woff'],
subsetPath = 'subfont/',
omitFallbacks = false,
inlineCss,
fontDisplay,
hrefType = 'rootRelative',
onlyInfo,
dynamic,
console = global.console,
text,
} = {}
) {
if (!validFontDisplayValues.includes(fontDisplay)) {
fontDisplay = undefined;
}

const subsetUrl = urltools.ensureTrailingSlash(assetGraph.root + subsetPath);

await assetGraph.applySourceMaps({ type: 'Css' });

await assetGraph.populate({
followRelations: {
$or: [
{
to: {
url: { $regex: googleFontsCssUrlRegex },
},
},
{
type: 'CssFontFaceSrc',
from: {
url: { $regex: googleFontsCssUrlRegex },
},
},
],
},
});

const htmlOrSvgAssets = assetGraph.findAssets({
$or: [
{
type: 'Html',
isInline: false,
},
{
type: 'Svg',
},
],
});

// Collect texts by page

const { htmlOrSvgAssetTextsWithProps, fontFaceDeclarationsByHtmlOrSvgAsset } =
await collectTextsByPage(assetGraph, htmlOrSvgAssets, {
text,
console,
dynamic,
});

const potentiallyOrphanedAssets = new Set();
if (omitFallbacks) {
for (const htmlOrSvgAsset of htmlOrSvgAssets) {
const accumulatedFontFaceDeclarations =
Expand Down

0 comments on commit 1dff381

Please sign in to comment.