|
|
@@ -60,7 +60,7 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
|
|
|
};
|
|
|
|
|
|
return {
|
|
|
- process: async (doc, stylesheets) => {
|
|
|
+ process: async (doc, stylesheets, fontURLs) => {
|
|
|
const fontsDetails = {
|
|
|
fonts: new Map(),
|
|
|
medias: new Map(),
|
|
|
@@ -89,9 +89,9 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
|
|
|
const media = stylesheetInfo.mediaText;
|
|
|
if (cssRules) {
|
|
|
if (media && media != MEDIA_ALL) {
|
|
|
- await processFontFaceRules(cssRules, sheetIndex, fontsDetails.medias.get("media-" + sheetIndex + "-" + media), stats);
|
|
|
+ await processFontFaceRules(cssRules, sheetIndex, fontsDetails.medias.get("media-" + sheetIndex + "-" + media), fontURLs, stats);
|
|
|
} else {
|
|
|
- await processFontFaceRules(cssRules, sheetIndex, fontsDetails, stats);
|
|
|
+ await processFontFaceRules(cssRules, sheetIndex, fontsDetails, fontURLs, stats);
|
|
|
}
|
|
|
stats.rules.discarded -= cssRules.getSize();
|
|
|
}
|
|
|
@@ -169,7 +169,7 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
|
|
|
fontsDetails.medias.forEach(mediaFontsDetails => processFontDetails(mediaFontsDetails));
|
|
|
}
|
|
|
|
|
|
- async function processFontFaceRules(cssRules, sheetIndex, fontsDetails, stats) {
|
|
|
+ async function processFontFaceRules(cssRules, sheetIndex, fontsDetails, fontURLs, stats) {
|
|
|
const cssTree = singlefile.lib.vendor.cssTree;
|
|
|
const removedRules = [];
|
|
|
let mediaIndex = 0, supportsIndex = 0;
|
|
|
@@ -177,17 +177,17 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
|
|
|
const ruleData = cssRule.data;
|
|
|
if (ruleData.type == "Atrule" && ruleData.name == "media" && ruleData.block && ruleData.block.children && ruleData.prelude) {
|
|
|
const mediaText = cssTree.generate(ruleData.prelude);
|
|
|
- await processFontFaceRules(ruleData.block.children, sheetIndex, fontsDetails.medias.get("media-" + sheetIndex + "-" + mediaIndex + "-" + mediaText), stats);
|
|
|
+ await processFontFaceRules(ruleData.block.children, sheetIndex, fontsDetails.medias.get("media-" + sheetIndex + "-" + mediaIndex + "-" + mediaText), fontURLs, stats);
|
|
|
mediaIndex++;
|
|
|
} else if (ruleData.type == "Atrule" && ruleData.name == "supports" && ruleData.block && ruleData.block.children && ruleData.prelude) {
|
|
|
const supportsText = cssTree.generate(ruleData.prelude);
|
|
|
- await processFontFaceRules(ruleData.block.children, sheetIndex, fontsDetails.supports.get("supports-" + sheetIndex + "-" + supportsIndex + "-" + supportsText), stats);
|
|
|
+ await processFontFaceRules(ruleData.block.children, sheetIndex, fontsDetails.supports.get("supports-" + sheetIndex + "-" + supportsIndex + "-" + supportsText), fontURLs, stats);
|
|
|
supportsIndex++;
|
|
|
} else if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
|
|
|
const fontInfo = fontsDetails.fonts.get(getFontKey(ruleData));
|
|
|
if (fontInfo) {
|
|
|
fontsDetails.fonts.delete(getFontKey(ruleData));
|
|
|
- await processFontFaceRule(ruleData, fontInfo, stats);
|
|
|
+ await processFontFaceRule(ruleData, fontInfo, fontURLs, stats);
|
|
|
} else {
|
|
|
removedRules.push(cssRule);
|
|
|
}
|
|
|
@@ -196,8 +196,18 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
|
|
|
removedRules.forEach(cssRule => cssRules.remove(cssRule));
|
|
|
}
|
|
|
|
|
|
- async function processFontFaceRule(ruleData, fontInfo, stats) {
|
|
|
+ async function processFontFaceRule(ruleData, fontInfo, fontURLs, stats) {
|
|
|
const cssTree = singlefile.lib.vendor.cssTree;
|
|
|
+
|
|
|
+ const removedNodes = [];
|
|
|
+ for (let node = ruleData.block.children.head; node; node = node.next) {
|
|
|
+ if (node.data.property == "src") {
|
|
|
+ removedNodes.push(node);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ removedNodes.pop();
|
|
|
+ removedNodes.forEach(node => ruleData.block.children.remove(node));
|
|
|
+ const srcDeclaration = ruleData.block.children.filter(node => node.property == "src").tail;
|
|
|
await Promise.all(fontInfo.map(async source => {
|
|
|
if (FontFace) {
|
|
|
const fontFace = new FontFace("test-font", source.src);
|
|
|
@@ -206,7 +216,15 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
|
|
|
await fontFace.loaded;
|
|
|
source.valid = true;
|
|
|
} catch (error) {
|
|
|
- // ignored
|
|
|
+ const fontURL = fontURLs.get(srcDeclaration.data);
|
|
|
+ const fontFace = new FontFace("test-font", "url(" + fontURL + ")");
|
|
|
+ try {
|
|
|
+ await fontFace.load();
|
|
|
+ await fontFace.loaded;
|
|
|
+ source.valid = true;
|
|
|
+ } catch (error) {
|
|
|
+ // ignored
|
|
|
+ }
|
|
|
}
|
|
|
} else {
|
|
|
source.valid = true;
|
|
|
@@ -233,15 +251,6 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
|
|
|
}
|
|
|
}
|
|
|
stats.fonts.discarded -= fontInfo.length;
|
|
|
- const removedNodes = [];
|
|
|
- for (let node = ruleData.block.children.head; node; node = node.next) {
|
|
|
- if (node.data.property == "src") {
|
|
|
- removedNodes.push(node);
|
|
|
- }
|
|
|
- }
|
|
|
- removedNodes.pop();
|
|
|
- removedNodes.forEach(node => ruleData.block.children.remove(node));
|
|
|
- const srcDeclaration = ruleData.block.children.filter(node => node.property == "src").tail;
|
|
|
if (srcDeclaration) {
|
|
|
fontInfo.reverse();
|
|
|
try {
|