-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathvite.config.ts
121 lines (115 loc) · 3.59 KB
/
vite.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import path from "node:path"
import dayjs from "dayjs"
import { defineConfig, loadEnv } from "vite"
import Uni from "@dcloudio/vite-plugin-uni"
import UniLayouts from "@uni-helper/vite-plugin-uni-layouts"
import UniPlatform from "@uni-helper/vite-plugin-uni-platform"
import UniManifest from "@uni-helper/vite-plugin-uni-manifest"
import UnoCSS from "unocss/vite"
import AutoImport from "unplugin-auto-import/vite"
import { visualizer } from "rollup-plugin-visualizer"
import ViteRestart from "vite-plugin-restart"
export default ({ command, mode }) => {
console.log("command, mode -> ", command, mode)
const { UNI_PLATFORM } = process.env
console.log("UNI_PLATFORM -> ", UNI_PLATFORM) // 得到 mp-weixin, h5, app 等
const env = loadEnv(mode, path.resolve(process.cwd(), "env"))
const {
VITE_APP_PORT,
VITE_SERVER_BASEURL,
VITE_DELETE_CONSOLE,
VITE_SHOW_SOURCEMAP,
VITE_APP_PROXY,
VITE_APP_PROXY_PREFIX,
} = env
console.log("环境变量 env -> ", env)
return defineConfig({
envDir: "./env", // 自定义env目录
plugins: [
UniLayouts(),
UniPlatform(),
UniManifest(),
Uni(),
{
name: "fix-vite-plugin-vue",
configResolved(config) {
const plugin = config.plugins.find((p) => p.name === "vite:vue")
if (plugin && plugin.api && plugin.api.options) {
plugin.api.options.devToolsEnabled = false
}
},
},
UnoCSS(),
AutoImport({
imports: ["vue", "uni-app"],
dts: "src/types/auto-import.d.ts",
dirs: ["src/hooks"], // 自动导入 hooks
eslintrc: { enabled: true },
vueTemplate: true, // default false
}),
ViteRestart({
// 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
restart: ["vite.config.js"],
}),
// h5环境增加编译时间
UNI_PLATFORM === "h5" && {
name: "html-transform",
transformIndexHtml(html) {
return html.replace("%BUILD_DATE%", dayjs().format("YYYY-MM-DD HH:mm:ss"))
},
},
// 打包分析插件,h5 + 生产环境才弹出
UNI_PLATFORM === "h5" &&
mode === "production" &&
visualizer({
filename: "./node_modules/.cache/visualizer/stats.html",
open: true,
gzipSize: true,
brotliSize: true,
}),
],
define: {
__UNI_PLATFORM__: JSON.stringify(UNI_PLATFORM),
__VITE_APP_PROXY__: JSON.stringify(VITE_APP_PROXY),
},
css: {
postcss: {
plugins: [],
},
},
resolve: {
alias: {
"@": path.join(process.cwd(), "./src"),
"@img": path.join(process.cwd(), "./src/static/images"),
},
},
server: {
host: "0.0.0.0",
hmr: true,
port: Number.parseInt(VITE_APP_PORT, 10),
// 仅 H5 端生效,其他端不生效(其他端走build,不走devServer)
proxy: JSON.parse(VITE_APP_PROXY)
? {
[VITE_APP_PROXY_PREFIX]: {
target: VITE_SERVER_BASEURL,
changeOrigin: true,
rewrite: (path) => path.replace(new RegExp(`^${VITE_APP_PROXY_PREFIX}`), ""),
},
}
: undefined,
},
build: {
// 方便非h5端调试
sourcemap: VITE_SHOW_SOURCEMAP === "true", // 默认是false
target: "es6",
// 开发环境不用压缩
minify: mode === "development" ? false : "terser",
terserOptions: {
compress: {
drop_console: VITE_DELETE_CONSOLE === "true",
drop_debugger: true,
},
},
},
})
}