-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvite.config.ts
More file actions
56 lines (46 loc) · 1.69 KB
/
vite.config.ts
File metadata and controls
56 lines (46 loc) · 1.69 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
import { defineConfig, type Plugin } from "vite";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const WEB_IFC_PUBLIC_PATH = "/wasm/";
const WEB_IFC_FILES = ["web-ifc.wasm", "web-ifc-mt.wasm", "web-ifc-mt.worker.js"] as const;
function webIfcAssets(): Plugin {
const rootDir = path.dirname(fileURLToPath(import.meta.url));
const webIfcDir = path.join(rootDir, "node_modules", "web-ifc");
const allowed = new Set<string>(WEB_IFC_FILES);
const resolveFile = (fileName: string) => path.join(webIfcDir, fileName);
return {
name: "web-ifc-assets",
configureServer(server) {
server.middlewares.use((req, res, next) => {
const url = req.url ?? "";
if (!url.startsWith(WEB_IFC_PUBLIC_PATH)) return next();
const fileName = url.slice(WEB_IFC_PUBLIC_PATH.length).split("?")[0]?.split("#")[0] ?? "";
if (!allowed.has(fileName)) return next();
const filePath = resolveFile(fileName);
if (!fs.existsSync(filePath)) {
res.statusCode = 404;
res.end();
return;
}
if (fileName.endsWith(".wasm")) res.setHeader("Content-Type", "application/wasm");
else res.setHeader("Content-Type", "text/javascript");
fs.createReadStream(filePath).pipe(res);
});
},
generateBundle() {
for (const fileName of WEB_IFC_FILES) {
const filePath = resolveFile(fileName);
if (!fs.existsSync(filePath)) continue;
this.emitFile({
type: "asset",
fileName: `wasm/${fileName}`,
source: fs.readFileSync(filePath),
});
}
},
};
}
export default defineConfig({
plugins: [webIfcAssets()],
});