75 lines
2.0 KiB
TypeScript
75 lines
2.0 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"
|
|
|
|
const fixIifeCodeSplitPlugin = () => ({
|
|
name: "fix-uni-iife-code-splitting",
|
|
apply: "build" as const,
|
|
enforce: "post" as const,
|
|
outputOptions(outputOptions: Record<string, any>) {
|
|
if (outputOptions.format === "iife" || outputOptions.format === "umd") {
|
|
delete outputOptions.manualChunks
|
|
outputOptions.inlineDynamicImports = true
|
|
}
|
|
return outputOptions
|
|
},
|
|
})
|
|
|
|
// https://vitejs.dev/config/
|
|
export default defineConfig(({ mode }) => {
|
|
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 {
|
|
// H5 部署子路径只在 manifest.json → h5.router.base 配置;
|
|
// 勿在此处写死 /h5/,否则 App(iOS/Android) 打包也会带上错误前缀。
|
|
build: {
|
|
target: "es6",
|
|
sourcemap: false,
|
|
cssCodeSplit: false,
|
|
reportCompressedSize: false,
|
|
},
|
|
plugins: [
|
|
uni(),
|
|
Unocss(),
|
|
fixIifeCodeSplitPlugin(),
|
|
{
|
|
...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 }),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
})
|