npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

postcss-font-guard

v0.4.2

Published

PostCSS plugin to guard font usage at build time — override, allow, or deny fonts to prevent licensing risks

Readme

postcss-font-guard

构建阶段字体管控插件,自动识别并剔除侵权字体风险。

快速上手

npm install postcss-font-guard --save-dev
// postcss.config.js
module.exports = {
  plugins: [require("postcss-font-guard")()],
};

无需任何配置。自动保留项目自定义字体(@font-face 声明的)和 CSS 通用族(sans-serifsystem-ui-apple-system 等),剔除其余系统字体名,用 sans-serif 兜底。

/* 输入 */
@font-face { font-family: "DIN-Bold"; src: url('/fonts/DIN-Bold.woff2'); }
body { font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, "PingFang SC", sans-serif; }
.price { font-family: "DIN-Bold", Arial, sans-serif; }
.label { font-family: Arial; }

/* 输出 */
@font-face { font-family: "DIN-Bold"; src: url('/fonts/DIN-Bold.woff2'); }
body { font-family: -apple-system, BlinkMacSystemFont, sans-serif; }
.price { font-family: "DIN-Bold", sans-serif; }
.label { font-family: sans-serif; }

配置项

零配置满足大多数场景。以下选项用于特殊需求:

| 选项 | 类型 | 默认值 | 说明 | |---|---|---|---| | fallback | string \| string[] \| null | "sans-serif" | 字体清空后的兜底值;设为 null 则移除空声明 | | allow | string[] \| (name) => boolean | - | 白名单模式,只保留匹配的字体 | | deny | string[] | - | 黑名单模式,只删除匹配的字体 | | override | string | - | 所有字体替换为此值 | | removeImports | string[] | - | 删除含指定域名的 @import | | fontVariables | string[] | 自动检测 | 需过滤的 CSS 变量名(默认自动检测 --*font-family*) | | verbose | boolean | false | 输出修改日志 |

优先级override > allow > deny > 零配置智能模式

sans-serifsystem-ui-apple-systemBlinkMacSystemFont 等 CSS 通用族 / 厂商关键字和 var() / env() 表达式始终保留。allowdeny 支持 * / ? 通配符。

场景示例

自定义兜底字体

require("postcss-font-guard")({ fallback: ["NotoSans-Thai", "sans-serif"] })

白名单:只保留指定字体

require("postcss-font-guard")({ allow: ['DIN*', 'Roboto*'] })

匹配的字体保留,其余删除。适合明确知道项目使用了哪些字体的场景。

黑名单:只删除指定字体

require("postcss-font-guard")({ deny: ['Helvetica Neue', 'Arial', 'PingFang SC'] })

匹配的字体删除,其余保留。适合只需移除少量已知侵权字体的场景。

全量替换

require("postcss-font-guard")({ override: "Inter, sans-serif" })

所有 font-familyfont 简写统一替换,最简单粗暴的方式。

移除字体 @import

require("postcss-font-guard")({ removeImports: ['fonts.googleapis.com'] })

CSS 变量自动过滤

自动识别并过滤变量名含 font-family 的 CSS 自定义属性(如 --adm-font-family)。非字体值(颜色、尺寸等)自动跳过,不会误改:

/* 输入 */  :root { --adm-font-family: -apple-system, Helvetica, Arial, sans-serif; }
/* 输出 */  :root { --adm-font-family: -apple-system, sans-serif; }

/* 非字体值不受影响 */
:root { --font-family-color: #fff; }       /* 不变 */
:root { --font-family-size: 16px; }        /* 不变 */

var() / env() 回退值中的侵权字体也会被过滤:

/* 输入 */  font-family: var(--font, "Helvetica Neue"), sans-serif;
/* 输出 */  font-family: var(--font), sans-serif;

如需过滤不含 font-family 的变量,用 fontVariables 显式指定:

require("postcss-font-guard")({ fontVariables: ['--theme-font', '--*-stack'] })

构建工具集成

Webpack / rspack:

// postcss.config.js
module.exports = {
  plugins: [require("postcss-font-guard")()],
};

Vite:

import fontGuard from "postcss-font-guard";
export default {
  css: { postcss: { plugins: [fontGuard()] } },
};

配合 Tailwind CSS:

Tailwind preflight 会注入系统字体栈,建议同时配置 Tailwind 字体:

// tailwind.config.js
module.exports = {
  theme: { fontFamily: { sans: ['NotoSans-Thai', 'sans-serif'] } },
};

兼容性

  • PostCSS 8.0+
  • 零运行时依赖

License

MIT