forked from wowsims/cata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
108 lines (101 loc) · 3.16 KB
/
vite.config.js
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
import fs from 'fs';
import glob from "glob";
import path from "path";
import { defineConfig } from 'vite'
function serveExternalAssets() {
return {
name: 'serve-external-assets',
configureServer(server) {
server.middlewares.use((req, res, next) => {
const workerMappings = {
'/cata/sim_worker.js': '/cata/local_worker.js',
'/cata/net_worker.js': '/cata/net_worker.js',
'/cata/lib.wasm': '/cata/lib.wasm',
};
if (Object.keys(workerMappings).includes(req.url)) {
const targetPath = workerMappings[req.url];
const assetsPath = path.resolve(__dirname, './dist/cata');
const requestedPath = path.join(assetsPath, targetPath.replace('/cata/', ''));
serveFile(res, requestedPath);
return;
}
if (req.url.includes('/cata/assets')) {
const assetsPath = path.resolve(__dirname, './assets');
const assetRelativePath = req.url.split('/cata/assets')[1];
const requestedPath = path.join(assetsPath, assetRelativePath);
serveFile(res, requestedPath);
return;
} else {
next();
}
});
},
};
}
function serveFile(res, filePath) {
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) {
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 default defineConfig(({ command, mode }) => ({
plugins: [serveExternalAssets()],
base: "/cata/",
root: path.join(__dirname, "ui"),
build: {
outDir: path.join(__dirname, "dist", "cata"),
minify: mode === "development" ? false : "terser",
sourcemap: command === "serve" ? "inline" : "false",
target: ["es2020"],
rollupOptions: {
input: {
...glob.sync(path.resolve(__dirname, "ui", "**/index.html").replace(/\\/g, "/")).reduce((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
},
}
}));