|
@@ -38,6 +38,7 @@ this.fontsMinifier = this.fontsMinifier || (() => {
|
|
|
const REGEXP_FONT_FORMAT_VALUE = /format\((.*?)\)\s*,?$/;
|
|
const REGEXP_FONT_FORMAT_VALUE = /format\((.*?)\)\s*,?$/;
|
|
|
const REGEXP_FONT_SRC = /(.*?)\s*,?$/;
|
|
const REGEXP_FONT_SRC = /(.*?)\s*,?$/;
|
|
|
const EMPTY_URL_SOURCE = "url(\"data:base64,\")";
|
|
const EMPTY_URL_SOURCE = "url(\"data:base64,\")";
|
|
|
|
|
+ const LOCAL_SOURCE = "local(";
|
|
|
const PSEUDO_ELEMENTS = ["::after", "::before", "::first-line", "::first-letter", ":before", ":after", ":first-line", ":first-letter", "::placeholder", "::selection", "::marker", "::cue", "::slotted", "::spelling-error", "::grammar-error"];
|
|
const PSEUDO_ELEMENTS = ["::after", "::before", "::first-line", "::first-letter", ":before", ":after", ":first-line", ":first-letter", "::placeholder", "::selection", "::marker", "::cue", "::slotted", "::spelling-error", "::grammar-error"];
|
|
|
const FONT_WEIGHTS = {
|
|
const FONT_WEIGHTS = {
|
|
|
normal: "400",
|
|
normal: "400",
|
|
@@ -230,36 +231,26 @@ this.fontsMinifier = this.fontsMinifier || (() => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function processFontFaceRule(cssRule, fontInfo, stats) {
|
|
function processFontFaceRule(cssRule, fontInfo, stats) {
|
|
|
- const fontTest = (fontSource, format) => !fontSource.src.startsWith(EMPTY_URL_SOURCE) && fontSource.format == format;
|
|
|
|
|
- let woffFontFound = fontInfo.find(fontSource => fontTest(fontSource, "woff2-variations"));
|
|
|
|
|
- if (!woffFontFound) {
|
|
|
|
|
- woffFontFound = fontInfo.find(fontSource => fontTest(fontSource, "woff2"));
|
|
|
|
|
- }
|
|
|
|
|
- if (!woffFontFound) {
|
|
|
|
|
- woffFontFound = fontInfo.find(fontSource => fontTest(fontSource, "woff"));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const findSource = fontFormat => fontInfo.find(source => source.src != EMPTY_URL_SOURCE && source.format == fontFormat);
|
|
|
|
|
+ const filterSource = fontSource => fontInfo.filter(source => source == fontSource || source.src.startsWith(LOCAL_SOURCE));
|
|
|
stats.fonts.processed += fontInfo.length;
|
|
stats.fonts.processed += fontInfo.length;
|
|
|
stats.fonts.discarded += fontInfo.length;
|
|
stats.fonts.discarded += fontInfo.length;
|
|
|
|
|
+ let woffFontFound = findSource("woff2-variations") || findSource("woff2") || findSource("woff");
|
|
|
|
|
+ let filteredFontInfo = fontInfo;
|
|
|
if (woffFontFound) {
|
|
if (woffFontFound) {
|
|
|
- fontInfo = [woffFontFound];
|
|
|
|
|
|
|
+ filteredFontInfo = filterSource(woffFontFound);
|
|
|
} else {
|
|
} else {
|
|
|
- let ttfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "truetype-variations"));
|
|
|
|
|
- if (!ttfFontFound) {
|
|
|
|
|
- ttfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "truetype"));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ let ttfFontFound = findSource("truetype-variations") || findSource("truetype");
|
|
|
if (ttfFontFound) {
|
|
if (ttfFontFound) {
|
|
|
- fontInfo = [ttfFontFound];
|
|
|
|
|
|
|
+ filteredFontInfo = filterSource(ttfFontFound);
|
|
|
} else {
|
|
} else {
|
|
|
- let otfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "opentype"));
|
|
|
|
|
- if (!otfFontFound) {
|
|
|
|
|
- otfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "embedded-opentype"));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ let otfFontFound = findSource("opentype") || findSource("embedded-opentype");
|
|
|
if (otfFontFound) {
|
|
if (otfFontFound) {
|
|
|
- fontInfo = [otfFontFound];
|
|
|
|
|
|
|
+ filteredFontInfo = filterSource(otfFontFound);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- stats.fonts.discarded -= fontInfo.length;
|
|
|
|
|
|
|
+ stats.fonts.discarded -= filteredFontInfo.length;
|
|
|
const removedNodes = [];
|
|
const removedNodes = [];
|
|
|
for (let node = cssRule.block.children.head; node; node = node.next) {
|
|
for (let node = cssRule.block.children.head; node; node = node.next) {
|
|
|
if (node.data.property == "src") {
|
|
if (node.data.property == "src") {
|
|
@@ -270,7 +261,8 @@ this.fontsMinifier = this.fontsMinifier || (() => {
|
|
|
removedNodes.forEach(node => cssRule.block.children.remove(node));
|
|
removedNodes.forEach(node => cssRule.block.children.remove(node));
|
|
|
const srcDeclaration = cssRule.block.children.filter(node => node.property == "src").tail;
|
|
const srcDeclaration = cssRule.block.children.filter(node => node.property == "src").tail;
|
|
|
if (srcDeclaration) {
|
|
if (srcDeclaration) {
|
|
|
- srcDeclaration.data.value = cssTree.parse(fontInfo.map(fontSource => fontSource.src).join(","), { context: "value" });
|
|
|
|
|
|
|
+ filteredFontInfo.reverse();
|
|
|
|
|
+ srcDeclaration.data.value = cssTree.parse(filteredFontInfo.map(fontSource => fontSource.src).join(","), { context: "value" });
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|