@42-central/vite-plugin-theme

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

将@42/ui-react的反FOUC主题 init脚本注入索引.html的Vite插件

中文使用文档
文档目录9 节
  1. 为什么
  2. 安装
  3. 使用量
  4. 选项
  5. 保持与 useTheme()
  6. 内容安全政策
  7. 注射什么药
  8. 不使用维特?
  9. 链接

@42-central/vite-plugin-theme

npm version Vite 7 | 8 ESM only

安装 • 妇女 使用量 • 妇女 选项 • 妇女 他们的博士们..


停止的 Vite 插件 @42-central/ui-react 应用程序在加载时闪出错误的主题。 里面放的是工具箱的主题动画脚本 在顶端 <head> 内 index.html,两者中 vite dev 财务报告和审定财务报表 vite build。 。 。 .

为什么

useTheme() 存储访客的 "light" | "dark" | "system" 选择和选择 保留 data-theme 打开 <html> 同步。 只有在第一次喷漆后才会反应, 虽然. 所以,没有帮助,一个黑暗模式的访客 看到一个光框之前 钩子赶上了.

在下一个.js, <ThemeScript /> 覆盖从服务器渲染的漏洞。 一个虚拟 SPA 没有服务器渲染,所以脚本必须坐入 index.html 块 解析。 这不可能 type="module",因为 Vite 推迟模块 剧本,而闪光又回来了. 此插件注入经典内线 <script> 通过Vite的 transformIndexHtml 钩子,所以你从来不编辑 HTML是手边的.

安装

安装为 dev 依赖, 套件旁边 :

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或8是一种同龄依赖. 插件用 @42-central/ui-react,它将其列为自己的依赖。 都留着 同版本的软件包:它们一起发行.

使用量

// 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()],
});

然后读取并更改主题 从任何组件与工具包的钩:

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>
  );
}

选项

themeInitPlugin(options?) 和工具箱一样 buildThemeInitScript()附加时 nonce数字 :

选项类型默认它会做什么
storageKeystring"ui-theme"localStorage 键,该键持有存储模式.
defaultColorScheme"light" | "dark" | "system""system"尚未存储时使用的模式 .
forceColorScheme"light" | "dark"– 说吧 –绑定主题并忽略存储和OS偏好.
attribute"data-theme" | "class",或者其中的数组"data-theme"写什么 <html>页:1 data-theme 属性, a .dark 或两者兼有.
noncestring– 说吧 –注入的CSP 非ce <script>。 。 。 .

保持与 useTheme()

没有提供者持有此配置, 所以脚本和钩子 无法分享 过 storageKey, (中文(简体) ), defaultColorScheme, (中文(简体) ), forceColorScheme 财务报告和审定财务报表 attribute 与 相同数值 到插件和每个 useTheme() 电话 如果两者不同,剧本和钩子先有分歧 装入.

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

共享 const THEME_OPTIONS = { … } 两个文件导入最简单 如何保持他们的一致.

内容安全政策

如果您的 CSP 屏蔽了内置脚本, 请通过您的服务器在 script-src 指令 :

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

值进入已建 index.html 逐字记录。 使用任意占位符 您的服务器或边缘功能将替换每个请求.

注射什么药

一个标签,第一个在 <head>数字 :

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

剧本运行一次,没有收听者。 选择操作系统 装入后继续运行 useTheme()这是工作.

不使用维特?

这个 反射读取 涵盖替代品:预建 theme-init.js 复制到 public/, (中文(简体) ), 或粘贴输出 buildThemeInitScript() 手边。 Next.js 应用程序使用 <ThemeScript /> 换句话说.

版本与发布记录

@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 是什么?

将@42/ui-react的反FOUC主题 init脚本注入索引.html的Vite插件

如何安装 @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 和提交记录判断。