-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.ts
More file actions
209 lines (184 loc) · 6.67 KB
/
Copy pathvite.config.ts
File metadata and controls
209 lines (184 loc) · 6.67 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
197
198
199
200
201
202
203
204
205
206
207
208
209
import { defineConfig } from 'vite'
import checker from 'vite-plugin-checker'
import { copyFileSync, readFileSync, writeFileSync, rmSync, existsSync, rmdirSync } from 'node:fs'
import { resolve } from 'node:path'
import { execFileSync } from 'node:child_process'
function escapeRegExp(s: string): string {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
function localReleaseBundle(): import('vite').Plugin {
let base = '/app/'
let outDir = 'dist'
const capturedAssets = new Map<string, { content: string; type: 'js' | 'css' }>()
return {
name: 'local-release-bundle',
configResolved(config) {
base = config.base
outDir = config.build.outDir ?? 'dist'
},
// Step 1 – capture JS/CSS content WITHOUT deleting from the bundle.
// Deleting here would prevent Vite's HTML plugin from emitting the
// <script src="..."> / <link href="..."> tags — we need those tags to
// exist in the written HTML so step 2 can find and replace them.
generateBundle(_options, bundle) {
if (base !== './' && base !== '') return
for (const [filename, chunk] of Object.entries(bundle)) {
if (chunk.type === 'chunk' && filename.endsWith('.js')) {
capturedAssets.set(filename, { content: chunk.code, type: 'js' })
} else if (
chunk.type === 'asset' &&
filename.endsWith('.css') &&
typeof chunk.source === 'string'
) {
capturedAssets.set(filename, { content: chunk.source, type: 'css' })
}
}
},
// Step 2 – after all files are on disk, read index.html, inline every
// captured asset, write back, then delete the now-redundant asset files.
writeBundle() {
if (base !== './' && base !== '') return
if (capturedAssets.size === 0) return
const distDir = resolve(__dirname, outDir)
const htmlPath = resolve(distDir, 'index.html')
let html = readFileSync(htmlPath, 'utf-8')
for (const [filename, { content, type }] of capturedAssets) {
const fullPath = `${base}${filename}` // e.g. ./assets/index-X.js
if (type === 'js') {
const safeContent = content.replace(/<\/script/gi, '<\\/script')
// Regex tolerates any attribute order around src=
const re = new RegExp(
`<script[^>]*\\ssrc="${escapeRegExp(fullPath)}"[^>]*>\\s*</script>`,
'i'
)
const srcAttr = new RegExp(`\\s+src="${escapeRegExp(fullPath)}"`, 'i')
html = html.replace(re, (tag) => {
const openTag = tag
.replace(srcAttr, '')
.replace(/>\s*<\/script>$/i, '>')
return `${openTag}${safeContent}</script>`
})
} else {
const safeContent = content.replace(/<\/style/gi, '<\\/style')
const re = new RegExp(
`<link[^>]*\\shref="${escapeRegExp(fullPath)}"[^>]*>`,
'i'
)
html = html.replace(re, () => `<style>${safeContent}</style>`)
}
// Remove the now-inlined asset file from disk
const assetPath = resolve(distDir, filename)
if (existsSync(assetPath)) rmSync(assetPath)
const mapPath = `${assetPath}.map`
if (existsSync(mapPath)) rmSync(mapPath)
}
// Remove the now-empty assets/ dir if it exists
const assetsDir = resolve(distDir, 'assets')
try { rmdirSync(assetsDir) } catch { /* not empty or doesn't exist — leave it */ }
writeFileSync(htmlPath, html)
},
// Step 3 – copy the (now fully self-contained) index.html to 404.html.
closeBundle() {
const dist = resolve(__dirname, outDir)
copyFileSync(`${dist}/index.html`, `${dist}/404.html`)
},
}
}
// Content-Security-Policy, injected at build time only:
// - dev stays CSP-free (Vite HMR needs inline preamble + ws: connections);
// - the single-file release build (base './') inlines all JS/CSS into the
// HTML via localReleaseBundle, which script-src 'self' would block, so it
// is skipped there too.
// style-src needs 'unsafe-inline' (ECharts/AG Grid inline style attributes)
// and fonts.googleapis.com (Inter is @import-ed from the bundled CSS).
// frame-ancestors is omitted — it has no effect in a <meta> policy.
const CSP_POLICY = [
"default-src 'self'",
"script-src 'self'",
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",
"font-src 'self' data: https://fonts.gstatic.com", // data: — AG Grid ships its icon font as a data: URI
"img-src 'self' data:",
"connect-src 'self' https://finnhub.io https://generativelanguage.googleapis.com",
"object-src 'none'",
"base-uri 'self'",
"form-action 'self'"
].join('; ')
function injectCsp(): import('vite').Plugin {
let base = '/app/'
return {
name: 'gl-csp',
apply: 'build',
configResolved(config) {
base = config.base
},
transformIndexHtml(html) {
if (base === './' || base === '') return html
return html.replace(
'<meta charset="UTF-8">',
`<meta charset="UTF-8">\n <meta http-equiv="Content-Security-Policy" content="${CSP_POLICY}">`
)
},
}
}
function normalizeReleaseVersion(version: string | undefined): string | null {
if (!version) return null
const normalized = version
.trim()
.replace(/^gammaledger-/, '')
.replace(/^app-/, '')
.replace(/^v/, '')
if (/^[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z][0-9A-Za-z.-]*)?$/.test(normalized)) {
return normalized
}
return null
}
function getLatestGitVersion(): string | null {
try {
const tags = execFileSync('git', ['tag', '--list', '--sort=-v:refname'], { encoding: 'utf-8' })
.split('\n')
.map(tag => normalizeReleaseVersion(tag))
.filter((tag): tag is string => tag !== null)
return tags[0] ?? null
} catch {
return null
}
}
function getAppVersion(): string {
const envVersion = normalizeReleaseVersion(process.env.VITE_APP_VERSION)
if (envVersion) return envVersion
const latestGitVersion = getLatestGitVersion()
if (latestGitVersion) return latestGitVersion
try {
return execFileSync('git', ['describe', '--tags', '--abbrev=0'], { encoding: 'utf-8' }).trim()
} catch {
return 'dev'
}
}
export default defineConfig({
base: '/app/',
root: '.',
plugins: [
checker({ typescript: true }),
injectCsp(),
localReleaseBundle()
],
define: {
__APP_VERSION__: JSON.stringify(getAppVersion()),
},
resolve: {
alias: {
'@core': '/src/core',
'@trades': '/src/trades',
'@calculations': '/src/calculations',
'@ui': '/src/ui',
'@utils': '/src/utils',
'@types-gl': '/src/types'
}
},
build: {
outDir: 'dist',
rollupOptions: {
input: 'index.html'
}
}
})