|
|
@@ -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) {
|