@iconoma/studio 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server.js +70 -65
- package/package.json +4 -5
package/dist/server.js
CHANGED
|
@@ -10,72 +10,77 @@ const __filename = fileURLToPath(import.meta.url);
|
|
|
10
10
|
const __dirname = dirname(__filename);
|
|
11
11
|
const isProduction = __filename.endsWith(".js");
|
|
12
12
|
const templateHtml = isProduction
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
? await fs.readFile(path.join(__dirname, "client", "index.html"), "utf-8")
|
|
14
|
+
: "";
|
|
15
15
|
export async function createServer({ port } = {}) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
app.use("*all", async (req, res) => {
|
|
37
|
-
try {
|
|
38
|
-
const url = req.originalUrl;
|
|
39
|
-
let template;
|
|
40
|
-
let render;
|
|
41
|
-
if (!isProduction) {
|
|
42
|
-
if (!vite) return res.status(500).end("Internal Server Error");
|
|
43
|
-
template = await fs.readFile("./index.html", "utf-8");
|
|
44
|
-
template = await vite.transformIndexHtml(url, template);
|
|
45
|
-
render = (await vite.ssrLoadModule("/src/entry-server.tsx")).render;
|
|
46
|
-
} else {
|
|
47
|
-
template = templateHtml;
|
|
48
|
-
// @ts-ignore
|
|
49
|
-
render = (await import("./server/entry-server.js")).render;
|
|
50
|
-
}
|
|
51
|
-
const rendered = await render(url);
|
|
52
|
-
const html = template
|
|
53
|
-
.replace(`<!--app-head-->`, rendered.head ?? "")
|
|
54
|
-
.replace(`<!--app-html-->`, rendered.html ?? "");
|
|
55
|
-
res.status(200).set({ "Content-Type": "text/html" }).send(html);
|
|
56
|
-
} catch (e) {
|
|
57
|
-
vite?.ssrFixStacktrace(e);
|
|
58
|
-
console.log(e.stack);
|
|
59
|
-
res.status(500).end(e.stack);
|
|
16
|
+
if (!port) {
|
|
17
|
+
port = await getPort({ port: [4545, 4546, 4547, 4548, 4549, 5173] });
|
|
18
|
+
}
|
|
19
|
+
const app = express();
|
|
20
|
+
app.use("/api", apiRoutes);
|
|
21
|
+
let vite;
|
|
22
|
+
if (!isProduction) {
|
|
23
|
+
const { createServer } = await import("vite");
|
|
24
|
+
vite = await createServer({
|
|
25
|
+
server: { middlewareMode: true },
|
|
26
|
+
appType: "custom",
|
|
27
|
+
base: "/",
|
|
28
|
+
});
|
|
29
|
+
app.use(vite.middlewares);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const compression = (await import("compression")).default;
|
|
33
|
+
const sirv = (await import("sirv")).default;
|
|
34
|
+
app.use(compression());
|
|
35
|
+
app.use("/", sirv(path.join(__dirname, "client"), { extensions: [] }));
|
|
60
36
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
37
|
+
app.use("*all", async (req, res) => {
|
|
38
|
+
try {
|
|
39
|
+
const url = req.originalUrl;
|
|
40
|
+
let template;
|
|
41
|
+
let render;
|
|
42
|
+
if (!isProduction) {
|
|
43
|
+
if (!vite)
|
|
44
|
+
return res.status(500).end("Internal Server Error");
|
|
45
|
+
template = await fs.readFile("./index.html", "utf-8");
|
|
46
|
+
template = await vite.transformIndexHtml(url, template);
|
|
47
|
+
render = (await vite.ssrLoadModule("/src/entry-server.tsx")).render;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
template = templateHtml;
|
|
51
|
+
// @ts-ignore
|
|
52
|
+
render = (await import("./server/entry-server.js")).render;
|
|
53
|
+
}
|
|
54
|
+
const rendered = await render(url);
|
|
55
|
+
const html = template
|
|
56
|
+
.replace(`<!--app-head-->`, rendered.head ?? "")
|
|
57
|
+
.replace(`<!--app-html-->`, rendered.html ?? "");
|
|
58
|
+
res.status(200).set({ "Content-Type": "text/html" }).send(html);
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
vite?.ssrFixStacktrace(e);
|
|
62
|
+
console.log(e.stack);
|
|
63
|
+
res.status(500).end(e.stack);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
const url = `http://localhost:${port}`;
|
|
67
|
+
const httpServer = http.createServer(app);
|
|
68
|
+
await new Promise((resolve, reject) => {
|
|
69
|
+
httpServer.once("error", reject);
|
|
70
|
+
httpServer.listen(port, "localhost", () => resolve());
|
|
71
|
+
});
|
|
72
|
+
async function close() {
|
|
73
|
+
if (vite) {
|
|
74
|
+
try {
|
|
75
|
+
await vite.close();
|
|
76
|
+
}
|
|
77
|
+
catch { }
|
|
78
|
+
vite = undefined;
|
|
79
|
+
}
|
|
80
|
+
await new Promise((resolve) => httpServer.close(() => resolve()));
|
|
74
81
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
82
|
+
return { url, close };
|
|
83
|
+
}
|
|
84
|
+
if (!isProduction) {
|
|
85
|
+
createServer().then(({ url }) => console.log(`Server started at ${url}`));
|
|
78
86
|
}
|
|
79
|
-
// if (!isProduction) {
|
|
80
|
-
createServer().then(({ url }) => console.log(`Server started at ${url}`));
|
|
81
|
-
// }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iconoma/studio",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/server.js",
|
|
6
6
|
"private": false,
|
|
@@ -59,8 +59,7 @@
|
|
|
59
59
|
"react-router": "^7.11.0",
|
|
60
60
|
"sirv": "^3.0.2",
|
|
61
61
|
"svgo": "^4.0.0",
|
|
62
|
-
"zod": "^3.25.76"
|
|
63
|
-
"@iconoma/ui": "0.0.1"
|
|
62
|
+
"zod": "^3.25.76"
|
|
64
63
|
},
|
|
65
64
|
"devDependencies": {
|
|
66
65
|
"@types/canvas-confetti": "^1.9.0",
|
|
@@ -72,8 +71,8 @@
|
|
|
72
71
|
"ts-add-js-extension": "^1.6.6",
|
|
73
72
|
"vite": "^7.3.0",
|
|
74
73
|
"tsx": "^4.21.0",
|
|
75
|
-
"@iconoma/
|
|
76
|
-
"@iconoma/
|
|
74
|
+
"@iconoma/eslint-config": "^0.0.0",
|
|
75
|
+
"@iconoma/typescript-config": "0.0.0"
|
|
77
76
|
},
|
|
78
77
|
"exports": {
|
|
79
78
|
".": "./dist/server.js"
|