-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
196 lines (189 loc) · 6.71 KB
/
Copy pathvite.config.ts
File metadata and controls
196 lines (189 loc) · 6.71 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
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
import { readFileSync } from 'node:fs'
import { createRequire } from 'node:module'
import { resolve } from 'node:path'
import { defineConfig, type Plugin } from 'vite'
import react from '@vitejs/plugin-react'
// The ZenNotes monorepo is consumed read-only, straight from source, the same
// way apps/web does it (aliases into packages/*). Nothing in that repo is
// modified by this project.
const ROOT = import.meta.dirname
const ZENNOTES = resolve(ROOT, '.zennotes-source')
// app-core's custom-code-language engine imports the oniguruma wasm as
// `?url`. Inline it as a data URL (same plugin as apps/web) so the lazily
// loaded engine chunk is self-contained — no wasm asset to serve under the
// capacitor:// scheme. The engine never loads on mobile today
// (supportsCustomCodeLanguages is false), but the import must still resolve
// at build time.
function onigurumaDataUrl(): Plugin {
const virtualId = '\0zennotes:oniguruma-wasm-data-url'
const wasmPath = createRequire(resolve(ROOT, 'package.json')).resolve(
'vscode-oniguruma/release/onig.wasm'
)
return {
name: 'zennotes-oniguruma-data-url',
enforce: 'pre',
resolveId(id) {
if (id === 'vscode-oniguruma/release/onig.wasm?url') return virtualId
return null
},
load(id) {
if (id !== virtualId) return null
const bytes = readFileSync(wasmPath)
const url = `data:application/wasm;base64,${bytes.toString('base64')}`
return `export default ${JSON.stringify(url)}`
}
}
}
// app-core imports Harper (the desktop and web grammar checker) and its wasm
// binary. Phones rely on the system keyboard for spelling, the mobile bridge
// never reports `supportsHarper`, and app-core never loads Harper here, so both
// imports resolve to an empty module: no 16 MB binary, no harper.js dependency.
function harperStub(): Plugin {
const virtualId = '\0zennotes:harper-stub'
return {
name: 'zennotes-harper-stub',
enforce: 'pre',
resolveId(id) {
if (id === 'harper.js' || id === 'harper.js/dist/harper_wasm_slim_bg.wasm?url') return virtualId
return null
},
load(id) {
if (id !== virtualId) return null
return 'export default ""\n'
}
}
}
function rendererManualChunk(id: string): string | undefined {
const normalizedId = id.split('\\').join('/')
if (normalizedId.endsWith('/packages/app-core/src/lib/wikilinks.ts')) {
return 'app-wikilinks'
}
if (normalizedId.endsWith('/packages/app-core/src/lib/local-assets.ts')) {
return 'app-local-assets'
}
if (normalizedId.endsWith('/packages/app-core/src/store.ts')) {
return 'app-store'
}
if (!id.includes('node_modules')) return undefined
// `@xyflow/react` (and the zustand nested under it) must NOT ride this
// rule: `/react/` matches it as a substring, and vendor-react is on the
// boot path, so React Flow would be fetched and evaluated on every launch
// for the Workflows canvas — a feature mobile hides entirely. Left alone,
// it lands in the lazily-imported WorkflowsView chunk, which never loads
// here.
if (
(id.includes('/react/') || id.includes('/react-dom/') || id.includes('/zustand/')) &&
!id.includes('/@xyflow/')
) {
return 'vendor-react'
}
if (id.includes('/@codemirror/language-data/')) {
return 'vendor-editor-languages'
}
if (
id.includes('/@codemirror/') ||
id.includes('/codemirror/') ||
id.includes('/@lezer/') ||
id.includes('/@replit/codemirror-vim/')
) {
return 'vendor-editor'
}
if (
id.includes('/remark-') ||
id.includes('/rehype-') ||
id.includes('/unified/') ||
id.includes('/unist-util-visit/') ||
id.includes('/gray-matter/') ||
id.includes('/katex/')
) {
return 'vendor-markdown'
}
if (id.includes('/highlight.js/')) {
return 'vendor-highlight'
}
if (id.includes('/vscode-textmate/') || id.includes('/vscode-oniguruma/')) {
return 'vendor-textmate'
}
// No manualChunks rule for mermaid / cytoscape / dagre on purpose (upstream
// PR #507 finding). Mermaid is only ever reached through a dynamic import,
// but forcing its modules into a named chunk makes Rollup hoist that chunk
// into the entry's STATIC graph — every launch then fetched and evaluated
// 2.5MB of diagram code (plus vendor-markdown, which it imports) before the
// user opened a note. Narrowing the rule does not help; the grouping itself
// breaks the async boundary. Left to Rollup, mermaid lands in its own async
// chunks fetched the first time a diagram actually renders. Total bundle
// size is unchanged; only the boot path is — which is exactly what the
// mobile cold-start budget cares about.
if (id.includes('/jsxgraph/')) {
return 'vendor-jsxgraph'
}
if (id.includes('/function-plot/')) {
return 'vendor-function-plot'
}
if (id.includes('/d3')) {
return 'vendor-d3'
}
return undefined
}
export default defineConfig({
root: ROOT,
base: './',
resolve: {
alias: [
{ find: '@renderer', replacement: resolve(ZENNOTES, 'packages/app-core/src') },
{ find: '@shared', replacement: resolve(ZENNOTES, 'packages/shared-domain/src') },
{ find: '@bridge-contract', replacement: resolve(ZENNOTES, 'packages/bridge-contract/src') },
{
find: /^@zennotes\/app-core\/(.*)$/,
replacement: resolve(ZENNOTES, 'packages/app-core/src') + '/$1'
},
{
find: /^@zennotes\/shared-domain\/(.*)$/,
replacement: resolve(ZENNOTES, 'packages/shared-domain/src') + '/$1'
},
{
find: /^@zennotes\/bridge-contract\/(.*)$/,
replacement: resolve(ZENNOTES, 'packages/bridge-contract/src') + '/$1'
},
// Two dependency-free desktop-main data/logic modules reused verbatim
// (demo tour content, wikilink rename rewriting). Read-only imports.
{ find: '@desktop-main', replacement: resolve(ZENNOTES, 'apps/desktop/src/main') }
],
// The app-core sources live in another repo whose node_modules would
// otherwise win nearest-wins resolution; dedupe pins every stateful
// singleton (React, Zustand, CodeMirror) to this project's copy.
dedupe: [
'react',
'react-dom',
'zustand',
'@codemirror/state',
'@codemirror/view',
'@codemirror/language',
'@codemirror/autocomplete',
'@codemirror/commands',
'@codemirror/search',
'@lezer/common',
'@lezer/highlight',
'@replit/codemirror-vim',
'codemirror'
]
},
server: {
port: 5183,
fs: {
allow: [ROOT, ZENNOTES]
}
},
plugins: [onigurumaDataUrl(), harperStub(), react()],
build: {
outDir: 'dist',
emptyOutDir: true,
chunkSizeWarningLimit: 3500,
sourcemap: false,
rollupOptions: {
output: {
manualChunks: rendererManualChunk
}
}
}
})