-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtsup.config.ts
94 lines (83 loc) · 2.75 KB
/
tsup.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
import { defineConfig } from "tsup";
import http from "http";
import fs from "fs";
import path from "path";
import { Server as SocketIO } from "socket.io";
import { globby } from "globby";
export default defineConfig(async (options) => {
const isDev = options.watch;
let io: SocketIO | null = null;
if (isDev) {
// Create socket server
const socketServer = http.createServer();
io = new SocketIO(socketServer, {
cors: {
origin: "*",
credentials: true,
methods: ["GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE", "HEAD"],
},
});
socketServer.listen(8001, "0.0.0.0");
}
const basePath = path.join(process.cwd(), "src");
return {
entry: await globby([`${basePath}/**/*.(t|j)s*`, `!${basePath}/**/*.d.ts`]),
platform: "browser",
format: ["esm", "cjs"],
dts: true,
minify: !isDev,
clean: true,
external: ["react", "framer", "framer-motion"],
async onSuccess() {
if (!isDev) return;
io?.emit("build");
// Create the HTTP server
const server = http.createServer((req, res) => {
// Construct the file path
let filePath = path.join(
__dirname,
"dist",
req.url === "/" || !req.url ? "index.html" : req.url
);
let extname = String(path.extname(filePath)).toLowerCase();
// need to remove query from extname
extname = extname.split("?")[0];
filePath = filePath.split("?")[0];
console.log("extname", extname);
console.log("filePath", filePath);
// Map file extensions to Content-Type
const mimeTypes = {
".js": "text/javascript",
".mjs": "application/javascript",
".css": "text/css",
".html": "text/html",
};
let contentType = mimeTypes[extname] || "application/octet-stream";
// Set CORS headers
res.setHeader("Access-Control-Allow-Origin", "*"); // Adjust as necessary for security
// Check if the file exists and serve it, otherwise serve a 404 page
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === "ENOENT") {
// File not found
res.writeHead(404, { "Content-Type": "text/html" });
res.end("<h1>404 Not Found</h1>", "utf-8");
} else {
// Some server error
res.writeHead(500);
res.end(`Server error: ${error.code}`, "utf-8");
}
} else {
// If no error, serve the file
res.writeHead(200, { "Content-Type": contentType });
res.end(content, "utf-8");
}
});
});
server.listen(8000);
return () => {
server.close();
};
},
};
});