63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import path from "path"
|
||
|
||
import { defineConfig, loadEnv } from "vite"
|
||
import Unocss from "unocss/vite"
|
||
|
||
import uni from "@dcloudio/vite-plugin-uni"
|
||
|
||
import { useRouter } from "./src/sheep/plugs/use-router/vite-router-plug"
|
||
|
||
// https://vitejs.dev/config/
|
||
export default defineConfig(({ mode }) => {
|
||
const fileEnv = loadEnv(mode, process.cwd(), '')
|
||
if (fileEnv.VITE_FILE_ACCESS_APP_ID !== 'mini' || !/^[a-zA-Z0-9_-]{1,32}$/.test(fileEnv.VITE_FILE_ACCESS_KEY_ID || '')
|
||
|| !/^[A-Za-z0-9+/]+={0,2}$/.test(fileEnv.VITE_FILE_ACCESS_KEY || '')
|
||
|| Buffer.from(fileEnv.VITE_FILE_ACCESS_KEY || '', 'base64').length < 32) {
|
||
throw new Error('请配置文件访问 appId、kid 和至少 32 字节的 Base64 密钥(.env.<mode>.local)')
|
||
}
|
||
|
||
const { NODE_ENV, VITE_APP_DEV_SERVICE, VITE_APP_PROD_SERVICE, VITE_IMAGE_DEV_PATH } = loadEnv(mode, process.cwd(), "")
|
||
const baseUrl = NODE_ENV == "development" ? VITE_APP_DEV_SERVICE : VITE_APP_PROD_SERVICE
|
||
// 生产环境强制 HTTPS 检查
|
||
if (NODE_ENV === "production" && baseUrl && !baseUrl.startsWith("https://")) {
|
||
throw new Error("生产环境必须使用 HTTPS 协议!请检查 VITE_APP_PROD_SERVICE 配置")
|
||
}
|
||
return {
|
||
build: {
|
||
target: "es6",
|
||
},
|
||
plugins: [
|
||
uni(),
|
||
Unocss(),
|
||
{
|
||
...useRouter({
|
||
pagesJsonDir: path.resolve(__dirname, "./src/pages.json"),
|
||
includes: ["meta"],
|
||
}),
|
||
enforce: "post",
|
||
},
|
||
],
|
||
resolve: {
|
||
alias: {
|
||
"@": path.resolve(__dirname, "./src"),
|
||
"@@": path.resolve(__dirname, "./src/sheep"),
|
||
},
|
||
},
|
||
server: {
|
||
host: "0.0.0.0",
|
||
port: 8889,
|
||
hmr: true,
|
||
proxy: {
|
||
"/api": {
|
||
target: baseUrl,
|
||
ws: true,
|
||
changeOrigin: true,
|
||
rewrite: (path) => path.replace(/^\/api/, ""),
|
||
// 生产环境强制 HTTPS 验证
|
||
...(NODE_ENV === "production" && { secure: true }),
|
||
},
|
||
},
|
||
},
|
||
}
|
||
})
|