Skip to content

Commit

Permalink
ignore commented links (#42)
Browse files Browse the repository at this point in the history
* ignore commented links

* test for commented links

* /gs modifiers

* distinct fixture urls

* check webfontsCss content refactor

---------

Co-authored-by: Bálint Szekeres <szekeres@b4lint.hu>
  • Loading branch information
mateenagy and 0xb4lint committed Nov 3, 2023
1 parent 93b7e0b commit 5a6dee5
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 28 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-webfont-dl",
"version": "3.8.0",
"version": "3.8.1",
"description": "Vite plugin for downloading and injecting webfonts",
"keywords": [
"vite",
Expand Down
24 changes: 13 additions & 11 deletions src/index-html-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ export class IndexHtmlProcessor {
// <link href="..." rel="stylesheet">

// https://fonts.googleapis.com
/<link[^>]+rel=['"]?stylesheet['"]?[^>]+href=['"]?(https:\/\/fonts\.googleapis\.com[^'">]+)['"]?[^>]*>/g,
/<link[^>]+href=['"]?(https:\/\/fonts\.googleapis\.com[^'">]+)['"]?[^>]+rel=['"]?stylesheet['"]?[^>]*>/g,
/(<!--(?!-->).*?)?<link[^>]+rel=['"]?stylesheet['"]?[^>]+href=['"]?(https:\/\/fonts\.googleapis\.com[^'">]+)['"]?[^>]*>/gs,
/(<!--(?!-->).*?)?<link[^>]+href=['"]?(https:\/\/fonts\.googleapis\.com[^'">]+)['"]?[^>]+rel=['"]?stylesheet['"]?[^>]*>/gs,

// https://fonts.bunny.net
/<link[^>]+rel=['"]?stylesheet['"]?[^>]+href=['"]?(https:\/\/fonts\.bunny\.net[^'">]+)['"]?[^>]*>/g,
/<link[^>]+href=['"]?(https:\/\/fonts\.bunny\.net[^'">]+)['"]?[^>]+rel=['"]?stylesheet['"]?[^>]*>/g,
/(<!--(?!-->).*?)?<link[^>]+rel=['"]?stylesheet['"]?[^>]+href=['"]?(https:\/\/fonts\.bunny\.net[^'">]+)['"]?[^>]*>/gs,
/(<!--(?!-->).*?)?<link[^>]+href=['"]?(https:\/\/fonts\.bunny\.net[^'">]+)['"]?[^>]+rel=['"]?stylesheet['"]?[^>]*>/gs,

// https://api.fontshare.com
/<link[^>]+rel=['"]?stylesheet['"]?[^>]+href=['"]?(https:\/\/api\.fontshare\.com[^'">]+)['"]?[^>]*>/g,
/<link[^>]+href=['"]?(https:\/\/api\.fontshare\.com[^'">]+)['"]?[^>]+rel=['"]?stylesheet['"]?[^>]*>/g,
/(<!--(?!-->).*?)?<link[^>]+rel=['"]?stylesheet['"]?[^>]+href=['"]?(https:\/\/api\.fontshare\.com[^'">]+)['"]?[^>]*>/gs,
/(<!--(?!-->).*?)?<link[^>]+href=['"]?(https:\/\/api\.fontshare\.com[^'">]+)['"]?[^>]+rel=['"]?stylesheet['"]?[^>]*>/gs,

// https://cdn.jsdelivr.net
/<link[^>]+rel=['"]?stylesheet['"]?[^>]+href=['"]?(https:\/\/cdn\.jsdelivr\.net[^'">]+\.css)['"]?[^>]*>/g,
/<link[^>]+href=['"]?(https:\/\/cdn\.jsdelivr\.net[^'">]+\.css)['"]?[^>]+rel=['"]?stylesheet['"]?[^>]*>/g,
/(<!--(?!-->).*?)?<link[^>]+rel=['"]?stylesheet['"]?[^>]+href=['"]?(https:\/\/cdn\.jsdelivr\.net[^'">]+\.css)['"]?[^>]*>/gs,
/(<!--(?!-->).*?)?<link[^>]+href=['"]?(https:\/\/cdn\.jsdelivr\.net[^'">]+\.css)['"]?[^>]+rel=['"]?stylesheet['"]?[^>]*>/gs,

// https://rsms.me
/<link[^>]+rel=['"]?stylesheet['"]?[^>]+href=['"]?(https:\/\/rsms\.me[^'">]+)['"]?[^>]*>/g,
/<link[^>]+href=['"]?(https:\/\/rsms\.me[^'">]+)['"]?[^>]+rel=['"]?stylesheet['"]?[^>]*>/g,
/(<!--(?!-->).*?)?<link[^>]+rel=['"]?stylesheet['"]?[^>]+href=['"]?(https:\/\/rsms\.me[^'">]+)['"]?[^>]*>/gs,
/(<!--(?!-->).*?)?<link[^>]+href=['"]?(https:\/\/rsms\.me[^'">]+)['"]?[^>]+rel=['"]?stylesheet['"]?[^>]*>/gs,
];

private preconnectRegexes = [
Expand Down Expand Up @@ -59,7 +59,9 @@ export class IndexHtmlProcessor {

if (matches) {
for (const match of matches) {
webfontUrls.add(match[1]);
if (!match[1]) {
webfontUrls.add(match[2]);
}
}
}
}
Expand Down
32 changes: 18 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,24 +399,28 @@ function viteWebfontDownload(
collectWebfontsFromBundleCss(bundle);

try {
parseFonts(await downloadWebfontCss());
await downloadFonts();
replaceFontUrls();
const webfontsCss = await downloadWebfontCss();

if (options.injectAsStyleTag === false || !htmlFiles.size) {
saveCss();
}
if (webfontsCss) {
parseFonts(webfontsCss);
await downloadFonts();
replaceFontUrls();

htmlFiles.forEach((html, htmlPath) => {
const asset = bundle[htmlPath] as OutputAsset;
if (options.injectAsStyleTag === false || !htmlFiles.size) {
saveCss();
}

if (asset !== undefined) {
asset.source = indexHtmlProcessor.removeTags(asset.source as string);
asset.source = injectToHtml(asset.source, cssContent);
htmlFiles.forEach((html, htmlPath) => {
const asset = bundle[htmlPath] as OutputAsset;

htmlFiles.set(htmlPath, asset.source);
}
});
if (asset !== undefined) {
asset.source = indexHtmlProcessor.removeTags(asset.source as string);
asset.source = injectToHtml(asset.source, cssContent);

htmlFiles.set(htmlPath, asset.source);
}
});
}
} catch (error) {
logger.error(
colors.red((error as Error).message)
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/index-bunny-fonts.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<meta charset="UTF-8">
<link rel="preconnect" href="https://fonts.bunny.net">
<link href="https://fonts.bunny.net/css2?family=Roboto+Mono:wght@300;700&family=Roboto:wght@300;700&display=swap" rel="stylesheet" />
<!-- <link href="https://fonts.bunny.net/css2?family=Roboto+Mono:wght@300;700&family=Roboto:wght@300;700&display=swap&v=2" rel="stylesheet" /> -->
<!--
<link href="https://fonts.bunny.net/css2?family=Roboto+Mono:wght@300;700&family=Roboto:wght@300;700&display=swap&v=3" rel="stylesheet" />
-->
</head>
<body>
<div id="app"></div>
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/index-fontshare.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<meta charset="UTF-8">
<link rel="preconnect" href="https://api.fontshare.com">
<link href="https://api.fontshare.com/v2/css?f[]=general-sans@200,300&f[]=cabinet-grotesk@200&display=swap" rel="stylesheet">
<!-- <link href="https://api.fontshare.com/v2/css?f[]=general-sans@200,300&f[]=cabinet-grotesk@200&display=swap&v=2" rel="stylesheet"> -->
<!--
<link href="https://api.fontshare.com/v2/css?f[]=general-sans@200,300&f[]=cabinet-grotesk@200&display=swap&v=3" rel="stylesheet">
-->
</head>
<body>
<div id="app"></div>
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/index-google-fonts.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@300;700&family=Roboto:wght@300;700&display=swap" rel="stylesheet">
<!-- <link href="https://fonts.googleapis.com/css2?family=Asap+Condensed:wght@700&family=Inter:wght@400;500;600;700&display=swap&v=2" rel="stylesheet"> -->
<!--
<link href="https://fonts.googleapis.com/css2?family=Asap+Condensed:wght@700&family=Inter:wght@400;500;600;700&display=swap&v=3" rel="stylesheet">
-->
</head>
<body>
<div id="app"></div>
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/index-jsdelivr.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/firacode@6.2.0/distr/fira_code.css">
<!-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/firacode@6.2.0/distr/fira_code.css?v=2"> -->
<!--
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/firacode@6.2.0/distr/fira_code.css?v=3">
-->
</head>
<body>
<div id="app"></div>
Expand Down

0 comments on commit 5a6dee5

Please sign in to comment.