forked from wowsims/sod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.mts
138 lines (127 loc) · 3.8 KB
/
vite.config.mts
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
/** @type {import('vite').UserConfig} */
import fs from 'fs';
import glob from 'glob';
import { IncomingMessage, ServerResponse } from 'http';
import path from 'path';
import { ConfigEnv, defineConfig, PluginOption, UserConfigExport } from 'vite';
import { checker } from 'vite-plugin-checker';
export const BASE_PATH = path.resolve(__dirname, 'ui');
export const OUT_DIR = path.join(__dirname, 'dist', 'sod');
function serveExternalAssets() {
const workerMappings = {
'/sod/sim_worker.js': '/sod/local_worker.js',
'/sod/net_worker.js': '/sod/net_worker.js',
'/sod/lib.wasm': '/sod/lib.wasm',
};
return {
name: 'serve-external-assets',
configureServer(server) {
server.middlewares.use((req, res, next) => {
const url = req.url!;
if (Object.keys(workerMappings).includes(url)) {
const targetPath = workerMappings[url as keyof typeof workerMappings];
const assetsPath = path.resolve(__dirname, './dist/sod');
const requestedPath = path.join(assetsPath, targetPath.replace('/sod/', ''));
serveFile(res, requestedPath);
return;
}
if (url.includes('/sod/assets')) {
const assetsPath = path.resolve(__dirname, './assets');
const assetRelativePath = url.split('/sod/assets')[1];
const requestedPath = path.join(assetsPath, assetRelativePath);
serveFile(res, requestedPath);
return;
} else {
next();
}
});
},
} satisfies PluginOption;
}
function serveFile(res: ServerResponse<IncomingMessage>, filePath: string) {
if (fs.existsSync(filePath)) {
const contentType = determineContentType(filePath);
res.writeHead(200, { 'Content-Type': contentType });
fs.createReadStream(filePath).pipe(res);
} else {
console.log('Not found on filesystem: ', filePath);
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
}
}
function determineContentType(filePath: string) {
const extension = path.extname(filePath).toLowerCase();
switch (extension) {
case '.jpg':
case '.jpeg':
return 'image/jpeg';
case '.png':
return 'image/png';
case '.gif':
return 'image/gif';
case '.css':
return 'text/css';
case '.js':
return 'text/javascript';
case '.woff':
case '.woff2':
return 'font/woff2';
case '.json':
return 'application/json';
case '.wasm':
return 'application/wasm'; // Adding MIME type for WebAssembly files
// Add more cases as needed
default:
return 'application/octet-stream';
}
}
export const getBaseConfig = ({ command, mode }: ConfigEnv) =>
({
base: '/sod/',
root: path.join(__dirname, 'ui'),
build: {
outDir: OUT_DIR,
minify: mode === 'development' ? false : 'terser',
sourcemap: command === 'serve' ? 'inline' : false,
target: ['es2020'],
},
} satisfies Partial<UserConfigExport>);
export default defineConfig(({ command, mode }) => {
const baseConfig = getBaseConfig({ command, mode });
return {
...baseConfig,
plugins: [
serveExternalAssets(),
checker({
root: path.resolve(__dirname, 'ui'),
typescript: true,
enableBuild: true,
}),
],
esbuild: {
jsxInject: "import { element, fragment } from 'tsx-vanilla';",
},
build: {
...baseConfig.build,
rollupOptions: {
input: {
...glob.sync(path.resolve(BASE_PATH, '**/index.html').replace(/\\/g, '/')).reduce<Record<string, string>>((acc, cur) => {
const name = path.relative(__dirname, cur);
acc[name] = cur;
return acc;
}, {}),
// Add shared.scss as a separate entry if needed or handle it separately
},
output: {
assetFileNames: () => 'bundle/[name]-[hash].style.css',
entryFileNames: () => 'bundle/[name]-[hash].entry.js',
chunkFileNames: () => 'bundle/[name]-[hash].chunk.js',
},
},
server: {
origin: 'http://localhost:3000',
// Adding custom middleware to serve 'dist' directory in development
},
},
};
});