vite.config.ts 5.7 KB

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