Repository navigation
Expand file tree
/
Copy pathelectron.vite.config.ts
More file actions
152 lines (142 loc) · 4.51 KB
/
Copy pathelectron.vite.config.ts
File metadata and controls
152 lines (142 loc) · 4.51 KB
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
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { defineConfig } from 'electron-vite'
import type { Plugin } from 'vite'
import react from '@vitejs/plugin-react'
function keepKatexWoff2(css: string): string {
return css.replace(
/url\(fonts\/([\w-]+\.woff2)\) format\("woff2"\),\s*url\(fonts\/[\w-]+\.woff\) format\("woff"\),\s*url\(fonts\/[\w-]+\.ttf\) format\("truetype"\)/g,
'url(fonts/$1) format("woff2")'
)
}
/** Chromium supports WOFF2, so avoid emitting KaTeX's legacy font fallbacks. */
function katexWoff2Only(): Plugin {
const katexCss = resolve(
__dirname,
'node_modules/katex/dist/katex.min.css'
).replace(/\\/g, '/')
return {
name: 'katex-woff2-only',
enforce: 'pre',
transform(code, id) {
const normalizedId = id.split('?')[0].replace(/\\/g, '/')
return normalizedId === katexCss
? { code: keepKatexWoff2(code), map: null }
: null
}
}
}
/** Chromium uses WOFF2, including the locally packaged Inter display font. */
function interWoff2Only(): Plugin {
const interCss = resolve(
__dirname,
'node_modules/@fontsource/inter/latin-400.css'
).replace(/\\/g, '/')
return {
name: 'inter-woff2-only',
enforce: 'pre',
transform(code, id) {
const normalizedId = id.split('?')[0].replace(/\\/g, '/')
if (normalizedId !== interCss) return null
return {
code: code.replace(
/url\(([^)]+\.woff2)\) format\('woff2'\),\s*url\([^)]+\.woff\) format\('woff'\)/g,
'url($1) format("woff2")'
),
map: null
}
}
}
}
/** markdown-it-texmath always receives our KaTeX engine, so its CommonJS fallback is dead code. */
function texmathWithoutFallbackKatex(): Plugin {
return {
name: 'texmath-without-fallback-katex',
enforce: 'pre',
transform(code, id) {
const normalizedId = id.split('?')[0].replace(/\\/g, '/')
if (!normalizedId.endsWith('/node_modules/markdown-it-texmath/texmath.js')) return null
return {
code: code.replace(
/else if \(typeof module === "object"\)\s*texmath\.katex = require\('katex'\);\s*else/,
'else'
),
map: null
}
}
}
}
/**
* Exposes `virtual:katex-fonts-css`: KaTeX's stylesheet with every woff2 font
* inlined as a base64 data URI. HTML/PDF/PNG export writes a standalone document to a
* temporary file and loads that, so it carries no reference back to the app's own asset
* directory — KaTeX's relative `url(fonts/…)` references would have nothing to resolve
* against there, so the fonts must travel inside the CSS itself for exported math to
* render faithfully. The woff/ttf fallbacks are dropped (Chromium always picks woff2).
*/
function katexEmbeddedFonts(): Plugin {
const virtualId = 'virtual:katex-fonts-css'
const resolvedId = '\0' + virtualId
const katexDist = resolve(__dirname, 'node_modules/katex/dist')
return {
name: 'katex-embedded-fonts',
resolveId(id) {
return id === virtualId ? resolvedId : null
},
load(id) {
if (id !== resolvedId) return null
let css = keepKatexWoff2(readFileSync(resolve(katexDist, 'katex.min.css'), 'utf8'))
css = css.replace(
/url\(fonts\/([\w-]+\.woff2)\)/g,
(_match, file: string) =>
`url(data:font/woff2;base64,${readFileSync(resolve(katexDist, 'fonts', file)).toString('base64')})`
)
return `export default ${JSON.stringify(css)}`
}
}
}
export default defineConfig({
main: {
build: {
outDir: 'out/main',
rollupOptions: {
// The PNG export runs its pixel loop on a worker thread, which needs its own
// entry so `new Worker(...)` has a file to load next to `main.js`.
input: {
main: resolve(__dirname, 'electron/main.ts'),
pngWorker: resolve(__dirname, 'electron/pngWorker.ts')
},
output: { entryFileNames: '[name].js' }
}
}
},
preload: {
build: {
outDir: 'out/preload',
lib: {
entry: resolve(__dirname, 'electron/preload.ts')
},
rollupOptions: {
output: { entryFileNames: 'preload.js' }
}
}
},
renderer: {
root: 'src',
worker: {
format: 'es'
},
build: {
outDir: 'out/renderer',
rollupOptions: {
input: resolve(__dirname, 'src/index.html')
}
},
resolve: {
alias: {
'@': resolve(__dirname, 'src')
}
},
plugins: [react(), texmathWithoutFallbackKatex(), katexWoff2Only(), interWoff2Only(), katexEmbeddedFonts()]
}
})