unibest/vite.config.ts

141 lines
4.9 KiB
TypeScript
Raw Normal View History

2023-12-21 17:30:49 +08:00
import path from 'node:path'
2024-01-15 11:15:37 +08:00
import dayjs from 'dayjs'
2023-12-21 17:30:49 +08:00
import { defineConfig, loadEnv } from 'vite'
import Uni from '@dcloudio/vite-plugin-uni'
// @see https://uni-helper.js.org/vite-plugin-uni-pages
import UniPages from '@uni-helper/vite-plugin-uni-pages'
// @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-platform
// 需要与 @uni-helper/vite-plugin-uni-pages 插件一起使用
import UniPlatform from '@uni-helper/vite-plugin-uni-platform'
// @see https://github.com/uni-helper/vite-plugin-uni-manifest
import UniManifest from '@uni-helper/vite-plugin-uni-manifest'
2024-01-15 10:35:13 +08:00
// @see https://unocss.dev/
import UnoCSS from 'unocss/vite'
// @see https://github.com/jpkleemans/vite-svg-loader
2023-12-21 17:30:49 +08:00
import svgLoader from 'vite-svg-loader'
2024-01-15 10:35:13 +08:00
import AutoImport from 'unplugin-auto-import/vite'
import { visualizer } from 'rollup-plugin-visualizer'
2024-04-17 15:32:15 +08:00
import ViteRestart from 'vite-plugin-restart'
2023-12-21 15:26:15 +08:00
// https://vitejs.dev/config/
2024-01-03 11:16:57 +08:00
export default ({ command, mode }) => {
2024-01-24 10:38:16 +08:00
// console.log(mode === process.env.NODE_ENV) // true
2024-01-15 09:58:46 +08:00
2023-12-21 17:30:49 +08:00
// mode: 区分生产环境还是开发环境
2024-03-28 17:39:01 +08:00
console.log('command, mode -> ', command, mode)
2024-01-03 11:16:57 +08:00
// pnpm dev:h5 时得到 => serve development
2024-02-01 16:29:36 +08:00
// pnpm build:h5 时得到 => build production
2024-01-03 11:16:57 +08:00
// pnpm dev:mp-weixin 时得到 => build development (注意区别command为build)
// pnpm build:mp-weixin 时得到 => build production
2024-04-17 15:32:15 +08:00
// pnpm dev:app 时得到 => build development (注意区别command为build)
// pnpm build:app 时得到 => build production
// dev 和 build 命令可以分别使用 .env.development 和 .env.production 的环境变量
const { UNI_PLATFORM } = process.env
console.log('UNI_PLATFORM -> ', UNI_PLATFORM) // 得到 mp-weixin, h5, app 等
2024-01-03 11:16:57 +08:00
2023-12-21 17:30:49 +08:00
const env = loadEnv(mode, path.resolve(process.cwd(), 'env'))
2024-04-17 15:32:15 +08:00
const { VITE_APP_PORT, VITE_SERVER_BASEURL, VITE_DELETE_CONSOLE, VITE_SHOW_SOURCEMAP } = env
console.log('环境变量 env -> ', env)
2024-01-16 10:15:20 +08:00
2023-12-21 17:30:49 +08:00
return defineConfig({
2024-01-09 10:02:40 +08:00
envDir: './env', // 自定义env目录
2024-03-28 17:39:01 +08:00
2023-12-21 17:30:49 +08:00
plugins: [
UniPages({
exclude: ['**/components/**/**.*'],
routeBlockLang: 'json5', // 虽然设了默认值但是vue文件还是要加上 lang="json5", 这样才能很好地格式化
2024-04-11 18:38:15 +08:00
// homePage 通过 vue 文件的 route-block 的type="home"来设定
// pages 目录为 src/pages分包目录不能配置在pages目录下
// subPackages: ['src/pages-sub'], // 是个数组可以配置多个但是不能为pages里面的目录
2024-04-17 15:32:15 +08:00
dts: 'src/types/uni-pages.d.ts',
}),
UniLayouts(),
UniPlatform(),
UniManifest(),
2024-01-15 10:35:13 +08:00
// UniXXX 需要在 Uni 之前引入
Uni(),
2023-12-21 17:30:49 +08:00
UnoCSS(),
2024-01-15 10:35:13 +08:00
// svg 可以当做组件来使用(Vite plugin to load SVG files as Vue components, using SVGO for optimization.)
2024-03-17 12:12:04 +08:00
svgLoader({
defaultImport: 'url', // or 'raw'
}),
2024-01-15 10:35:13 +08:00
AutoImport({
2024-01-30 09:47:20 +08:00
imports: ['vue', 'uni-app'],
2024-04-17 15:32:15 +08:00
dts: 'src/types/auto-import.d.ts',
dirs: ['src/hooks'], // 自动导入 hooks
eslintrc: { enabled: true },
vueTemplate: true, // default false
2024-01-15 10:35:13 +08:00
}),
ViteRestart({
// 通过这个插件在修改vite.config.js文件则不需要重新运行也生效配置
restart: ['vite.config.js'],
}),
// h5环境增加编译时间
2024-04-17 15:32:15 +08:00
UNI_PLATFORM === 'h5' && {
name: 'html-transform',
transformIndexHtml(html) {
return html.replace('%BUILD_DATE%', dayjs().format('YYYY-MM-DD HH:mm:ss'))
},
},
2024-04-17 15:32:15 +08:00
// 打包分析插件h5 + 生产环境才弹出
UNI_PLATFORM === 'h5' &&
mode === 'production' &&
2024-01-03 11:16:57 +08:00
visualizer({
filename: './node_modules/.cache/visualizer/stats.html',
open: true,
gzipSize: true,
brotliSize: true,
}),
2023-12-21 17:30:49 +08:00
],
define: {
2024-04-17 15:32:15 +08:00
__UNI_PLATFORM__: JSON.stringify(UNI_PLATFORM),
},
2023-12-21 17:30:49 +08:00
css: {
postcss: {
plugins: [
// autoprefixer({
// // 指定目标浏览器
// overrideBrowserslist: ['> 1%', 'last 2 versions'],
// }),
2023-12-21 17:30:49 +08:00
],
},
},
resolve: {
alias: {
'@': path.join(process.cwd(), './src'),
2024-03-28 10:19:14 +08:00
'@img': path.join(process.cwd(), './src/static/images'),
2023-12-21 17:30:49 +08:00
},
},
server: {
host: '0.0.0.0',
hmr: true,
2024-04-17 15:32:15 +08:00
port: Number.parseInt(VITE_APP_PORT, 10),
2024-04-18 17:03:05 +08:00
// 仅 H5 端生效其他端不生效其他端走build不走devServer)
2024-04-17 15:32:15 +08:00
proxy: {
'/api': {
target: VITE_SERVER_BASEURL,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
},
},
2023-12-21 17:30:49 +08:00
},
2023-12-23 12:04:31 +08:00
build: {
2024-04-17 15:32:15 +08:00
// 方便非h5端调试
sourcemap: VITE_SHOW_SOURCEMAP === 'true', // 默认是false
target: 'es6',
2023-12-23 12:04:31 +08:00
minify: 'terser',
terserOptions: {
compress: {
2024-04-17 15:32:15 +08:00
drop_console: VITE_DELETE_CONSOLE === 'true',
drop_debugger: true,
2023-12-23 12:04:31 +08:00
},
},
},
2023-12-21 17:30:49 +08:00
})
}