@better-css-modules/vite

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

Vite plugin for better-css-modules

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. Vite+ with vite aliased to its core
  4. Configuration
  5. License

@better-css-modules/vite

Vite plugin for better-css-modules. It generates the .d.ts files for CSS Modules when the dev server or a build starts, keeps them in sync while the dev server runs, and puts every module in the cascade layer the config names.

Install

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

The config file imports defineConfig from @better-css-modules/core, and the CLI runs check. The plugin needs Vite 8.

Usage

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

export default defineConfig({
  plugins: [betterCssModules()],
});

The types are generated once per process for each config, however many environments Vite builds and however many projects Vitest runs. The dev server reads the config when it starts; restart it after changing the config.

Vite+ with vite aliased to its core

The plugin declares vite ^8.0.0 as a peer dependency. A Vite+ project that declares vite as an alias of the Vite+ core, such as "vite": "npm:@voidzero-dev/[email protected]" in a catalog, installs a package whose own version is outside that range. pnpm then reports the peer as unmet, as it does for every Vite plugin, and npm stops with ERESOLVE. vp migrate relaxes the peer for you; if you set up the alias yourself, do the same.

With pnpm, in pnpm-workspace.yaml:

peerDependencyRules:
  allowAny:
    - vite
  allowedVersions:
    vite: "*"

With npm, override vite with the same alias in package.json:

{
  "overrides": {
    "vite": "npm:@voidzero-dev/[email protected]"
  }
}

Configuration

The plugin takes one option, the config file to use, relative to the working directory: betterCssModules({ config: "apps/web/better-css-modules.config.ts" }). By default it reads the better-css-modules.config.* in the working directory, the same file the CLI reads. The directory of the config file is the project root, and include, outDir and the search for sources stay inside it, so put the config in a directory that holds the stylesheets and the sources. See the project README for the Quick Start, which sets up tsconfig.json, the configuration and cascade layers.

License

MIT

版本与发布记录

@better-css-modules/vite 共发布 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/vite 的常见问题

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

Vite plugin for better-css-modules

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

在项目目录运行 npm install @better-css-modules/vite,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vite(^8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

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

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

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

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

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

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

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

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

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

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

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

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