From 303715f82c09cd1e64ed6bd8b5e51dcc9db9af28 Mon Sep 17 00:00:00 2001 From: "guangfei.zhao" Date: Tue, 28 Oct 2025 15:28:56 +0800 Subject: [PATCH] feat: add flask dev mode and optimize SVG handling - Optimize SVG loading using import.meta.glob and manual chunking --- .env | 6 ++++-- package.json | 1 + src/assets/svg/file-icon/webp.svg | 10 ++++++++++ src/components/AppSvgIcon.tsx | 21 ++++++++++++++++----- src/utils/request.ts | 15 ++++++++++++--- vite.config.ts | 16 ++++++++++++++-- 6 files changed, 57 insertions(+), 12 deletions(-) diff --git a/.env b/.env index 7e5f5d5..0983704 100644 --- a/.env +++ b/.env @@ -1,5 +1,7 @@ -VITE_API_BASE_URL = http://150.158.121.95 -# VITE_API_BASE_URL = http://154.9.253.114:9380 +# VITE_API_BASE_URL +VITE_API_BASE_URL = http://154.9.253.114:9380 +# VITE_FLASK_API_BASE_URL +VITE_FLASK_API_BASE_URL = http://150.158.121.95 VITE_RSA_PUBLIC_KEY="-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArq9XTUSeYr2+N1h3Afl/z8Dse/2yD0ZGrKwx+EEEcdsBLca9Ynmx3nIB5obmLlSfmskLpBo0UACBmB5rEjBp2Q2f3AG3Hjd4B+gNCG6BDaawuDlgANIhGnaTLrIqWrrcm4EMzJOnAOI1fgzJRsOOUEfaS318Eq9OVO3apEyCCt0lOQK6PuksduOjVxtltDav+guVAA068NrPYmRNabVKRNLJpL8w4D44sfth5RvZ3q9t+6RTArpEtc5sh5ChzvqPOzKGMXW83C95TxmXqpbK6olN4RevSfVjEAgCydH6HN6OhtOQEcnrU97r9H0iZOWwbw3pVrZiUkuRD1R56Wzs2wIDAQAB diff --git a/package.json b/package.json index 6d88258..ac8c83e 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "type": "module", "scripts": { "dev": "vite", + "dev:flask": "vite --mode flask", "build": "tsc -b && vite build", "build:flask": "tsc -b && vite build --mode flask", "lint": "eslint .", diff --git a/src/assets/svg/file-icon/webp.svg b/src/assets/svg/file-icon/webp.svg index e69de29..3657e6b 100644 --- a/src/assets/svg/file-icon/webp.svg +++ b/src/assets/svg/file-icon/webp.svg @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/src/components/AppSvgIcon.tsx b/src/components/AppSvgIcon.tsx index 4f555c5..18d73e9 100644 --- a/src/components/AppSvgIcon.tsx +++ b/src/components/AppSvgIcon.tsx @@ -2,7 +2,12 @@ import React from "react"; import { SvgIcon, type SvgIconProps } from "@mui/material"; import logger from "@/utils/logger"; -const svgPath = '/src/assets/svg' +// 使用 import.meta.glob 预加载所有 SVG 文件 +const svgModules = import.meta.glob('/src/assets/svg/**/*.svg', { + query: '?react', + import: 'default', + eager: false +}); interface AppSvgIconProps extends SvgIconProps { name: string; @@ -39,10 +44,16 @@ export default function AppSvgIcon(props: AppSvgIconProps) { const loadIcon = async () => { try { setLoading(true); - const iconPath = `${svgPath}${pointPath}/${name}.svg?react`; - // logger.debug(`[AppSvgIcon] 加载图标: ${iconPath}`); - const iconModule = await import(/* @vite-ignore */ iconPath); - setIcon(() => iconModule.default); + const iconPath = `/src/assets/svg${pointPath}/${name}.svg`; + + // 使用预定义的模块映射 + const moduleLoader = svgModules[iconPath]; + if (moduleLoader) { + const iconModule = await moduleLoader(); + setIcon(() => iconModule as React.FC>); + } else { + throw new Error(`Icon not found: ${iconPath}`); + } } catch (error) { logger.warn(`[AppSvgIcon] 未找到图标: ${name}`, error); setIcon(null); diff --git a/src/utils/request.ts b/src/utils/request.ts index 88c8e44..0500d85 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -74,9 +74,18 @@ const convertTheKeysOfTheObjectToSnake = (obj: any): any => { return result; }; +const getBaseURL = (): string => { + if (import.meta.env.DEV) { + if (import.meta.env.MODE == 'flask') { + return import.meta.env.VITE_FLASK_API_BASE_URL; + } + } + return import.meta.env.VITE_API_BASE_URL; +} + // 创建axios实例 const request: AxiosInstance = axios.create({ - baseURL: import.meta.env.VITE_API_BASE_URL, + baseURL: getBaseURL(), timeout: 300000, headers: { 'Content-Type': 'application/json', @@ -107,7 +116,7 @@ request.interceptors.request.use( // 添加授权头 const authorization = getAuthorization(); if (authorization && !config.headers?.skipToken) { - config.headers.Authorization = authorization; + config.headers['Authorization'] = authorization; } return config; @@ -119,7 +128,7 @@ request.interceptors.request.use( // 响应拦截器 request.interceptors.response.use( - (response: AxiosResponse) => { + (response: AxiosResponse) => { const { status } = response; // 处理特定状态码 diff --git a/vite.config.ts b/vite.config.ts index 787b23b..36656ce 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -11,9 +11,21 @@ export default defineConfig({ svgrOptions: { icon: true, prettier: true, - } - }) + }, + }), ], + build: { + rollupOptions: { + output: { + manualChunks: (id) => { + // 将所有 SVG 文件打包到一个单独的 chunk 中 + if (id.includes('.svg') && id.includes('?react')) { + return 'svg-icons'; + } + } + } + } + }, server: { host: '0.0.0.0', allowedHosts: ['154.9.253.114', 'localhost', 'teres.deep-pilot.chat'],