42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
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: [servePublicFiles(), uni()],
|
|
// 将构建时的 DEPLOY_ENV 环境变量注入到客户端代码中
|
|
define: {
|
|
'process.env.DEPLOY_ENV': JSON.stringify(process.env.DEPLOY_ENV || ''),
|
|
},
|
|
css: {
|
|
preprocessorOptions: {
|
|
scss: {
|
|
silenceDeprecations: ['import', 'global-builtin', 'color-functions', 'if-function'],
|
|
},
|
|
},
|
|
},
|
|
publicDir: 'public',
|
|
})
|