vite.config.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import path from 'node:path'
  2. import dayjs from 'dayjs'
  3. import { defineConfig, loadEnv } from 'vite'
  4. import Uni from '@dcloudio/vite-plugin-uni'
  5. // @see https://uni-helper.js.org/vite-plugin-uni-pages
  6. import UniPages from '@uni-helper/vite-plugin-uni-pages'
  7. // @see https://uni-helper.js.org/vite-plugin-uni-layouts
  8. import UniLayouts from '@uni-helper/vite-plugin-uni-layouts'
  9. // @see https://github.com/uni-helper/vite-plugin-uni-platform
  10. // 需要与 @uni-helper/vite-plugin-uni-pages 插件一起使用
  11. import UniPlatform from '@uni-helper/vite-plugin-uni-platform'
  12. // @see https://github.com/uni-helper/vite-plugin-uni-manifest
  13. import UniManifest from '@uni-helper/vite-plugin-uni-manifest'
  14. // @see https://unocss.dev/
  15. import UnoCSS from 'unocss/vite'
  16. import AutoImport from 'unplugin-auto-import/vite'
  17. import { visualizer } from 'rollup-plugin-visualizer'
  18. import ViteRestart from 'vite-plugin-restart'
  19. // https://vitejs.dev/config/
  20. export default ({ command, mode }) => {
  21. // console.log(mode === process.env.NODE_ENV) // true
  22. // mode: 区分生产环境还是开发环境
  23. console.log('command, mode -> ', command, mode)
  24. // pnpm dev:h5 时得到 => serve development
  25. // pnpm build:h5 时得到 => build production
  26. // pnpm dev:mp-weixin 时得到 => build development (注意区别,command为build)
  27. // pnpm build:mp-weixin 时得到 => build production
  28. // pnpm dev:app 时得到 => build development (注意区别,command为build)
  29. // pnpm build:app 时得到 => build production
  30. // dev 和 build 命令可以分别使用 .env.development 和 .env.production 的环境变量
  31. const { UNI_PLATFORM } = process.env
  32. console.log('UNI_PLATFORM -> ', UNI_PLATFORM) // 得到 mp-weixin, h5, app 等
  33. const env = loadEnv(mode, path.resolve(process.cwd(), 'env'))
  34. const {
  35. VITE_APP_PORT,
  36. VITE_SERVER_BASEURL,
  37. VITE_DELETE_CONSOLE,
  38. VITE_SHOW_SOURCEMAP,
  39. VITE_APP_PROXY,
  40. VITE_APP_PROXY_PREFIX,
  41. } = env
  42. console.log('环境变量 env -> ', env)
  43. return defineConfig({
  44. envDir: './env', // 自定义env目录
  45. plugins: [
  46. UniPages({
  47. exclude: ['**/components/**/**.*'],
  48. routeBlockLang: 'json5', // 虽然设了默认值,但是vue文件还是要加上 lang="json5", 这样才能很好地格式化
  49. // homePage 通过 vue 文件的 route-block 的type="home"来设定
  50. // pages 目录为 src/pages,分包目录不能配置在pages目录下
  51. // subPackages: ['src/pages-sub'], // 是个数组,可以配置多个,但是不能为pages里面的目录
  52. dts: 'src/types/uni-pages.d.ts',
  53. }),
  54. UniLayouts(),
  55. UniPlatform(),
  56. UniManifest(),
  57. // UniXXX 需要在 Uni 之前引入
  58. Uni(),
  59. {
  60. // 临时解决 dcloudio 官方的 @dcloudio/uni-mp-compiler 出现的编译 BUG
  61. // 参考 github issue: https://github.com/dcloudio/uni-app/issues/4952
  62. // 自定义插件禁用 vite:vue 插件的 devToolsEnabled,强制编译 vue 模板时 inline 为 true
  63. name: 'fix-vite-plugin-vue',
  64. configResolved(config) {
  65. const plugin = config.plugins.find((p) => p.name === 'vite:vue')
  66. if (plugin && plugin.api && plugin.api.options) {
  67. plugin.api.options.devToolsEnabled = false
  68. }
  69. },
  70. },
  71. UnoCSS(),
  72. AutoImport({
  73. imports: ['vue', 'uni-app'],
  74. dts: 'src/types/auto-import.d.ts',
  75. dirs: ['src/hooks'], // 自动导入 hooks
  76. eslintrc: { enabled: true },
  77. vueTemplate: true, // default false
  78. }),
  79. ViteRestart({
  80. // 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
  81. restart: ['vite.config.js'],
  82. }),
  83. // h5环境增加编译时间
  84. UNI_PLATFORM === 'h5' && {
  85. name: 'html-transform',
  86. transformIndexHtml(html) {
  87. return html.replace('%BUILD_DATE%', dayjs().format('YYYY-MM-DD HH:mm:ss'))
  88. },
  89. },
  90. // 打包分析插件,h5 + 生产环境才弹出
  91. UNI_PLATFORM === 'h5' &&
  92. mode === 'production' &&
  93. visualizer({
  94. filename: './node_modules/.cache/visualizer/stats.html',
  95. open: true,
  96. gzipSize: true,
  97. brotliSize: true,
  98. }),
  99. ],
  100. define: {
  101. __UNI_PLATFORM__: JSON.stringify(UNI_PLATFORM),
  102. __VITE_APP_PROXY__: JSON.stringify(VITE_APP_PROXY),
  103. },
  104. css: {
  105. postcss: {
  106. plugins: [
  107. // autoprefixer({
  108. // // 指定目标浏览器
  109. // overrideBrowserslist: ['> 1%', 'last 2 versions'],
  110. // }),
  111. ],
  112. },
  113. },
  114. resolve: {
  115. alias: {
  116. '@': path.join(process.cwd(), './src'),
  117. '@img': path.join(process.cwd(), './src/static/images'),
  118. },
  119. },
  120. server: {
  121. host: '0.0.0.0',
  122. hmr: true,
  123. port: Number.parseInt(VITE_APP_PORT, 10),
  124. // 仅 H5 端生效,其他端不生效(其他端走build,不走devServer)
  125. proxy: JSON.parse(VITE_APP_PROXY)
  126. ? {
  127. [VITE_APP_PROXY_PREFIX]: {
  128. target: VITE_SERVER_BASEURL,
  129. changeOrigin: true,
  130. rewrite: (path) => path.replace(new RegExp(`^${VITE_APP_PROXY_PREFIX}`), ''),
  131. },
  132. }
  133. : undefined,
  134. },
  135. build: {
  136. // 方便非h5端调试
  137. sourcemap: VITE_SHOW_SOURCEMAP === 'true', // 默认是false
  138. target: 'es6',
  139. // 开发环境不用压缩
  140. minify: mode === 'development' ? false : 'terser',
  141. terserOptions: {
  142. compress: {
  143. drop_console: VITE_DELETE_CONSOLE === 'true',
  144. drop_debugger: true,
  145. },
  146. },
  147. },
  148. })
  149. }