wildpig 1.0.11 → 1.0.13
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 +14 -2
- package/bun.lock +13 -294
- package/index.ts +1 -1
- package/package.json +12 -19
- package/public/devIndex.html +13 -0
- package/public/render.js +22297 -111
- package/public/render.js.br +0 -0
- package/public/render.js.map +27 -0
- package/router/MetaGuard.tsx +38 -0
- package/scripts/WildPig.ts +12 -4
- package/scripts/packageStatic.ts +11 -2
- package/scripts/prepareRoutes.ts +2 -2
- package/test.ts +3 -0
- package/utils/client/environment.ts +17 -0
- package/src/App.tsx +0 -7
- package/src/api/hello/index.ts +0 -16
- package/src/middleware.ts +0 -13
- package/src/page/home/index.tsx +0 -6
- package/src/page/home/meta.ts +0 -8
- package/src/page/index.tsx +0 -8
- package/src/page/layout.tsx +0 -10
- package/src/page/meta.ts +0 -8
- package/src/router/guard.tsx +0 -14
- package/src/router/index.tsx +0 -11
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { getBrowserEnvironment } from "../utils/client/environment";
|
|
2
|
+
import { createBrowserRouter } from "react-router";
|
|
3
|
+
// 为了获取type而创建的router
|
|
4
|
+
const browserRouterForType = createBrowserRouter([
|
|
5
|
+
{
|
|
6
|
+
path: "/just-for-type",
|
|
7
|
+
Component: null,
|
|
8
|
+
}
|
|
9
|
+
]);
|
|
10
|
+
export type BrowserRouter = typeof browserRouterForType;
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 获取meta信息
|
|
15
|
+
* @param url like "/home?a=1"
|
|
16
|
+
*/
|
|
17
|
+
const refreshMeta = (url: string) =>{
|
|
18
|
+
console.log("get url:", url);
|
|
19
|
+
fetch("/_WILDPIG_META_API" + url).then(res => res.json()).then(({title}) => {
|
|
20
|
+
document.title = title;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const RouterMetaGuard = (browserRouter: BrowserRouter): BrowserRouter => {
|
|
25
|
+
|
|
26
|
+
// 检查meta,判断是否是dev环境
|
|
27
|
+
const isDev = getBrowserEnvironment() === "development";
|
|
28
|
+
if(isDev){
|
|
29
|
+
// 开发环境首次执行,生产阶段为了seo,无需首次执行
|
|
30
|
+
refreshMeta(window.location.pathname + window.location.search);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 每次跳转都会触发,包括 <Link>/navigate/前进后退
|
|
34
|
+
browserRouter.subscribe(({ location }) => {
|
|
35
|
+
refreshMeta(location.pathname + location.search);
|
|
36
|
+
});
|
|
37
|
+
return browserRouter;
|
|
38
|
+
}
|
package/scripts/WildPig.ts
CHANGED
|
@@ -2,8 +2,10 @@ import chalk from "chalk";
|
|
|
2
2
|
import { htmlString } from "./htmlString";
|
|
3
3
|
import { routes, metaRoutes } from "./prepareRoutes";
|
|
4
4
|
import { readFileSync } from "node:fs";
|
|
5
|
+
import devIndexHtml from "#/public/devIndex.html"
|
|
5
6
|
|
|
6
7
|
const env = process.env;
|
|
8
|
+
const isDev = env.NODE_ENV === "development";
|
|
7
9
|
|
|
8
10
|
|
|
9
11
|
export const startServer = () => {
|
|
@@ -21,10 +23,11 @@ export const startServer = () => {
|
|
|
21
23
|
"/render.js": () => new Response((readFileSync("./public/render.js.br")), {
|
|
22
24
|
headers: {
|
|
23
25
|
"Content-Encoding": "br",
|
|
24
|
-
"
|
|
26
|
+
"Content-Type": "text/javascript; charset=utf-8",
|
|
27
|
+
"Cache-Control": isDev ? "no-cache" : "public, max-age=31536000, immutable"
|
|
25
28
|
}
|
|
26
29
|
}),
|
|
27
|
-
"/*": async (request) => {
|
|
30
|
+
"/*": isDev ? devIndexHtml : async (request) => {
|
|
28
31
|
const url = "/" + (request.url.split("/")[3] || "");
|
|
29
32
|
// console.log("url:", url);
|
|
30
33
|
const pathname = "/_WILDPIG_META_API" + url.split("?")[0];
|
|
@@ -42,7 +45,7 @@ export const startServer = () => {
|
|
|
42
45
|
})
|
|
43
46
|
},
|
|
44
47
|
},
|
|
45
|
-
development:
|
|
48
|
+
development: isDev
|
|
46
49
|
})
|
|
47
50
|
console.clear();
|
|
48
51
|
console.log(` __ __ _ _ _ ____ _
|
|
@@ -51,8 +54,13 @@ console.log(` __ __ _ _ _ ____ _
|
|
|
51
54
|
\\ V V / | || || (_| | | __/ | || (_| |
|
|
52
55
|
\\_/\\_/ |_||_| \\__,_| |_| |_| \\__, |
|
|
53
56
|
|___/ `)
|
|
54
|
-
console.log(chalk.blue.bgGreen(" 🐗 WildPig version 1.0.
|
|
57
|
+
console.log(chalk.blue.bgGreen(" 🐗 WildPig version 1.0.11 by eriktse "));
|
|
55
58
|
console.log(chalk.green(" Strong & Fast Fullstack Framework\n"));
|
|
56
59
|
console.log(chalk.green("✨ WildPig is running on port " + env.PORT || 3000));
|
|
60
|
+
if(isDev){
|
|
61
|
+
console.log(chalk.yellow("💻 Wildpig is Running in development mode."));
|
|
62
|
+
}else{
|
|
63
|
+
console.log(chalk.green("💻 Wildpig is Running in production mode."));
|
|
64
|
+
}
|
|
57
65
|
console.log(chalk.green("🔗 Click to debug in Browser: http://localhost" + ":" + (env.PORT || 3000)));
|
|
58
66
|
}
|
package/scripts/packageStatic.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Bun from "bun";
|
|
2
2
|
import fs, { readFileSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { brotliCompressSync } from "node:zlib";
|
|
3
|
+
import { brotliCompressSync, constants } from "node:zlib";
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
export const packageStatic = async () => {
|
|
@@ -12,7 +12,16 @@ export const packageStatic = async () => {
|
|
|
12
12
|
minify: true,
|
|
13
13
|
});
|
|
14
14
|
// 将 ./public/render.js 转为 ./public/render.br
|
|
15
|
-
writeFileSync("./dist/public/render.js.br", brotliCompressSync(
|
|
15
|
+
writeFileSync("./dist/public/render.js.br", brotliCompressSync(
|
|
16
|
+
Buffer.from(readFileSync("./dist/public/render.js")),
|
|
17
|
+
{
|
|
18
|
+
params: {
|
|
19
|
+
[constants.BROTLI_PARAM_QUALITY]: 11, // 最高质量
|
|
20
|
+
[constants.BROTLI_PARAM_LGWIN]: 16, // 最大窗口 16 MB
|
|
21
|
+
[constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_TEXT, // 针对文本优化
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
));
|
|
16
25
|
|
|
17
26
|
/** 打包ico */
|
|
18
27
|
fs.copyFileSync("./public/favicon.ico", "./dist/public/favicon.ico");
|
package/scripts/prepareRoutes.ts
CHANGED
|
@@ -16,8 +16,8 @@ if(isDev){
|
|
|
16
16
|
await Bun.build({
|
|
17
17
|
entrypoints: ["./public/render.tsx"],
|
|
18
18
|
outdir: "./public",
|
|
19
|
-
format: "
|
|
20
|
-
|
|
19
|
+
format: "esm",
|
|
20
|
+
sourcemap: true,
|
|
21
21
|
});
|
|
22
22
|
// 将 ./public/render.js 转为 ./public/render.br
|
|
23
23
|
writeFileSync("./public/render.js.br", brotliCompressSync(Buffer.from(readFileSync("./public/render.js"))));
|
package/test.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export const getBrowserEnvironment = () => {
|
|
5
|
+
const metaTags = document.head.getElementsByTagName("meta");
|
|
6
|
+
if(!metaTags || metaTags.length === 0)
|
|
7
|
+
return "production";
|
|
8
|
+
// 如果某个meta标签的name属性为wildpig-environment,且content属性为development,则返回development
|
|
9
|
+
if(Array.from(metaTags).some(metaTag =>
|
|
10
|
+
metaTag.getAttribute("name") === "wildpig-environment"
|
|
11
|
+
&& metaTag.getAttribute("content") === "development"
|
|
12
|
+
)){
|
|
13
|
+
return "development";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return "production";
|
|
17
|
+
}
|
package/src/App.tsx
DELETED
package/src/api/hello/index.ts
DELETED
package/src/middleware.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export const middleware = async (req: Request, next: (req: Request) => Promise<Response>) => {
|
|
7
|
-
console.log("middleware: ", req.url);
|
|
8
|
-
|
|
9
|
-
const response = await next(req);
|
|
10
|
-
|
|
11
|
-
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
12
|
-
return response;
|
|
13
|
-
}
|
package/src/page/home/index.tsx
DELETED
package/src/page/home/meta.ts
DELETED
package/src/page/index.tsx
DELETED
package/src/page/layout.tsx
DELETED
package/src/page/meta.ts
DELETED
package/src/router/guard.tsx
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { BrowserRouter } from "."
|
|
2
|
-
|
|
3
|
-
export const RouterGuard = (browserRouter: BrowserRouter) => {
|
|
4
|
-
// 每次跳转都会触发,包括 <Link>/navigate/前进后退
|
|
5
|
-
browserRouter.subscribe(({ location }) => {
|
|
6
|
-
// navigationType 值:'PUSH' | 'POP' | 'REPLACE'
|
|
7
|
-
const url = location.pathname + location.search;
|
|
8
|
-
// apiGetMeta(location.pathname);
|
|
9
|
-
// 请求对应的meta api,修改title
|
|
10
|
-
fetch("/_WILDPIG_META_API" + url).then(res => res.json()).then(({title}) => {
|
|
11
|
-
document.title = title;
|
|
12
|
-
})
|
|
13
|
-
});
|
|
14
|
-
}
|
package/src/router/index.tsx
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { MainLayout } from "@/page/layout";
|
|
2
|
-
import { createBrowserRouter } from "react-router";
|
|
3
|
-
|
|
4
|
-
export const browserRouter = createBrowserRouter([
|
|
5
|
-
{
|
|
6
|
-
path: "/",
|
|
7
|
-
Component: MainLayout,
|
|
8
|
-
},
|
|
9
|
-
]);
|
|
10
|
-
|
|
11
|
-
export type BrowserRouter = typeof browserRouter;
|