wildpig 1.4.0 → 1.5.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/package.json +1 -3
- package/scripts/build.ts +2 -2
- package/scripts/devServer.ts +6 -4
- package/scripts/prodServer.ts +6 -3
package/package.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wildpig",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"author": "eriktse",
|
|
5
|
-
"main": "index.ts",
|
|
6
5
|
"type": "module",
|
|
7
6
|
"bin": {
|
|
8
7
|
"wildpig": "./bin/cli.ts"
|
|
@@ -29,7 +28,6 @@
|
|
|
29
28
|
],
|
|
30
29
|
"license": "ISC",
|
|
31
30
|
"scripts": {
|
|
32
|
-
"publish": "yalc publish",
|
|
33
31
|
"dev": "wildpig dev",
|
|
34
32
|
"build": "wildpig build",
|
|
35
33
|
"start": "wildpig start"
|
package/scripts/build.ts
CHANGED
|
@@ -6,14 +6,14 @@ const prebuild = async () => {
|
|
|
6
6
|
const promises = [];
|
|
7
7
|
// 先编译客户端代码
|
|
8
8
|
promises.push(viteBuild({
|
|
9
|
-
configFile: path.resolve(__dirname, "
|
|
9
|
+
configFile: path.resolve(__dirname, "../../../vite.config.ts"),
|
|
10
10
|
build: {
|
|
11
11
|
outDir: "./dist/client",
|
|
12
12
|
},
|
|
13
13
|
}));
|
|
14
14
|
// 编译服务端入口文件
|
|
15
15
|
promises.push(viteBuild({
|
|
16
|
-
configFile: path.resolve(__dirname, "
|
|
16
|
+
configFile: path.resolve(__dirname, "../../../vite.config.ts"),
|
|
17
17
|
|
|
18
18
|
build: {
|
|
19
19
|
rollupOptions:{
|
package/scripts/devServer.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { getApiRouteModules } from "./apiRoutes";
|
|
2
2
|
import { createServer as createViteServer } from "vite";
|
|
3
|
-
import {
|
|
3
|
+
import { matchRoutes } from "react-router";
|
|
4
4
|
import fs from "node:fs";
|
|
5
5
|
|
|
6
6
|
const __dirname = import.meta.dirname;
|
|
7
7
|
|
|
8
8
|
// 用户代码
|
|
9
|
-
import pageRoutes from "@/router/routes";
|
|
10
9
|
import path from "node:path";
|
|
11
10
|
import chalk from "chalk";
|
|
11
|
+
import { WildPigRouteObject } from "router/types";
|
|
12
12
|
|
|
13
13
|
const env = process.env;
|
|
14
14
|
const port = env.PORT || 3000;
|
|
@@ -33,12 +33,14 @@ const viteHandler = (apiModules: any) => async (request: Request) => {
|
|
|
33
33
|
viteURL.port = viteServer.config.server.port.toString();
|
|
34
34
|
console.log("转发请求:" + viteURL.toString());
|
|
35
35
|
const response = await fetch(viteURL.toString(), {
|
|
36
|
-
headers: request.headers,
|
|
37
36
|
method: request.method,
|
|
37
|
+
headers: request.headers,
|
|
38
|
+
body: request.body,
|
|
38
39
|
});
|
|
39
|
-
return response;
|
|
40
|
+
return response.clone();
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
const pageRoutes = (await viteServer.ssrLoadModule("@/router/routes.ts"!)).default as WildPigRouteObject[];
|
|
42
44
|
const matches = matchRoutes(pageRoutes, url.pathname);
|
|
43
45
|
if(!matches)return new Response("404 Not Found", { status: 404 });
|
|
44
46
|
|
package/scripts/prodServer.ts
CHANGED
|
@@ -4,8 +4,9 @@ import { matchRoutes } from "react-router";
|
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
|
|
6
6
|
// 用户代码
|
|
7
|
-
|
|
7
|
+
const pageRoutes = (await import("@/router/routes"!)).default as WildPigRouteObject[];
|
|
8
8
|
import chalk from "chalk";
|
|
9
|
+
import { WildPigRouteObject } from "router/types";
|
|
9
10
|
|
|
10
11
|
const env = process.env;
|
|
11
12
|
const port = env.PORT || 3000;
|
|
@@ -46,8 +47,7 @@ export const startServer = async () => {
|
|
|
46
47
|
"/*": async (request: Request) => {
|
|
47
48
|
// 判断pathname是否匹配pageRoutes
|
|
48
49
|
const url = new URL(request.url);
|
|
49
|
-
|
|
50
|
-
if(!matches){
|
|
50
|
+
if(url.pathname.includes(".") || url.pathname.startsWith("/@") || url.pathname.startsWith("/assets")){
|
|
51
51
|
const filepath = "./client" + url.pathname;
|
|
52
52
|
// 检查文件是否存在
|
|
53
53
|
if(fs.existsSync(filepath) && fs.statSync(filepath).isFile()){
|
|
@@ -58,6 +58,9 @@ export const startServer = async () => {
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
// 请求服务端数据
|
|
61
|
+
const matches = matchRoutes(pageRoutes, url.pathname);
|
|
62
|
+
if(!matches)return new Response("Not Found", {status: 404});
|
|
63
|
+
|
|
61
64
|
const matchRoute = matches.at(-1)!;
|
|
62
65
|
let serverDataApi = matchRoute.route.serverDataApi;
|
|
63
66
|
const getServerData = async () => {
|