fix(website-docs): prebundle fastdom when mermaid imports it (#4208)
mermaid >= 11.17 dynamically imports the UMD package fastdom. Vite's dependency scanner misses that import during vitepress dev, so the browser loads raw fastdom.js and throws "does not provide an export named 'default'". Pre-bundle fastdom only when this install can resolve it. The lockfile pins mermaid 11.16.0, which does not depend on fastdom, and an unconditional optimizeDeps.include makes Vite report "Failed to resolve dependency". Fixes #2899 Co-authored-by: Frank-zhu2000 <332350688+Frank-zhu2000@users.noreply.github.com>main
parent
7972cdd251
commit
621bc9b98a
|
|
@ -1,4 +1,5 @@
|
|||
import { readFileSync, readdirSync } from 'node:fs'
|
||||
import { createRequire } from 'node:module'
|
||||
import { resolve } from 'node:path'
|
||||
import { defineConfig, type DefaultTheme } from 'vitepress'
|
||||
import { withMermaid } from 'vitepress-plugin-mermaid'
|
||||
|
|
@ -6,6 +7,17 @@ import './theme-config.d.ts'
|
|||
import { repoVersionLabel } from './version'
|
||||
|
||||
const root = resolve(import.meta.dirname, '..')
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
/** 当前安装能解析到该模块时返回 true。缺失的包不能写进 optimizeDeps.include。 */
|
||||
function installedDep(id: string): boolean {
|
||||
try {
|
||||
require.resolve(id)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const sections: { dir: string; label: string; newestFirst?: boolean }[] = [
|
||||
{ dir: '01-getting-started', label: '快速开始' },
|
||||
|
|
@ -179,6 +191,16 @@ export default withMermaid(
|
|||
},
|
||||
},
|
||||
|
||||
// mermaid >= 11.17 会动态 import UMD 包 fastdom。Vite 启动时扫不到这次导入,
|
||||
// 浏览器直接加载原始 fastdom.js,报 does not provide an export named 'default'
|
||||
//(vitepress-plugin-mermaid#98)。只在能解析到这两个包时才预构建:lockfile 里的
|
||||
// mermaid 11.16.0 没有 fastdom,无条件 include 会报 Failed to resolve dependency。
|
||||
vite: {
|
||||
optimizeDeps: {
|
||||
include: ['fastdom', 'fastdom/extensions/fastdom-promised.js'].filter(installedDep),
|
||||
},
|
||||
},
|
||||
|
||||
mermaid: {
|
||||
theme: 'base',
|
||||
fontFamily:
|
||||
|
|
|
|||
Loading…
Reference in New Issue