From f03150128622867edebc46ca418b9c6814f01b56 Mon Sep 17 00:00:00 2001 From: kk <875203880@qq.com> Date: Thu, 2 Jul 2026 18:36:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=A4=84=E7=90=86public?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E4=B8=8B=E7=9A=84=E9=9D=99=E6=80=81=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vite.config.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/vite.config.js b/vite.config.js index 07b42d6..33d370a 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,8 +1,31 @@ import { defineConfig } from 'vite' import uni from '@dcloudio/vite-plugin-uni' +import { existsSync, readFileSync } from 'fs' +import { resolve } from 'path' + +// 开发服务器:显式处理 public 目录下的静态文件,避免被 SPA history 路由拦截 +function servePublicFiles() { + return { + name: 'serve-public-files', + configureServer(server) { + server.middlewares.use((req, res, next) => { + if (req.url) { + const filePath = resolve(__dirname, 'public', req.url.slice(1)) + if (existsSync(filePath) && filePath.endsWith('.txt')) { + const content = readFileSync(filePath, 'utf-8') + res.setHeader('Content-Type', 'text/plain; charset=utf-8') + res.end(content) + return + } + } + next() + }) + }, + } +} export default defineConfig({ - plugins: [uni()], + plugins: [servePublicFiles(), uni()], css: { preprocessorOptions: { scss: { @@ -10,4 +33,5 @@ export default defineConfig({ }, }, }, + publicDir: 'public', })