wildpig 1.3.19 → 1.4.0
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/README.md +5 -1
- package/bin/cli.ts +4 -4
- package/build/built-api-routes.ts +4 -6
- package/bun.lock +219 -1
- package/entry/client.tsx +12 -0
- package/entry/server.tsx +25 -0
- package/index.html +13 -0
- package/package.json +7 -3
- package/router/ServerDataGuard.tsx +12 -2
- package/scripts/build.ts +25 -5
- package/scripts/devServer.ts +132 -0
- package/scripts/prodServer.ts +114 -0
- package/scripts/server.ts +3 -21
- package/public/devHtml.html +0 -12
- package/public/render.tsx +0 -29
- package/public/tailwindcss4.js +0 -8
- package/scripts/WildPig.tsx +0 -103
- package/scripts/packageStatic.ts +0 -21
- package/test.ts +0 -3
package/scripts/WildPig.tsx
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
|
-
import { getApiRouteModules } from "./apiRoutes";
|
|
3
|
-
import { renderToReadableStream } from "react-dom/server";
|
|
4
|
-
import devHtml from "../public/devHtml.html"
|
|
5
|
-
|
|
6
|
-
import { createStaticHandler, createStaticRouter, matchRoutes } from "react-router";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const __dirname = import.meta.dirname;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
// 用户代码
|
|
13
|
-
import pageRoutes from "#/src/router/routes";
|
|
14
|
-
import { App } from "@/App"
|
|
15
|
-
import path from "node:path";
|
|
16
|
-
|
|
17
|
-
const env = process.env;
|
|
18
|
-
const port = env.PORT || 3000;
|
|
19
|
-
const hostname = env.HOST || env.HOSTNAME || "localhost";
|
|
20
|
-
const isDev = env.NODE_ENV === "development";
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if(false && isDev){
|
|
24
|
-
setTimeout(() => {import("../public/render")}, 0);
|
|
25
|
-
/** 打包js */
|
|
26
|
-
await Bun.build({
|
|
27
|
-
entrypoints: [path.resolve(__dirname, "../public/render.tsx")],
|
|
28
|
-
outdir: path.resolve(__dirname, "../public"),
|
|
29
|
-
format: "esm",
|
|
30
|
-
minify: false,
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export const startServer = async () => {
|
|
35
|
-
|
|
36
|
-
// 确保重启后可以重新拿到路由
|
|
37
|
-
const apiModules = await getApiRouteModules(isDev ? "dev" : "prod") as any;
|
|
38
|
-
return Bun.serve({
|
|
39
|
-
port,
|
|
40
|
-
hostname,
|
|
41
|
-
routes:{
|
|
42
|
-
...apiModules,
|
|
43
|
-
// 这个来自用户文件
|
|
44
|
-
"/favicon.ico": () => new Response(Bun.file("./public/favicon.ico"), {
|
|
45
|
-
headers: {
|
|
46
|
-
"content-type": "image/x-icon",
|
|
47
|
-
}
|
|
48
|
-
}),
|
|
49
|
-
"/render.js": () => new Response((readFileSync("./public/render.js")), {
|
|
50
|
-
headers: {
|
|
51
|
-
"Content-Type": "text/javascript; charset=utf-8",
|
|
52
|
-
"Cache-Control": isDev ? "no-cache" : "public, max-age=31536000, immutable"
|
|
53
|
-
}
|
|
54
|
-
}),
|
|
55
|
-
"/*": isDev ? devHtml : async (request: Request) => {
|
|
56
|
-
// 判断pathname是否匹配pageRoutes
|
|
57
|
-
const url = new URL(request.url);
|
|
58
|
-
const matches = matchRoutes(pageRoutes, url.pathname);
|
|
59
|
-
if(!matches){
|
|
60
|
-
return new Response("404 Not Found", {
|
|
61
|
-
status: 404,
|
|
62
|
-
headers: {
|
|
63
|
-
"content-type": "text/plain; charset=utf-8",
|
|
64
|
-
"Access-Control-Allow-Origin": "*",
|
|
65
|
-
}
|
|
66
|
-
})
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
let { query, dataRoutes } = createStaticHandler(pageRoutes);
|
|
70
|
-
let context = await query(request);
|
|
71
|
-
if (context instanceof Response) return context;
|
|
72
|
-
let router = createStaticRouter(dataRoutes, context);
|
|
73
|
-
|
|
74
|
-
// 请求服务端数据
|
|
75
|
-
const matchRoute = matches.at(-1)!;
|
|
76
|
-
let serverDataApi = matchRoute.route.serverDataApi;
|
|
77
|
-
const getServerData = async () => {
|
|
78
|
-
if(!serverDataApi)return undefined;
|
|
79
|
-
// 需要请求服务端数据, 替换动态参数
|
|
80
|
-
for(const [key, value] of Object.entries(matchRoute.params)){
|
|
81
|
-
if(value)serverDataApi = serverDataApi.replace(":" + key, value);
|
|
82
|
-
}
|
|
83
|
-
const pathname = serverDataApi.split("?")[0]; // 获取路径
|
|
84
|
-
const handleUrl = "http://" + hostname + ":" + port + pathname;
|
|
85
|
-
const serverData = await fetch(handleUrl).then(r => r.json());
|
|
86
|
-
return serverData;
|
|
87
|
-
};
|
|
88
|
-
let serverData = await getServerData();
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const stream = await renderToReadableStream(<App router={router} serverData={serverData} />);
|
|
92
|
-
return new Response(stream, {
|
|
93
|
-
headers: {
|
|
94
|
-
"content-type": "text/html; charset=utf-8",
|
|
95
|
-
"Access-Control-Allow-Origin": "*",
|
|
96
|
-
}
|
|
97
|
-
})
|
|
98
|
-
},
|
|
99
|
-
},
|
|
100
|
-
development: isDev,
|
|
101
|
-
|
|
102
|
-
})
|
|
103
|
-
}
|
package/scripts/packageStatic.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import Bun from "bun";
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
|
|
5
|
-
const __dirname = import.meta.dirname;
|
|
6
|
-
|
|
7
|
-
export const packageStatic = async () => {
|
|
8
|
-
const targetPublicPath = "./dist/public";
|
|
9
|
-
if(fs.existsSync(targetPublicPath)) fs.rmSync(targetPublicPath, { recursive: true });
|
|
10
|
-
fs.mkdirSync(targetPublicPath, {recursive: true});
|
|
11
|
-
/** 打包js */
|
|
12
|
-
await Bun.build({
|
|
13
|
-
entrypoints: [path.resolve(__dirname, "../public/render.tsx")],
|
|
14
|
-
outdir: targetPublicPath,
|
|
15
|
-
format: "esm",
|
|
16
|
-
minify: true,
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
/** 打包ico */
|
|
20
|
-
fs.copyFileSync("./public/favicon.ico", "./dist/public/favicon.ico");
|
|
21
|
-
}
|
package/test.ts
DELETED