-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathvite.config.ts
More file actions
208 lines (202 loc) · 7.78 KB
/
vite.config.ts
File metadata and controls
208 lines (202 loc) · 7.78 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import path from 'node:path'
import process from 'node:process'
import Uni from '@uni-helper/plugin-uni'
import UniComponents from '@uni-helper/vite-plugin-uni-components'
// @see https://uni-helper.js.org/vite-plugin-uni-layouts
import UniLayouts from '@uni-helper/vite-plugin-uni-layouts'
// @see https://github.com/uni-helper/vite-plugin-uni-manifest
import UniManifest from '@uni-helper/vite-plugin-uni-manifest'
// @see https://uni-helper.js.org/vite-plugin-uni-pages
import UniPages from '@uni-helper/vite-plugin-uni-pages'
// @see https://github.com/uni-helper/vite-plugin-uni-platform
// 需要与 @uni-helper/vite-plugin-uni-pages 插件一起使用
import UniPlatform from '@uni-helper/vite-plugin-uni-platform'
/**
* 分包优化、模块异步跨包调用、组件异步跨包引用
* @see https://github.com/uni-ku/bundle-optimizer
*/
import UniOptimization from '@uni-ku/bundle-optimizer'
// https://github.com/uni-ku/root
import UniKuRoot from '@uni-ku/root'
import dayjs from 'dayjs'
import { visualizer } from 'rollup-plugin-visualizer'
import UnoCSS from 'unocss/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { defineConfig, loadEnv } from 'vite'
import ViteRestart from 'vite-plugin-restart'
import openDevTools from './scripts/open-dev-tools'
import vitePluginEruda from './scripts/vite-plugin-eruda'
import { createCopyNativeResourcesPlugin } from './vite-plugins/copy-native-resources'
import syncManifestPlugin from './vite-plugins/sync-manifest-plugins'
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
// @see https://unocss.dev/
// const UnoCSS = (await import('unocss/vite')).default
// console.log(mode === process.env.NODE_ENV) // true
// mode: 区分生产环境还是开发环境
console.log('command, mode -> ', command, mode)
// pnpm dev:h5 时得到 => serve development
// pnpm build:h5 时得到 => build production
// pnpm dev:mp-weixin 时得到 => build development (注意区别,command为build)
// pnpm build:mp-weixin 时得到 => build production
// pnpm dev:app 时得到 => build development (注意区别,command为build)
// pnpm build:app 时得到 => build production
// dev 和 build 命令可以分别使用 .env.development 和 .env.production 的环境变量
const { UNI_PLATFORM, SKIP_OPEN_DEVTOOLS } = 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_APP_TITLE,
VITE_DELETE_CONSOLE,
VITE_APP_PUBLIC_BASE,
VITE_APP_PROXY_ENABLE,
VITE_APP_PROXY_PREFIX,
VITE_COPY_NATIVE_RES_ENABLE,
} = env
console.log('环境变量 env -> ', env)
return defineConfig({
envDir: './env', // 自定义env目录
base: VITE_APP_PUBLIC_BASE,
plugins: [
// UniXXX 需要在 Uni 之前引入
UniLayouts(),
UniPlatform(),
UniManifest(),
UniComponents({
extensions: ['vue'],
deep: true, // 是否递归扫描子目录,
directoryAsNamespace: false, // 是否把目录名作为命名空间前缀,true 时组件名为 目录名+组件名,
dts: 'src/types/components.d.ts', // 自动生成的组件类型声明文件路径(用于 TypeScript 支持)
}),
UniPages({
exclude: ['**/components/**/**.*', '**/sections/**/**.*'],
// pages 目录为 src/pages,分包目录不能配置在pages目录下!!
// 是个数组,可以配置多个,但是不能为pages里面的目录!!
// "src/pages-demo" 是unibest demo 预留的,方便后续插入demo示例
subPackages: ['src/pages-demo'],
dts: 'src/types/uni-pages.d.ts',
}),
// UniOptimization 插件需要 page.json 文件,故应在 UniPages 插件之后执行
UniOptimization({
enable: {
'optimization': true,
'async-import': true,
'async-component': true,
},
dts: {
base: 'src/types',
},
logger: false,
}),
// 若存在改变 pages.json 的插件,请将 UniKuRoot 放置其后
UniKuRoot({
excludePages: ['**/components/**/**.*', '**/sections/**/**.*'],
}),
Uni(),
{
// 临时解决 dcloudio 官方的 @dcloudio/uni-mp-compiler 出现的编译 BUG
// 参考 github issue: https://github.com/dcloudio/uni-app/issues/4952
// 自定义插件禁用 vite:vue 插件的 devToolsEnabled,强制编译 vue 模板时 inline 为 true
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
vueTemplate: true, // default false
}),
ViteRestart({
// 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
restart: ['vite.config.js'],
}),
// h5环境增加 BUILD_TIME 和 BUILD_BRANCH
UNI_PLATFORM === 'h5' && {
name: 'html-transform',
transformIndexHtml(html) {
return html
.replace('%BUILD_TIME%', dayjs().format('YYYY-MM-DD HH:mm:ss'))
.replace('%VITE_APP_TITLE%', VITE_APP_TITLE)
},
},
// 打包分析插件,h5 + 生产环境才弹出
UNI_PLATFORM === 'h5'
&& mode === 'production'
&& visualizer({
filename: './node_modules/.cache/visualizer/stats.html',
open: true,
gzipSize: true,
brotliSize: true,
}),
// 原生插件资源复制插件 - 仅在 app 平台且启用时生效
createCopyNativeResourcesPlugin(
UNI_PLATFORM === 'app' && VITE_COPY_NATIVE_RES_ENABLE === 'true',
{
verbose: mode === 'development', // 开发模式显示详细日志
},
),
syncManifestPlugin(),
vitePluginEruda({
open: UNI_PLATFORM === 'h5' && mode === 'development',
}),
// 自动打开开发者工具插件 (必须修改 .env 文件中的 VITE_WX_APPID)
// 上传时通过 SKIP_OPEN_DEVTOOLS=true 跳过
SKIP_OPEN_DEVTOOLS !== 'true' && openDevTools({ mode }),
],
define: {
__VITE_APP_PROXY__: JSON.stringify(VITE_APP_PROXY_ENABLE),
},
css: {
postcss: {
plugins: [
// autoprefixer({
// // 指定目标浏览器
// overrideBrowserslist: ['> 1%', 'last 2 versions'],
// }),
],
},
},
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_ENABLE)
? {
[VITE_APP_PROXY_PREFIX]: {
target: VITE_SERVER_BASEURL,
changeOrigin: true,
// 后端有/api前缀则不做处理,没有则需要去掉
rewrite: path =>
path.replace(new RegExp(`^${VITE_APP_PROXY_PREFIX}`), ''),
},
}
: undefined,
},
esbuild: {
drop: VITE_DELETE_CONSOLE === 'true' ? ['console', 'debugger'] : [],
},
build: {
sourcemap: false,
// 方便非h5端调试
// sourcemap: VITE_SHOW_SOURCEMAP === 'true', // 默认是false
target: 'es6',
// 开发环境不用压缩
minify: mode === 'development' ? false : 'esbuild',
},
})
})