docs/.vitepress/configs/index.ts
185 lines · 5455 bytes
7 symbols in this file
1/**
2 * All Rights Reserved
3 *
4 * Copyright (c) 2025 taskylizard
5 *
6 * All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
7 */
8import {
9 PageProperties,
10 PagePropertiesMarkdownSection
11} from '@nolebase/vitepress-plugin-page-properties/vite'
12import { fileURLToPath, URL } from 'node:url'
13import UnoCSS from 'unocss/vite'
14import { createLogger } from 'vite'
15import Devtools from 'vite-plugin-vue-devtools'
16import type { DefaultTheme, Plugin, UserConfig } from 'vitepress'
17import { hostname, sidebar, siteConfig } from './constants'
18import { generateImages, generateMeta } from './hooks'
19import { configureMarkdown } from './markdown'
20import { aliases, defs, movePlugin } from './markdown/emoji'
21import { collectPageLinks, urlSearchDevPlugin, writeUrlSearchIndex } from '../plugins/urlSearchPlugin'
22
23const logger = createLogger()
24const warn = logger.warn
25const warnOnce = logger.warnOnce
26
27function shouldSuppressWarning(message: string) {
28 return message.includes('[lightningcss minify] Unknown at rule: @property') ||
29 message.includes('Some chunks are larger than')
30}
31
32logger.warn = (message, options) => {
33 if (shouldSuppressWarning(message)) return
34 warn(message, options)
35}
36
37logger.warnOnce = (message, options) => {
38 if (shouldSuppressWarning(message)) return
39 warnOnce(message, options)
40}
41
42const GIT_COMMIT = process.env.NODE_ENV === 'development'
43 ? 'dev'
44 : (process.env.GITHUB_SHA ??
45 (await (await import('tinyexec'))
46 .x('git', ['rev-parse', 'HEAD'])
47 .then((result) => result.stdout.trim())) ??
48 'dev')
49
50const GIT_COMMIT_TIMESTAMP = process.env.NODE_ENV === 'development'
51 ? String(Math.floor(Date.now() / 1000))
52 : ((await (await import('tinyexec'))
53 .x('git', ['log', '-1', '--format=%ct', 'HEAD'])
54 .then((result) => result.stdout.trim())) ?? String(Math.floor(Date.now() / 1000)))
55
56export const shared: UserConfig<DefaultTheme.Config> = {
57 ...siteConfig,
58 transformHead: async (context) => generateMeta(context, hostname),
59 buildEnd: async (context) => {
60 writeUrlSearchIndex(context.outDir)
61 if (process.env.CI) {
62 await generateImages(context)
63 }
64 },
65 markdown: {
66 emoji: { defs, shortcuts: aliases },
67 config(md) {
68 configureMarkdown(md)
69 }
70 },
71 themeConfig: {
72 search: {
73 options: {
74 miniSearch: {
75 searchOptions: {
76 combineWith: 'AND',
77 fuzzy: false,
78 // @ts-ignore
79 boostDocument: (
80 _,
81 term,
82 storedFields: Record<string, string | string[]>
83 ) => {
84 const titles = (storedFields?.titles as string[])
85 .filter((t) => Boolean(t))
86 .map((t) => t.toLowerCase())
87 const titleIndex = titles
88 .map((t, i) => (t?.includes(term) ? i : -1))
89 .find((i) => i >= 0) ?? -1
90 if (titleIndex >= 0) return 10000 - titleIndex
91 return 1
92 }
93 }
94 },
95 _render(src, env, md) {
96 collectPageLinks(src, env.relativePath)
97 return md.render(src, env)
98 },
99 detailedView: true
100 },
101 provider: 'local'
102 },
103 logo: { src: '/asset/fav.svg' },
104 sidebar,
105 socialLinks: [
106 { icon: 'github', link: 'https://github.com/wotakumoe/Wotaku' },
107 { icon: 'discord', link: 'https://discord.gg/vShRGx8ZBC' }
108 ]
109 },
110 vite: {
111 customLogger: logger,
112 define: {
113 __GIT_COMMIT__: JSON.stringify(GIT_COMMIT),
114 __GIT_COMMIT_TS__: parseInt(GIT_COMMIT_TIMESTAMP, 10)
115 },
116 experimental: {
117 enableNativePlugin: true
118 },
119 build: {
120 cssMinify: 'lightningcss'
121 },
122 optimizeDeps: {
123 exclude: [
124 '@nolebase/vitepress-plugin-enhanced-readabilities/client',
125 '@nolebase/vitepress-plugin-page-properties/client'
126 ]
127 },
128 ssr: {
129 noExternal: [
130 '@nolebase/vitepress-plugin-enhanced-readabilities',
131 '@nolebase/vitepress-plugin-page-properties',
132 '@nolebase/ui',
133 '@fmhy/components'
134 ]
135 },
136 plugins: [
137 urlSearchDevPlugin(),
138 Devtools(),
139 PageProperties(),
140 PagePropertiesMarkdownSection(),
141 UnoCSS({
142 configFile: '../unocss.config.ts'
143 }),
144 {
145 name: 'custom:adjust-order',
146 configResolved(c) {
147 movePlugin(
148 c.plugins as unknown as Plugin[],
149 'vitepress',
150 'before',
151 'unocss:transformers:pre'
152 )
153 movePlugin(
154 c.plugins as unknown as Plugin[],
155 'custom:tooltip-loader',
156 'before',
157 'vitepress'
158 )
159 }
160 }
161 ],
162 resolve: {
163 alias: [
164 {
165 find: /^.*\/VPBadge\.vue$/,
166 replacement: fileURLToPath(
167 new URL('../theme/components/Badge.vue', import.meta.url)
168 )
169 },
170 {
171 find: /^.*\/VPNavBarSearch\.vue$/,
172 replacement: fileURLToPath(
173 new URL('../theme/components/NavBarSearch.vue', import.meta.url)
174 )
175 },
176 {
177 find: /^\.\.\/composables\/sidebar$/,
178 replacement: fileURLToPath(
179 new URL('../theme/composables/sidebar.ts', import.meta.url)
180 )
181 }
182 ]
183 }
184 }
185}