-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
221 lines (216 loc) · 6.67 KB
/
vite.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import {resolve} from 'node:path'
import * as fs from 'node:fs'
import * as process from 'node:process'
import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import pages from 'vite-plugin-pages'
import imports from 'unplugin-auto-import/vite'
import matter from 'gray-matter'
import markdown from 'unplugin-vue-markdown/vite'
import anchor from 'markdown-it-anchor'
import linkattr from 'markdown-it-link-attributes'
import toc from 'markdown-it-table-of-contents'
import unocss from 'unocss/vite'
import {presetAttributify, presetIcons, presetUno, presetWebFonts, transformerDirectives} from 'unocss'
import components from 'unplugin-vue-components/vite'
import katex from '@uniob/markdown-it-katex'
import sup from 'markdown-it-sup'
import sub from 'markdown-it-sub'
import mark from 'markdown-it-mark'
import Inspect from 'vite-plugin-inspect'
import generateSitemap from 'vite-plugin-pages-sitemap'
import type {RouteRecordNormalized, RouteRecordRaw} from 'vue-router'
import MarkdownItShiki from '@shikijs/markdown-it'
import {
transformerMetaHighlight,
transformerNotationDiff,
transformerNotationErrorLevel,
transformerNotationFocus,
transformerNotationHighlight,
} from '@shikijs/transformers'
import MarkdownItGitHubAlerts from 'markdown-it-github-alerts'
import {slugify} from './scripts/routing-support'
import {checkCustomComponent, katexOptions} from './scripts/tex-defs'
import {customFenceWrapper, retainMermaid} from './scripts/markdown-custom-fences'
import wrapMagnifier from './scripts/markdown-img-wrapper'
import ViteLoadString from './scripts/vite-load-string'
import {BrilTransformerFactory} from './scripts/BrilTransformer'
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: [
// rename imports
{find: '~/', replacement: `${resolve(__dirname, 'src')}/`},
],
},
optimizeDeps: {
include: ['vue', 'vue-router', '@vueuse/core', 'pinia'],
},
appType: 'spa',
plugins: [
// vue
vue({
include: [/\.vue$/, /\.md$/],
template: {
compilerOptions: {
isCustomElement: checkCustomComponent,
},
},
}),
// unocss styling
unocss({
presets: [
presetIcons(),
presetAttributify(),
presetUno(),
presetWebFonts({
fonts: {
sans: [{name: 'Inter', weights: [400, 500, 600], italic: true}],
mono: ['JetBrains Mono', 'monospace'],
},
}),
],
transformers: [transformerDirectives()],
}),
imports({
include: [
/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
/\.vue$/, /\.vue\?vue/, // .vue
/\.md$/, // .md
],
imports: [
'vue',
'vue-router',
'pinia',
'@vueuse/core',
'@vueuse/head',
],
}),
// handling markdown and vue pages routing
pages({
extensions: ['vue', 'md'],
dirs: ['pages', 'projects'],
exclude: ['**/components/**/*.vue'],
extendRoute(route) {
const path = resolve(__dirname, route.component.slice(1))
const md = fs.readFileSync(path, 'utf-8')
const data = matter(md, {excerpt_separator: '<!-- more -->'})
route.meta = Object.assign(route.meta || {}, {
frontmatter: {
...data.data,
excerpt: data.excerpt.length > 0 ? data.excerpt : undefined,
get description(): string | undefined {
return data.data.description || this.abstract || this.excerpt
},
},
})
return route
},
onRoutesGenerated(routes: RouteRecordRaw[]) {
generateSitemap({
hostname: 'https://universe.observer',
routes: routes.filter((r: RouteRecordNormalized) => {
if (r.meta?.frontmatter)
return r.meta.frontmatter.hidden !== true
return true
}),
})
return routes
},
}),
markdown({
wrapperComponent: (id) => {
if (id.includes('components'))
return undefined
return 'Post'
},
wrapperClasses: (id) => {
if (id.includes('components'))
return undefined
return 'post-md-content'
},
escapeCodeTagInterpolation: false,
markdownItOptions: {
linkify: true,
quotes: '""\'\'',
},
exposeFrontmatter: false,
exportFrontmatter: false,
frontmatter: true,
async markdownItSetup(md) {
const shikiPlugin = await (async () => {
const bril = JSON.parse(fs.readFileSync('./scripts/bril-textmate.json', 'utf8'))
const nord = JSON.parse(fs.readFileSync('./scripts/nord.json', 'utf8'))
const rosePineDawn = JSON.parse(fs.readFileSync('./scripts/rose-pine-dawn.json', 'utf8'))
return MarkdownItShiki({
themes: {
dark: nord,
light: rosePineDawn,
},
langs: ['typescript', 'c++', 'python', 'markdown', 'latex', 'swift', 'kotlin', bril],
defaultColor: false,
cssVariablePrefix: '--shiki-',
transformers: [
transformerMetaHighlight(),
transformerNotationDiff(),
transformerNotationErrorLevel(),
transformerNotationHighlight(),
transformerNotationFocus(),
...await BrilTransformerFactory(bril, nord, rosePineDawn),
],
})
})()
md.use(shikiPlugin)
md.use(anchor, {
slugify,
permalink: anchor.permalink.linkInsideHeader({
symbol: '🔗',
renderAttrs: () => ({'aria-hidden': 'true'}),
}),
})
md.use(linkattr, {
matcher: (link: string) => /^https?:\/\//.test(link),
attrs: {
target: '_blank',
rel: 'noopener',
},
})
md.use(toc, {
slugify,
includeLevel: [1, 2, 3, 4],
})
md.use(katex, katexOptions)
md.use(sup)
md.use(sub)
md.use(mark)
md.use(customFenceWrapper)
md.use(retainMermaid)
md.use(wrapMagnifier)
md.use(MarkdownItGitHubAlerts)
},
}),
components({
extensions: ['vue', 'md'],
dts: true,
include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
globs: ['**/components/**/*.vue'],
}),
ViteLoadString(),
process.env.NODE_ENV === 'production' ? null : Inspect(),
],
build: {
target: 'esnext',
minify: 'terser',
rollupOptions: {
output: {
entryFileNames: 'assets/[name].js',
chunkFileNames: 'assets/[name].js',
assetFileNames: 'assets/[name].[ext]',
},
},
},
ssgOptions: {
script: 'async',
formatting: 'minify',
},
})