vite.config.ts 6.2 KB

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