vite.config.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // @see https://github.com/jpkleemans/vite-svg-loader
  17. import svgLoader from 'vite-svg-loader'
  18. import AutoImport from 'unplugin-auto-import/vite'
  19. import vueSetupExtend from 'vite-plugin-vue-setup-extend'
  20. import { visualizer } from 'rollup-plugin-visualizer'
  21. import ViteRestart from 'vite-plugin-restart'
  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 { VITE_APP_PORT, VITE_SERVER_BASEURL, VITE_DELETE_CONSOLE, VITE_SHOW_SOURCEMAP } = env
  38. console.log('环境变量 env -> ', env)
  39. return defineConfig({
  40. envDir: './env', // 自定义env目录
  41. plugins: [
  42. UniPages({
  43. exclude: ['**/components/**/**.*'],
  44. routeBlockLang: 'json5', // 虽然设了默认值,但是vue文件还是要加上 lang="json5", 这样才能很好地格式化
  45. // homePage 通过 vue 文件的 route-block 的type="home"来设定
  46. // pages 目录为 src/pages,分包目录不能配置在pages目录下
  47. // subPackages: ['src/pages-sub'], // 是个数组,可以配置多个,但是不能为pages里面的目录
  48. dts: 'src/types/uni-pages.d.ts',
  49. }),
  50. UniLayouts(),
  51. UniPlatform(),
  52. UniManifest(),
  53. // UniXXX 需要在 Uni 之前引入
  54. Uni(),
  55. UnoCSS(),
  56. // svg 可以当做组件来使用(Vite plugin to load SVG files as Vue components, using SVGO for optimization.)
  57. svgLoader({
  58. defaultImport: 'url', // or 'raw'
  59. }),
  60. AutoImport({
  61. imports: ['vue', 'uni-app'],
  62. dts: 'src/types/auto-import.d.ts',
  63. dirs: ['src/hooks'], // 自动导入 hooks
  64. eslintrc: { enabled: true },
  65. vueTemplate: true, // default false
  66. }),
  67. vueSetupExtend(),
  68. ViteRestart({
  69. // 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
  70. restart: ['vite.config.js'],
  71. }),
  72. // h5环境增加编译时间
  73. UNI_PLATFORM === 'h5' && {
  74. name: 'html-transform',
  75. transformIndexHtml(html) {
  76. return html.replace('%BUILD_DATE%', dayjs().format('YYYY-MM-DD HH:mm:ss'))
  77. },
  78. },
  79. // 打包分析插件,h5 + 生产环境才弹出
  80. UNI_PLATFORM === 'h5' &&
  81. mode === 'production' &&
  82. visualizer({
  83. filename: './node_modules/.cache/visualizer/stats.html',
  84. open: true,
  85. gzipSize: true,
  86. brotliSize: true,
  87. }),
  88. ],
  89. define: {
  90. __UNI_PLATFORM__: JSON.stringify(UNI_PLATFORM),
  91. },
  92. css: {
  93. postcss: {
  94. plugins: [
  95. // autoprefixer({
  96. // // 指定目标浏览器
  97. // overrideBrowserslist: ['> 1%', 'last 2 versions'],
  98. // }),
  99. ],
  100. },
  101. },
  102. resolve: {
  103. alias: {
  104. '@': path.join(process.cwd(), './src'),
  105. '@img': path.join(process.cwd(), './src/static/images'),
  106. },
  107. },
  108. server: {
  109. host: '0.0.0.0',
  110. hmr: true,
  111. port: Number.parseInt(VITE_APP_PORT, 10),
  112. // 仅 H5 端生效,其他端不生效(其他端走build,不走devServer)
  113. proxy: {
  114. '/api': {
  115. target: VITE_SERVER_BASEURL,
  116. changeOrigin: true,
  117. rewrite: (path) => path.replace(/^\/api/, ''),
  118. },
  119. },
  120. },
  121. build: {
  122. // 方便非h5端调试
  123. sourcemap: VITE_SHOW_SOURCEMAP === 'true', // 默认是false
  124. target: 'es6',
  125. minify: 'terser',
  126. terserOptions: {
  127. compress: {
  128. drop_console: VITE_DELETE_CONSOLE === 'true',
  129. drop_debugger: true,
  130. },
  131. },
  132. },
  133. })
  134. }