Skip to content

Commit

Permalink
fix(optimize-css): do not remove custom @font rules (#66)
Browse files Browse the repository at this point in the history
Fixes #65

Currently, `preserveAllIBMFonts: false` (the default configuration), will remove non-product IBM `@font-face` rules. However, it also unintentionally removes custom `@font-face` rules. The fix is to first check that the rule-to-remove is actually IBM Plex.
  • Loading branch information
metonym authored Jul 28, 2024
1 parent bb56136 commit 6157e1a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/plugins/create-optimized-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ export function createOptimizedCss(options: CreateOptimizedCssOptions) {
}
});

// Do not proceed if font is not IBM Plex.
if (!attributes["font-family"].startsWith("IBM Plex")) {
return;
}

const is_mono =
attributes["font-style"] === "normal" &&
attributes["font-family"] === "IBM Plex Mono" &&
Expand Down
12 changes: 11 additions & 1 deletion tests/__snapshots__/create-optimized-css.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ exports[`create-optimized-css removes unused selectors 1`] = `
"
`;

exports[`create-optimized-css removes unused @font rules 1`] = `
exports[`create-optimized-css removes unused IBM @font rules 1`] = `
"
@font-face {
font-family: 'CustomFont';
src: url('path/to/custom-font.ttf');
}
@font-face {
font-display: auto;
font-family: IBM Plex Mono;
Expand All @@ -36,6 +41,11 @@ exports[`create-optimized-css removes unused @font rules 1`] = `

exports[`create-optimized-css preserves all IBM fonts 1`] = `
"
@font-face {
font-family: 'CustomFont';
src: url('path/to/custom-font.ttf');
}
@font-face {
font-display: auto;
font-family: IBM Plex Mono;
Expand Down
5 changes: 5 additions & 0 deletions tests/create-optimized-css.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ describe("create-optimized-css", () => {
});

const font_rules = `
@font-face {
font-family: 'CustomFont';
src: url('path/to/custom-font.ttf');
}
@font-face {
font-display: auto;
font-family: IBM Plex Mono;
Expand Down

0 comments on commit 6157e1a

Please sign in to comment.