-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvite.config.ts
More file actions
76 lines (62 loc) · 1.95 KB
/
vite.config.ts
File metadata and controls
76 lines (62 loc) · 1.95 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
import { defineConfig, type ResolvedConfig } from 'vite'
import preact from '@preact/preset-vite'
import { readFileSync, writeFileSync, mkdirSync } from 'fs'
import { join } from 'path'
const packageJson = JSON.parse(readFileSync('./package.json', 'utf-8'))
// Calculate base path once and reuse
const getBasePath = () => {
if (process.env.NODE_ENV === 'development') {
return '/'
}
return new URL(packageJson.homepage).pathname
}
const basePath = getBasePath()
// Custom plugin to transform spaRedirect.html
function spaRedirectPlugin() {
let config: ResolvedConfig
return {
name: 'spa-redirect-transform',
configResolved(resolvedConfig: ResolvedConfig) {
config = resolvedConfig
},
writeBundle() {
// Read the source file
const sourceFile = readFileSync('./src/spaRedirect.html', 'utf-8')
const homepage = basePath.replace(/\/$/, '')
// Replace the placeholder
const transformedContent = sourceFile.replace('%HOMEPAGE%', homepage)
// Write to build directory
const buildDir = config.build.outDir
writeFileSync(join(buildDir, 'spaRedirect.html'), transformedContent)
// Also copy to subdirectories as per original build script
const subdirs = ['login', 'callback', 'view']
subdirs.forEach(subdir => {
const subdirPath = join(buildDir, subdir)
// Create directory if it doesn't exist
try {
mkdirSync(subdirPath, { recursive: true })
writeFileSync(join(subdirPath, 'index.html'), transformedContent)
} catch (err) {
console.warn(`Failed to create ${subdirPath}:`, err)
}
})
}
}
}
export default defineConfig({
plugins: [preact(), spaRedirectPlugin()],
base: basePath,
build: {
outDir: 'build',
sourcemap: true,
},
resolve: {
alias: {
'react': 'preact/compat',
'react-dom': 'preact/compat'
}
},
optimizeDeps: {
include: ['preact', 'wouter-preact']
}
})