Răsfoiți Sursa

🔧 chore(build): 调整构建和预览脚本

- 修改build:client命令,添加--manifest参数生成资源清单
- 重命名build:server为build:server.tsx,并新增build:server命令构建server.js
- 更新preview命令,直接运行生产环境构建后的server文件

🔧 chore(server): 调整服务器配置和导入方式

- 将api路由导入移至文件顶部,修复导入顺序问题
- 注释掉生产环境模板HTML缓存相关代码
- 注释掉基础路径规范化代码
- 修改生产环境渲染模块导入路径,直接导入源码文件

📝 docs(html): 添加样式表链接

- 在index.html中添加style.css样式表链接
yourname 7 luni în urmă
părinte
comite
61e3ce8827
3 a modificat fișierele cu 13 adăugiri și 10 ștergeri
  1. 1 0
      index.html
  2. 4 3
      package.json
  3. 8 7
      server.js

+ 1 - 0
index.html

@@ -5,6 +5,7 @@
   <meta charset="UTF-8" />
   <link rel="icon" type="image/svg+xml" href="/vite.svg" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+  <link href='/src/style.css' rel="stylesheet" />
   <title>Vite + React + TS</title>
   <!--app-head-->
 </head>

+ 4 - 3
package.json

@@ -6,9 +6,10 @@
   "scripts": {
     "dev": "PORT=8080 tsx server",
     "build": "npm run build:client && npm run build:server",
-    "build:client": "vite build --outDir dist/client",
-    "build:server": "vite build --ssr src/server/index.tsx --outDir dist/server",
-    "preview": "PORT=8080 cross-env NODE_ENV=production tsx server"
+    "build:client": "vite build --outDir dist/client --manifest",
+    "build:server.tsx": "vite build --ssr src/server/index.tsx --outDir dist/server",
+    "build:server": "vite build --ssr server.js --outDir dist/server",
+    "preview": "PORT=8080 cross-env NODE_ENV=production node dist/server/server"
   },
   "dependencies": {
     "@ant-design/icons": "^6.0.0",

+ 8 - 7
server.js

@@ -7,11 +7,11 @@ import { serve } from '@hono/node-server';
 import { Hono } from 'hono';
 import { createServer as createNodeServer } from 'node:http';
 import process from 'node:process';
+import api from './src/server/api.ts';
 
 // 创建 Hono 应用
 const app = new Hono();// API路由
 
-import api from './src/server/api.ts';
 
 
 app.route('/', api);
@@ -38,10 +38,10 @@ const parentServer = serve({
 });
 
 // Cached production assets
-let templateHtml = '';
-if (isProduction) {
-  templateHtml = await fs.readFile('./dist/client/index.html', 'utf-8');
-}
+// let templateHtml = '';
+// if (isProduction) {
+//   templateHtml = await fs.readFile('./dist/client/index.html', 'utf-8');
+// }
 
 // 生产环境中间件
 let compressionMiddleware;
@@ -96,7 +96,7 @@ app.use(async (c, next) => {
     }
 
     // 处理基础路径
-    const normalizedUrl = path.replace(baseUrl.pathname, '/') || '/';
+    // const normalizedUrl = path.replace(baseUrl.pathname, '/') || '/';
 
     // 开发环境:使用 Vite 中间件
     if (!isProduction && vite) {
@@ -248,7 +248,8 @@ app.use(async (c) => {
       render = module.render;
     } else {
       // 生产环境:使用缓存的模板
-      const module = (await import('./dist/server/index.js'))
+      // const module = (await import('./dist/server/index.js'))
+      const module = (await import('/src/server/index.tsx'))
       template = module.template;
       render = module.render;
     }