惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
L
LINUX DO - 最新话题
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
S
Schneier on Security
B
Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
T
Tenable Blog
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
D
DataBreaches.Net
S
Secure Thoughts
Security Latest
Security Latest
H
Heimdal Security Blog
The Hacker News
The Hacker News
O
OpenAI News
AWS News Blog
AWS News Blog
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
腾讯CDC
U
Unit 42
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hugging Face - Blog
Hugging Face - Blog
The Last Watchdog
The Last Watchdog
Recorded Future
Recorded Future
V2EX - 技术
V2EX - 技术
爱范儿
爱范儿
F
Full Disclosure

博客园 - 丁少华

React UI 库推荐 Loop Engineering Vue 3 UI 组件库 Neon白嫖免费pgsql 域名利用cloudflare免费图床 nginx反代CloudflarePages提示502 vite使用shadcn vite使用biome Window下Nginx winserver2022安装不上软件 mac已损坏无法打开 命令运行器之task 命令运行器之just windows开启wsl 白嫖Redis 白嫖MongoDB vercel无服务函数 无服务器函数完全指南 在线 mock 方案 解释型语言和编译型语言 何时该使用monorepo monorepo前端专属吗 Cursor锁区问题 CentOS9上Let’s Encrypt自动续签 Windows给文件夹别名 ai 常识 nestjs逆向工程Prisma与DTO nestjs的orm之Prisma
代码高亮
丁少华 · 2026-03-13 · via 博客园 - 丁少华

直接结论:Vite/Rsbuild 环境下,优先 Shiki(构建时),其次 Prism(客户端),最后考虑 highlight.js。

详细对比:

维度 Shiki Prism.js highlight.js
主题美观度 ⭐⭐⭐⭐⭐ VS Code 同款 ⭐⭐⭐ 基础主题 ⭐⭐ 较朴素
语法准确性 ⭐⭐⭐⭐⭐ TextMate 引擎 ⭐⭐⭐ 正则匹配 ⭐⭐ 易误判
客户端体积 ⭐ 大(200KB+) ⭐⭐⭐ 中(~15KB+语言) ⭐⭐⭐⭐⭐ 小(~6KB+语言)
构建时支持 ⭐⭐⭐⭐⭐ 完美 ⭐⭐ 需插件 ⭐⭐ 需插件
React 生态 ⭐⭐⭐⭐ MDX 首选 ⭐⭐⭐⭐ 组件丰富 ⭐⭐⭐ 需封装

针对 Vite/Rsbuild 的具体建议

场景 1:代码写在文件里(文档/博客)→ Shiki 构建时

unplugin-shikirehype-pretty-code,构建阶段直接生成高亮 HTML:

// vite.config.ts
import Shiki from 'unplugin-shiki/vite'

export default {
  plugins: [
    Shiki({ theme: 'dracula' }) // 构建时转换,0 运行时开销
  ]
}

优势:浏览器不收高亮库代码,秒开无闪烁,SEO 完美。

场景 2:代码动态从 API 获取 → Prism 轻量版Shiki 客户端

  • 追求极致加载速度:Prism(只注册需要的语言,体积可控制在 20KB 内)
  • 追求 VS Code 效果:Shiki 客户端模式,但要接受额外 300KB 左右 JS

场景 3:极简单展示(如后台管理系统)→ highlight.js

如果只是一个简单的 <pre> 代码展示,不需要行号、复制按钮等复杂功能:

import hljs from 'highlight.js/lib/core'
import typescript from 'highlight.js/lib/languages/typescript'

hljs.registerLanguage('typescript', typescript)
// 体积最小,够用就行

避坑指南

别在 Vite 里客户端跑完整版 Shiki(除非必须):

  • 完整 Shiki 客户端包 ≈ 500KB+(含所有语言和主题)
  • 即使动态导入单个主题也要 ~200KB
  • 首次高亮会有明显延迟(Wasm 初始化)

Prism 在 Rsbuild 里的坑
Rsbuild 默认开启代码压缩,Prism 的按需加载语法(import 'prismjs/components/...')有时会 tree-shaking 出错,建议用 babel-plugin-prismjs 显式配置语言。

一句话决策

  • VS Code 同款效果 且能 构建时处理Shiki(无敌)
  • 客户端动态高亮 且控制 包体积Prism(平衡)
  • 只要 能用就行,越小越好highlight.js(极简)