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'
|
2023-12-23 10:43:49 +08:00
|
|
|
|
import Uni from '@dcloudio/vite-plugin-uni'
|
2024-01-04 09:53:17 +08:00
|
|
|
|
// @see https://uni-helper.js.org/vite-plugin-uni-pages
|
|
|
|
|
import UniPages from '@uni-helper/vite-plugin-uni-pages'
|
2024-01-12 17:12:31 +08:00
|
|
|
|
// @see https://uni-helper.js.org/vite-plugin-uni-layouts
|
|
|
|
|
import UniLayouts from '@uni-helper/vite-plugin-uni-layouts'
|
2024-01-04 14:35:01 +08:00
|
|
|
|
// @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'
|
2024-01-15 09:18:58 +08:00
|
|
|
|
// @see https://github.com/uni-helper/vite-plugin-uni-manifest
|
|
|
|
|
import UniManifest from '@uni-helper/vite-plugin-uni-manifest'
|
2024-02-24 18:03:16 +08:00
|
|
|
|
// @see https://github.com/uni-helper/vite-plugin-uni-components
|
|
|
|
|
import Components from '@uni-helper/vite-plugin-uni-components'
|
2024-01-15 10:35:13 +08:00
|
|
|
|
// @see https://unocss.dev/
|
|
|
|
|
import UnoCSS from 'unocss/vite'
|
2024-01-31 18:50:58 +08:00
|
|
|
|
// import autoprefixer from 'autoprefixer'
|
2024-01-15 10:35:13 +08:00
|
|
|
|
// @see https://github.com/jpkleemans/vite-svg-loader
|
2023-12-21 17:30:49 +08:00
|
|
|
|
import svgLoader from 'vite-svg-loader'
|
|
|
|
|
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
|
2024-01-15 10:35:13 +08:00
|
|
|
|
// @see https://github.com/vbenjs/vite-plugin-vue-setup-extend
|
|
|
|
|
import vueSetupExtend from 'vite-plugin-vue-setup-extend'
|
|
|
|
|
// @see https://github.com/vbenjs/vite-plugin-svg-icons
|
|
|
|
|
import AutoImport from 'unplugin-auto-import/vite'
|
2024-02-02 15:15:21 +08:00
|
|
|
|
// import viteCompression from 'vite-plugin-compression'
|
2024-01-15 10:35:13 +08:00
|
|
|
|
import ViteRestart from 'vite-plugin-restart'
|
|
|
|
|
import { visualizer } from 'rollup-plugin-visualizer'
|
2024-02-19 16:40:19 +08:00
|
|
|
|
import imagemin from './vite-plugins/imagemin'
|
2023-12-21 17:30:49 +08:00
|
|
|
|
|
2024-03-26 10:48:45 +08:00
|
|
|
|
console.log('process.platform -> ', process.platform)
|
|
|
|
|
|
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-01-03 11:16:57 +08:00
|
|
|
|
console.log(command, mode)
|
|
|
|
|
// 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
|
|
|
|
|
|
2023-12-21 17:30:49 +08:00
|
|
|
|
// process.cwd(): 获取当前文件的目录跟地址
|
|
|
|
|
// loadEnv(): 返回当前环境env文件中额外定义的变量
|
|
|
|
|
const env = loadEnv(mode, path.resolve(process.cwd(), 'env'))
|
2024-01-20 16:23:27 +08:00
|
|
|
|
console.log(env)
|
2024-01-04 10:43:38 +08:00
|
|
|
|
console.log(process.env.UNI_PLATFORM) // 得到 mp-weixin, h5 等
|
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目录
|
2023-12-21 17:30:49 +08:00
|
|
|
|
plugins: [
|
2024-01-15 17:36:11 +08:00
|
|
|
|
UniPages({
|
2024-01-22 14:02:28 +08:00
|
|
|
|
exclude: ['**/components/**/**.*'],
|
2024-01-24 14:19:33 +08:00
|
|
|
|
routeBlockLang: 'json5', // 虽然设了默认值,但是vue文件还是要加上 lang="json5", 这样才能很好地格式化
|
2024-01-24 14:13:45 +08:00
|
|
|
|
homePage: 'pages/index/index',
|
2024-01-29 12:52:20 +08:00
|
|
|
|
subPackages: ['src/pages-sub'], // 是个数组,可以配置多个
|
2024-01-15 17:36:11 +08:00
|
|
|
|
}),
|
2024-01-12 17:12:31 +08:00
|
|
|
|
UniLayouts(),
|
2024-01-04 14:35:01 +08:00
|
|
|
|
UniPlatform(),
|
2024-01-15 09:18:58 +08:00
|
|
|
|
UniManifest(),
|
2024-03-04 21:16:36 +08:00
|
|
|
|
// 自动安装 src/components 里面的组件为全局组件,非全局组件不要放到 src/components
|
2024-02-24 18:03:16 +08:00
|
|
|
|
Components(),
|
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
|
|
|
|
createSvgIconsPlugin({
|
|
|
|
|
// 指定要缓存的文件夹
|
|
|
|
|
iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
|
|
|
|
|
// 指定symbolId格式
|
|
|
|
|
symbolId: 'icon-[dir]-[name]',
|
|
|
|
|
}),
|
|
|
|
|
vueSetupExtend(),
|
|
|
|
|
AutoImport({
|
2024-01-30 09:47:20 +08:00
|
|
|
|
imports: ['vue', 'uni-app'],
|
|
|
|
|
dts: 'src/auto-import.d.ts',
|
2024-03-04 21:25:33 +08:00
|
|
|
|
// dirs: ['src/hooks'], // 自动导入 hooks
|
2024-03-04 21:16:36 +08:00
|
|
|
|
eslintrc: { enabled: false },
|
2024-01-15 10:35:13 +08:00
|
|
|
|
}),
|
|
|
|
|
|
2024-02-02 15:15:21 +08:00
|
|
|
|
// viteCompression(),
|
2024-01-15 10:35:13 +08:00
|
|
|
|
ViteRestart({
|
|
|
|
|
// 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
|
|
|
|
|
restart: ['vite.config.js'],
|
|
|
|
|
}),
|
2024-01-15 11:17:55 +08:00
|
|
|
|
// h5环境增加编译时间
|
|
|
|
|
process.env.UNI_PLATFORM === 'h5' && {
|
|
|
|
|
name: 'html-transform',
|
|
|
|
|
transformIndexHtml(html) {
|
|
|
|
|
return html.replace('%BUILD_DATE%', dayjs().format('YYYY-MM-DD HH:mm:ss'))
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-12-21 17:30:49 +08:00
|
|
|
|
// 打包分析插件
|
2024-01-03 11:16:57 +08:00
|
|
|
|
mode === 'production' &&
|
|
|
|
|
visualizer({
|
|
|
|
|
filename: './node_modules/.cache/visualizer/stats.html',
|
|
|
|
|
open: true,
|
|
|
|
|
gzipSize: true,
|
|
|
|
|
brotliSize: true,
|
|
|
|
|
}),
|
2023-12-21 17:30:49 +08:00
|
|
|
|
// 这个图片压缩插件比较耗时,希望仅在生产环境使用
|
2024-02-19 16:40:19 +08:00
|
|
|
|
// TODO: 缓存每次压缩过的图片,已经压缩过的不再压缩
|
|
|
|
|
imagemin(mode === 'production'),
|
2023-12-21 17:30:49 +08:00
|
|
|
|
],
|
2024-01-29 12:58:59 +08:00
|
|
|
|
|
2023-12-21 17:30:49 +08:00
|
|
|
|
css: {
|
|
|
|
|
postcss: {
|
|
|
|
|
plugins: [
|
2024-01-31 18:50:58 +08:00
|
|
|
|
// autoprefixer({
|
|
|
|
|
// // 指定目标浏览器
|
|
|
|
|
// overrideBrowserslist: ['> 1%', 'last 2 versions'],
|
|
|
|
|
// }),
|
2023-12-21 17:30:49 +08:00
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
resolve: {
|
|
|
|
|
alias: {
|
|
|
|
|
'@': path.join(process.cwd(), './src'),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
server: {
|
|
|
|
|
host: '0.0.0.0',
|
|
|
|
|
hmr: true,
|
2023-12-23 12:04:31 +08:00
|
|
|
|
port: Number.parseInt(env.VITE_APP_PORT, 10),
|
2023-12-21 17:30:49 +08:00
|
|
|
|
},
|
2023-12-23 12:04:31 +08:00
|
|
|
|
build: {
|
|
|
|
|
minify: 'terser',
|
|
|
|
|
terserOptions: {
|
|
|
|
|
compress: {
|
|
|
|
|
drop_console: env.VITE_DELETE_CONSOLE === 'true',
|
|
|
|
|
drop_debugger: env.VITE_DELETE_CONSOLE === 'true',
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-03-26 10:48:45 +08:00
|
|
|
|
// 解决windows系统对微信小程序自动关闭服务的问题
|
|
|
|
|
watch:
|
|
|
|
|
process.platform === 'win32' // 检测是否为 windows 系统
|
|
|
|
|
? {
|
|
|
|
|
exclude: ['node_modules/**', '/__uno.css'],
|
|
|
|
|
}
|
|
|
|
|
: null,
|
2023-12-23 12:04:31 +08:00
|
|
|
|
},
|
2023-12-21 17:30:49 +08:00
|
|
|
|
})
|
|
|
|
|
}
|