@better-css-modules/core

中文文档
0.4.1发布于 2026/10/21MITTypeScriptES Module构建来源已验证近 30 天下载 9918

Analysis, type generation, unused-class detection, token checks, watcher and config for better-css-modules

原始使用文档
文档目录4 节
  1. Install
  2. The config file
  3. API
  4. License

@better-css-modules/core

The analysis behind better-css-modules: config loading, .d.ts generation and the checks. The CLI and every bundler plugin are built on it. Install it next to them for defineConfig, which the config file imports.

Install

pnpm add -D @better-css-modules/core

The config file

// better-css-modules.config.ts
import { defineConfig } from "@better-css-modules/core";

export default defineConfig({
  globalCss: ["./src/globals.css"],
  layer: "components",
});

defineConfig returns the config as it is and types it. The settings are described in the project README.

API

import {
  check,
  ConfigError,
  formatDiagnostic,
  generate,
  loadConfig,
} from "@better-css-modules/core";

try {
  const config = await loadConfig();
  const { files } = await generate(config);
  const { diagnostics, modules, tokens } = await check(config);
  for (const diagnostic of diagnostics) console.log(formatDiagnostic(diagnostic, process.cwd()));
  console.log(`${files.length} types, ${modules} modules, tokens: ${tokens.join(", ")}`);
} catch (error) {
  if (!(error instanceof ConfigError)) throw error;
  console.error(error.message);
  process.exitCode = 2;
}
  • loadConfig({ cwd?, config? }) loads the config file config names, relative to cwd, or the better-css-modules.config.* in cwd (by default the working directory), and returns it as a ResolvedConfig: the defaults filled in, checked, and with root, the directory every relative path starts from, and file, the config file or null.
  • generate(config) writes the .d.ts files and their declaration maps, removes the ones it wrote for stylesheets that are gone, and returns a GenerateResult: files, removed and the syntax diagnostics.
  • check(config) runs every check and returns a CheckResult: the sorted diagnostics, how many modules it checked, and the token categories (tokens) the global CSS restricts.
  • exportTokens(config) reads the global CSS and returns its tokens as a DTCG resolver document, the DtcgResolver the tokens command prints; see Token export.
  • formatDiagnostic(diagnostic, cwd) gives the file:line:col error rule: message line of the CLI, and formatGitHubAnnotation(diagnostic, cwd) the GitHub Actions workflow command of --format github.
  • ConfigError is a mistake in the config or in the files it names. loadConfig throws it, and so does check when include matches no file, the global CSS cannot be read, or it does not declare the layer, and exportTokens when the global CSS cannot be read or a token has a name DTCG cannot hold.
  • The types Config, ResolvedConfig, GenerateResult, CheckResult, Diagnostic, RuleId, DtcgResolver and DtcgToken describe these.

@better-css-modules/core/internal holds what the CLI and the plugins share. It is for those packages only and outside semver.

The rules check reports are described in the project README.

License

MIT

版本与发布记录

@better-css-modules/core 共发布 8 个版本,首次发布于 2026/03/27;过去 12 个月发布了 8 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.1latest2026/10/2117 天前
0.4.02026/10/1425 天前
0.3.02026/10/1325 天前
0.2.12026/10/1326 天前
0.2.02026/10/1029 天前
0.1.32026/07/223 个月前

关于 @better-css-modules/core 的常见问题

@better-css-modules/core 是什么?

Analysis, type generation, unused-class detection, token checks, watcher and config for better-css-modules

如何安装 @better-css-modules/core?

在项目目录运行 npm install @better-css-modules/core,或使用 pnpm add / yarn add / bun add。

@better-css-modules/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@better-css-modules/core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@better-css-modules/core 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@better-css-modules/core 可以商用吗?

@better-css-modules/core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@better-css-modules/core 的下载量有多少?

根据 npm 官方下载统计,@better-css-modules/core 在 2026/10/08 – 2026/11/06 这 30 天内被下载 9,918 次,日均约 331 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@better-css-modules/core 还在维护吗?

最新版本 0.4.1 发布于 2026/10/21(17 天前);至今共发布 8 个版本,其中过去 12 个月发布了 8 个;首次发布于 2026/03/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。