-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
145 lines (125 loc) · 3.95 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
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import inlineWorker from './inlineWorker.js'
import json5 from 'json5'
import { join, normalize } from 'node:path'
import * as fs from 'node:fs'
function srcPath(...paths: string[]) {
return normalize(join(import.meta.dirname, 'src', ...paths))
}
const configPath = './config.json'
var userDefsS: string | undefined
try {
userDefsS = fs.readFileSync(configPath).toString('utf8')
}
catch(e) {
console.warn('Config is not defined. Defaults will be used')
userDefs = {}
}
var userDefs = {}
try {
if(userDefsS) userDefs = json5.parse(userDefsS)
}
catch(e) {
console.warn('Could not parse config. Defaults will be used')
console.warn(e)
}
var defaultDefs = {
__use_default_in_builds: true,
worker: true,
worker_objects: true,
worker_markers: true,
worker_colliders: true,
setup_markers: true,
setup_colliders: true,
setup_circular: true,
render_markers: true,
render_colliders: true,
render_circular: true,
backgrounds: true,
backgrounds_mipmap_levels: 10,
markers_mipmap_levels: 6,
}
var defines: any = {}
for(const k in defaultDefs) {
const v = userDefs[k] ?? defaultDefs[k]
defines[k] = v
}
function injectPreloads(command) {
if(command !== 'build') return
return {
transformIndexHtml: {
order: 'pre',
handler() {
return [
{
tag: 'link',
attrs: {
rel: 'preload',
href: './data-processed/backgrounds.pak',
as: 'fetch',
crossorigin: 'anonymous',
},
},
{
tag: 'link',
attrs: {
rel: 'preload',
href: './data-raw/markers/markers.png',
as: 'image',
},
},
]
},
}
}
}
export default defineConfig(({ command, mode, isSsrBuild, isPreview }) => {
const useDefault = defines.__use_default_in_builds
if(command === 'build' && useDefault) {
console.warn('Using default config for build')
defines = { ...defaultDefs }
}
const finalDefines = {}
delete defines.__use_default_in_builds
for(const k in defines) finalDefines['__' + k] = JSON.stringify(defines[k])
const gh = process?.env?.MY_GITHUB_PAGES
return {
root: './src',
base: gh != null ? gh : '/',
build: {
outDir: '../dist',
emptyOutDir: true,
rollupOptions: {
output: {
manualChunks: {
react: ['react', 'react-dom'],
},
}
}
},
define: finalDefines,
resolve: {
alias: {
'$/objects.bp' : srcPath('./data-raw/objects/objects.bp'),
'$/polygons.bp': srcPath('./data-processed/polygons.bp'),
'$/markers.png': srcPath('./data-raw/markers/markers.png'),
'$/backgrounds.pak': srcPath('./data-processed/backgrounds.pak'),
'$/backgrounds.json': srcPath('./data-processed/backgrounds.json'),
'$/markers-meta.json': srcPath('./data-processed/markers-meta.json'),
'$/markers.json': srcPath('./data-processed/markers.json'),
'$/meta.json': srcPath('./data-processed/meta.json'),
},
},
assetsInclude: ['**/*.bp', '**/*.pak'],
plugins: [
react(),
inlineWorker({
htmlPath: srcPath('index.html'),
workerPath: srcPath('worker.js'),
assignTo: 'window.worker',
}),
injectPreloads(command),
],
}
})