Ver código fonte

🔧 chore(build): 重构构建脚本和服务器入口

- 重命名并调整构建脚本:
  - 移除冗余的 build:server.tsx 命令
  - 将 build:server 命令指向 src/server/index.tsx
  - 新增 build:api 命令用于构建 API 服务
  - 更新 preview 命令直接使用 node server 启动

- 优化服务器路由加载逻辑:
  - 根据环境动态加载 API 路由
  - 开发环境: 直接加载源代码中的 API 路由
  - 生产环境: 加载构建后的 API 路由
  - 移除静态导入,采用动态导入优化性能

- 修复模板替换问题:
  - 修复 CSS 链接替换的引号匹配问题
  - 优化模板文件读取逻辑,确保生产环境正确加载构建后的模板
yourname 7 meses atrás
pai
commit
96becdbe31
2 arquivos alterados com 15 adições e 15 exclusões
  1. 3 3
      package.json
  2. 12 12
      server.js

+ 3 - 3
package.json

@@ -7,9 +7,9 @@
     "dev": "PORT=8080 tsx server",
     "build": "npm run build:client && npm run build: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"
+    "build:server": "vite build --ssr src/server/index.tsx --outDir dist/server",
+    "build:api": "vite build --ssr src/server/api.ts --outDir dist/api",
+    "preview": "PORT=8080 cross-env NODE_ENV=production node server"
   },
   "dependencies": {
     "@ant-design/icons": "^6.0.0",

+ 12 - 12
server.js

@@ -7,16 +7,10 @@ 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路由
 
-
-
-app.route('/', api);
-
-
 // Constants
 const isProduction = process.env.NODE_ENV === 'production';
 const port = process.env.PORT || 8080;
@@ -26,8 +20,6 @@ const ABORT_DELAY = 10000;
 // 解析基础路径为 URL 对象,方便后续处理
 const baseUrl = new URL(base, `http://localhost:${port}`);
 
-
-
 // 使用 Hono 的 serve 启动服务器
 const parentServer = serve({
   fetch: app.fetch,
@@ -43,6 +35,14 @@ const parentServer = serve({
 //   templateHtml = await fs.readFile('./dist/client/index.html', 'utf-8');
 // }
 
+if (!isProduction) {
+  const api = (await import('./src/server/api.ts')).default
+  app.route('/', api);
+}else{
+  const api = (await import('./dist/api/api.js')).default
+  app.route('/', api);
+}
+
 // 生产环境中间件
 let compressionMiddleware;
 let sirvMiddleware;
@@ -251,6 +251,8 @@ app.use(async (c) => {
       // const module = (await import('/src/server/index.tsx'))
       // template = module.template;
 
+      // 读取原始模板
+      const module = await import('./dist/server/index.js');
        // 读取 manifest.json 并处理模板
       try {
         // 读取 manifest
@@ -264,9 +266,7 @@ app.use(async (c) => {
           throw new Error('manifest 中未找到 index.html 入口配置');
         }
 
-        // 读取原始模板
-        const module = await import('./dist/server/index.js');
-        let template = module.template;
+        template = module.template;
 
         // 替换 CSS 链接
         const cssLinks = indexManifest.css?.map(cssFile => {
@@ -281,7 +281,7 @@ app.use(async (c) => {
 
         // 执行替换
         template = template
-          .replace(/<link href='\/src\/style.css' rel="stylesheet" \/>/, cssLinks)
+          .replace(/<link href="\/src\/style.css" rel="stylesheet"\/>/, cssLinks)
           .replace(/<script type="module" src="\/src\/client\/index.tsx"><\/script>/, entryScript);
 
       } catch (err) {