vite.config.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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://github.com/uni-helper/vite-plugin-uni-components
  15. import Components from '@uni-helper/vite-plugin-uni-components'
  16. // @see https://unocss.dev/
  17. import UnoCSS from 'unocss/vite'
  18. // import autoprefixer from 'autoprefixer'
  19. // @see https://github.com/jpkleemans/vite-svg-loader
  20. import svgLoader from 'vite-svg-loader'
  21. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  22. // @see https://github.com/vbenjs/vite-plugin-vue-setup-extend
  23. import vueSetupExtend from 'vite-plugin-vue-setup-extend'
  24. // @see https://github.com/vbenjs/vite-plugin-svg-icons
  25. import AutoImport from 'unplugin-auto-import/vite'
  26. // import viteCompression from 'vite-plugin-compression'
  27. import ViteRestart from 'vite-plugin-restart'
  28. import { visualizer } from 'rollup-plugin-visualizer'
  29. // import imagemin from './vite-plugins/imagemin'
  30. console.log('process.platform -> ', process.platform)
  31. // https://vitejs.dev/config/
  32. export default ({ command, mode }) => {
  33. // console.log(mode === process.env.NODE_ENV) // true
  34. // mode: 区分生产环境还是开发环境
  35. console.log('command, mode -> ', command, mode)
  36. // pnpm dev:h5 时得到 => serve development
  37. // pnpm build:h5 时得到 => build production
  38. // pnpm dev:mp-weixin 时得到 => build development (注意区别,command为build)
  39. // pnpm build:mp-weixin 时得到 => build production
  40. // process.cwd(): 获取当前文件的目录跟地址
  41. // loadEnv(): 返回当前环境env文件中额外定义的变量
  42. const env = loadEnv(mode, path.resolve(process.cwd(), 'env'))
  43. console.log('env -> ', env)
  44. console.log('process.env.UNI_PLATFORM: ', process.env.UNI_PLATFORM) // 得到 mp-weixin, h5, app 等
  45. console.log('isH5: ', process.env.UNI_PLATFORM === 'h5') // 得到 mp-weixin, h5, app 等
  46. return defineConfig({
  47. envDir: './env', // 自定义env目录
  48. plugins: [
  49. UniPages({
  50. exclude: ['**/components/**/**.*'],
  51. routeBlockLang: 'json5', // 虽然设了默认值,但是vue文件还是要加上 lang="json5", 这样才能很好地格式化
  52. // homePage 通过 vue 文件的 route-block 的type="home"来设定
  53. // pages 目录为 src/pages,分包目录不能配置在pages目录下
  54. // subPackages: ['src/pages-sub'], // 是个数组,可以配置多个,但是不能为pages里面的目录
  55. }),
  56. UniLayouts(),
  57. UniPlatform(),
  58. UniManifest(),
  59. // 自动安装 src/components 里面的组件为全局组件,非全局组件不要放到 src/components
  60. Components(),
  61. // UniXXX 需要在 Uni 之前引入
  62. Uni(),
  63. UnoCSS(),
  64. // svg 可以当做组件来使用(Vite plugin to load SVG files as Vue components, using SVGO for optimization.)
  65. svgLoader({
  66. defaultImport: 'url', // or 'raw'
  67. }),
  68. createSvgIconsPlugin({
  69. // 指定要缓存的文件夹
  70. iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
  71. // 指定symbolId格式
  72. symbolId: 'icon-[dir]-[name]',
  73. }),
  74. vueSetupExtend(),
  75. AutoImport({
  76. imports: ['vue', 'uni-app'],
  77. dts: 'src/auto-import.d.ts',
  78. // dirs: ['src/hooks'], // 自动导入 hooks
  79. eslintrc: { enabled: false },
  80. vueTemplate: true, // default false
  81. }),
  82. // viteCompression(),
  83. ViteRestart({
  84. // 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
  85. restart: ['vite.config.js'],
  86. }),
  87. // h5环境增加编译时间
  88. process.env.UNI_PLATFORM === 'h5' && {
  89. name: 'html-transform',
  90. transformIndexHtml(html) {
  91. return html.replace('%BUILD_DATE%', dayjs().format('YYYY-MM-DD HH:mm:ss'))
  92. },
  93. },
  94. // 打包分析插件
  95. mode === 'production' &&
  96. visualizer({
  97. filename: './node_modules/.cache/visualizer/stats.html',
  98. open: true,
  99. gzipSize: true,
  100. brotliSize: true,
  101. }),
  102. // 这个图片压缩插件比较耗时,希望仅在生产环境使用
  103. // TODO: 缓存每次压缩过的图片,已经压缩过的不再压缩
  104. // imagemin(mode === 'production'),
  105. ],
  106. define: {
  107. __UNI_PLATFORM__: JSON.stringify(process.env.UNI_PLATFORM),
  108. },
  109. css: {
  110. postcss: {
  111. plugins: [
  112. // autoprefixer({
  113. // // 指定目标浏览器
  114. // overrideBrowserslist: ['> 1%', 'last 2 versions'],
  115. // }),
  116. ],
  117. },
  118. },
  119. resolve: {
  120. alias: {
  121. '@': path.join(process.cwd(), './src'),
  122. '@img': path.join(process.cwd(), './src/static/images'),
  123. },
  124. },
  125. server: {
  126. host: '0.0.0.0',
  127. hmr: true,
  128. port: Number.parseInt(env.VITE_APP_PORT, 10),
  129. },
  130. build: {
  131. minify: 'terser',
  132. terserOptions: {
  133. compress: {
  134. drop_console: env.VITE_DELETE_CONSOLE === 'true',
  135. drop_debugger: env.VITE_DELETE_CONSOLE === 'true',
  136. },
  137. },
  138. // 解决windows系统对微信小程序自动关闭服务的问题
  139. watch:
  140. process.platform === 'win32' // 检测是否为 windows 系统
  141. ? {
  142. exclude: ['node_modules/**', '/__uno.css'],
  143. }
  144. : null,
  145. },
  146. })
  147. }