@42-central/vite-plugin-theme

中文文档
0.26.0发布于 2026/11/05UNLICENSEDTypeScriptES Module近 30 天下载 1966

Vite plugin that injects @42/ui-react's anti-FOUC theme init script into index.html

原始使用文档
文档目录9 节
  1. Why
  2. Install
  3. Usage
  4. Options
  5. Keep it in sync with useTheme()
  6. Content Security Policy
  7. What it injects
  8. Not using Vite?
  9. Links

@42-central/vite-plugin-theme

npm version Vite 7 | 8 ESM only

Install · Usage · Options · Theming docs ↗


A Vite plugin that stops @42-central/ui-react apps from flashing the wrong theme on load. It puts the kit's theme-init script at the top of <head> in index.html, in both vite dev and vite build.

Why

useTheme() stores the visitor's "light" | "dark" | "system" choice and keeps data-theme on <html> in sync. React only runs after first paint, though. So without help, a dark-mode visitor sees one light frame before the hook catches up.

In Next.js, <ThemeScript /> covers that gap from the server render. A Vite SPA has no server render, so the script has to sit in index.html and block parsing. It also can't be type="module", because Vite defers module scripts, and the flash comes back. This plugin injects a classic inline <script> through Vite's transformIndexHtml hook, so you never edit the HTML by hand.

Install

Install it as a dev dependency, next to the kit:

npm install -D @42-central/vite-plugin-theme
pnpm add -D @42-central/vite-plugin-theme
yarn add --dev @42-central/vite-plugin-theme
bun add --dev @42-central/vite-plugin-theme

vite 7 or 8 is a peer dependency. The plugin builds its script with @42-central/ui-react, which it lists as its own dependency. Keep both packages on the same version: they are released together.

Usage

// vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { themeInitPlugin } from "@42-central/vite-plugin-theme";

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

Then read and change the theme from any component with the kit's hook:

import { useTheme } from "@42-central/ui-react";

export function ThemeToggle() {
  const { resolvedTheme, setMode } = useTheme();
  return (
    <button onClick={() => setMode(resolvedTheme === "dark" ? "light" : "dark")}>
      Toggle theme
    </button>
  );
}

Options

themeInitPlugin(options?) takes the same options as the kit's buildThemeInitScript(), plus nonce:

OptionTypeDefaultWhat it does
storageKeystring"ui-theme"localStorage key that holds the stored mode.
defaultColorScheme"light" | "dark" | "system""system"Mode used when nothing is stored yet.
forceColorScheme"light" | "dark"–Pins the theme and ignores both storage and the OS preference.
attribute"data-theme" | "class", or an array of them"data-theme"What to write on <html>: the data-theme attribute, a .dark class, or both.
noncestring–CSP nonce added to the injected <script>.

Keep it in sync with useTheme()

There is no provider holding this configuration, so the script and the hook can't share it. Pass storageKey, defaultColorScheme, forceColorScheme and attribute with the same values to the plugin and to every useTheme() call. If they differ, the script and the hook disagree on first load.

// vite.config.ts
themeInitPlugin({ forceColorScheme: "dark", attribute: ["class", "data-theme"] });
// anywhere in the app
useTheme({ forceColorScheme: "dark", attribute: ["class", "data-theme"] });

A shared const THEME_OPTIONS = { … } imported by both files is the easiest way to keep them aligned.

Content Security Policy

If your CSP blocks inline scripts, pass the nonce your server puts in the script-src directive:

themeInitPlugin({ nonce: "{{CSP_NONCE}}" });

The value goes into the built index.html verbatim. Use whatever placeholder your server or edge function replaces per request.

What it injects

One tag, first in <head>:

<head>
  <script>/* ~600 bytes: read localStorage, resolve "system", set data-theme */</script>
  <!-- the rest of your head -->
</head>

The script runs once and registers no listeners. Following the OS preference live after load is useTheme()'s job.

Not using Vite?

The ui-react README covers the alternatives: a prebuilt theme-init.js you copy into public/, or pasting the output of buildThemeInitScript() by hand. Next.js apps use <ThemeScript /> instead.

版本与发布记录

@42-central/vite-plugin-theme 共发布 18 个版本,首次发布于 2026/10/08;过去 12 个月发布了 18 个版本。

版本发布日期距今
0.26.0latest2026/11/053 天前
0.25.02026/11/025 天前
0.24.02026/10/299 天前
0.23.02026/10/2810 天前
0.22.02026/10/2712 天前
0.20.22026/10/2217 天前

关于 @42-central/vite-plugin-theme 的常见问题

@42-central/vite-plugin-theme 是什么?

Vite plugin that injects @42/ui-react's anti-FOUC theme init script into index.html

如何安装 @42-central/vite-plugin-theme?

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

@42-central/vite-plugin-theme 支持 TypeScript 吗?

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

@42-central/vite-plugin-theme 支持 ESM 还是 CommonJS?

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

@42-central/vite-plugin-theme 可以商用吗?

@42-central/vite-plugin-theme 使用 UNLICENSED 许可。作者声明为 UNLICENSED,即不授予公开使用许可(与公共领域的 Unlicense 不同)。使用前需获得作者授权。以上为概括,不构成法律意见,请以许可原文为准。

@42-central/vite-plugin-theme 的下载量有多少?

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

@42-central/vite-plugin-theme 还在维护吗?

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