vite.config.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import path from 'node:path'
  2. import process from 'node:process'
  3. import Uni from '@uni-helper/plugin-uni'
  4. import Components from '@uni-helper/vite-plugin-uni-components'
  5. import { WotResolver } from '@uni-helper/vite-plugin-uni-components/resolvers'
  6. // @see https://uni-helper.js.org/vite-plugin-uni-layouts
  7. import UniLayouts from '@uni-helper/vite-plugin-uni-layouts'
  8. // @see https://github.com/uni-helper/vite-plugin-uni-manifest
  9. import UniManifest from '@uni-helper/vite-plugin-uni-manifest'
  10. // @see https://uni-helper.js.org/vite-plugin-uni-pages
  11. import UniPages from '@uni-helper/vite-plugin-uni-pages'
  12. // @see https://github.com/uni-helper/vite-plugin-uni-platform
  13. // 需要与 @uni-helper/vite-plugin-uni-pages 插件一起使用
  14. import UniPlatform from '@uni-helper/vite-plugin-uni-platform'
  15. /**
  16. * 分包优化、模块异步跨包调用、组件异步跨包引用
  17. * @see https://github.com/uni-ku/bundle-optimizer
  18. */
  19. import Optimization from '@uni-ku/bundle-optimizer'
  20. // https://github.com/uni-ku/root
  21. import UniKuRoot from '@uni-ku/root'
  22. import dayjs from 'dayjs'
  23. import { visualizer } from 'rollup-plugin-visualizer'
  24. import UnoCSS from 'unocss/vite'
  25. import AutoImport from 'unplugin-auto-import/vite'
  26. import { defineConfig, loadEnv } from 'vite'
  27. import ViteRestart from 'vite-plugin-restart'
  28. import openDevTools from './scripts/open-dev-tools'
  29. import { createCopyNativeResourcesPlugin } from './vite-plugins/copy-native-resources'
  30. import syncManifestPlugin from './vite-plugins/sync-manifest-plugins'
  31. // https://vitejs.dev/config/
  32. export default defineConfig(({ command, mode }) => {
  33. // @see https://unocss.dev/
  34. // const UnoCSS = (await import('unocss/vite')).default
  35. // console.log(mode === process.env.NODE_ENV) // true
  36. // mode: 区分生产环境还是开发环境
  37. console.log('command, mode -> ', command, mode)
  38. // pnpm dev:h5 时得到 => serve development
  39. // pnpm build:h5 时得到 => build production
  40. // pnpm dev:mp-weixin 时得到 => build development (注意区别,command为build)
  41. // pnpm build:mp-weixin 时得到 => build production
  42. // pnpm dev:app 时得到 => build development (注意区别,command为build)
  43. // pnpm build:app 时得到 => build production
  44. // dev 和 build 命令可以分别使用 .env.development 和 .env.production 的环境变量
  45. const { UNI_PLATFORM } = process.env
  46. console.log('UNI_PLATFORM -> ', UNI_PLATFORM) // 得到 mp-weixin, h5, app 等
  47. const env = loadEnv(mode, path.resolve(process.cwd(), 'env'))
  48. const {
  49. VITE_APP_PORT,
  50. VITE_SERVER_BASEURL,
  51. VITE_APP_TITLE,
  52. VITE_DELETE_CONSOLE,
  53. VITE_APP_PUBLIC_BASE,
  54. VITE_APP_PROXY_ENABLE,
  55. VITE_APP_PROXY_PREFIX,
  56. VITE_COPY_NATIVE_RES_ENABLE,
  57. } = env
  58. console.log('环境变量 env -> ', env)
  59. return defineConfig({
  60. envDir: './env', // 自定义env目录
  61. base: VITE_APP_PUBLIC_BASE,
  62. plugins: [
  63. UniLayouts(),
  64. UniPlatform(),
  65. UniManifest(),
  66. UniPages({
  67. exclude: ['**/components/**/**.*'],
  68. // pages 目录为 src/pages,分包目录不能配置在pages目录下!!
  69. // 是个数组,可以配置多个,但是不能为pages里面的目录!!
  70. subPackages: [
  71. 'src/pages-core', // 这个是相对必要的路由,尽量留着(登录页、注册页、404页等)
  72. 'src/pages-system', // “系统管理”模块
  73. 'src/pages-infra', // “基础设施”模块
  74. 'src/pages-bpm', // “工作流程”模块
  75. 'src/pages-crm', // “客户关系管理”模块
  76. ],
  77. dts: 'src/types/uni-pages.d.ts',
  78. }),
  79. // Optimization 插件需要 page.json 文件,故应在 UniPages 插件之后执行
  80. Optimization({
  81. enable: {
  82. 'optimization': true,
  83. 'async-import': true,
  84. 'async-component': true,
  85. },
  86. dts: {
  87. base: 'src/types',
  88. },
  89. logger: false,
  90. }),
  91. // UniXXX 需要在 Uni 之前引入
  92. // 若存在改变 pages.json 的插件,请将 UniKuRoot 放置其后
  93. UniKuRoot({
  94. excludePages: ['**/components/**/**.*'],
  95. }),
  96. // Components 需要在 Uni 之前引入
  97. Components({
  98. extensions: ['vue'],
  99. deep: true, // 是否递归扫描子目录,
  100. directoryAsNamespace: false, // 是否把目录名作为命名空间前缀,true 时组件名为 目录名+组件名,
  101. dts: 'src/types/components.d.ts', // 自动生成的组件类型声明文件路径(用于 TypeScript 支持)
  102. }),
  103. Uni(),
  104. {
  105. // 临时解决 dcloudio 官方的 @dcloudio/uni-mp-compiler 出现的编译 BUG
  106. // 参考 github issue: https://github.com/dcloudio/uni-app/issues/4952
  107. // 自定义插件禁用 vite:vue 插件的 devToolsEnabled,强制编译 vue 模板时 inline 为 true
  108. name: 'fix-vite-plugin-vue',
  109. configResolved(config) {
  110. const plugin = config.plugins.find(p => p.name === 'vite:vue')
  111. if (plugin && plugin.api && plugin.api.options) {
  112. plugin.api.options.devToolsEnabled = false
  113. }
  114. },
  115. },
  116. UnoCSS(),
  117. AutoImport({
  118. imports: ['vue', 'uni-app'],
  119. dts: 'src/types/auto-import.d.ts',
  120. dirs: ['src/hooks'], // 自动导入 hooks
  121. vueTemplate: true, // default false
  122. }),
  123. ViteRestart({
  124. // 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
  125. restart: ['vite.config.js'],
  126. }),
  127. // h5环境增加 BUILD_TIME 和 BUILD_BRANCH
  128. UNI_PLATFORM === 'h5' && {
  129. name: 'html-transform',
  130. transformIndexHtml(html) {
  131. return html.replace('%BUILD_TIME%', dayjs().format('YYYY-MM-DD HH:mm:ss')).replace('%VITE_APP_TITLE%', VITE_APP_TITLE)
  132. },
  133. },
  134. // 打包分析插件,h5 + 生产环境才弹出
  135. UNI_PLATFORM === 'h5'
  136. && mode === 'production'
  137. && visualizer({
  138. filename: './node_modules/.cache/visualizer/stats.html',
  139. open: true,
  140. gzipSize: true,
  141. brotliSize: true,
  142. }),
  143. // 原生插件资源复制插件 - 仅在 app 平台且启用时生效
  144. createCopyNativeResourcesPlugin(
  145. UNI_PLATFORM === 'app' && VITE_COPY_NATIVE_RES_ENABLE === 'true',
  146. {
  147. verbose: mode === 'development', // 开发模式显示详细日志
  148. },
  149. ),
  150. syncManifestPlugin(),
  151. Components({
  152. resolvers: [WotResolver()],
  153. extensions: ['vue'],
  154. deep: true, // 是否递归扫描子目录,
  155. directoryAsNamespace: false, // 是否把目录名作为命名空间前缀,true 时组件名为 目录名+组件名,
  156. dts: 'src/types/components.d.ts', // 自动生成的组件类型声明文件路径(用于 TypeScript 支持)
  157. }),
  158. // 自动打开开发者工具插件 (必须修改 .env 文件中的 VITE_WX_APPID)
  159. openDevTools(),
  160. ],
  161. define: {
  162. __VITE_APP_PROXY__: JSON.stringify(VITE_APP_PROXY_ENABLE),
  163. },
  164. css: {
  165. postcss: {
  166. plugins: [
  167. // autoprefixer({
  168. // // 指定目标浏览器
  169. // overrideBrowserslist: ['> 1%', 'last 2 versions'],
  170. // }),
  171. ],
  172. },
  173. },
  174. resolve: {
  175. alias: {
  176. '@': path.join(process.cwd(), './src'),
  177. '@img': path.join(process.cwd(), './src/static/images'),
  178. },
  179. },
  180. server: {
  181. host: '0.0.0.0',
  182. hmr: true,
  183. port: Number.parseInt(VITE_APP_PORT, 10),
  184. // 仅 H5 端生效,其他端不生效(其他端走build,不走devServer)
  185. proxy: JSON.parse(VITE_APP_PROXY_ENABLE)
  186. ? {
  187. [VITE_APP_PROXY_PREFIX]: {
  188. target: VITE_SERVER_BASEURL,
  189. changeOrigin: true,
  190. // 后端有/api前缀则不做处理,没有则需要去掉
  191. rewrite: path => path.replace(new RegExp(`^${VITE_APP_PROXY_PREFIX}`), ''),
  192. },
  193. }
  194. : undefined,
  195. },
  196. esbuild: {
  197. drop: VITE_DELETE_CONSOLE === 'true' ? ['console', 'debugger'] : [],
  198. },
  199. build: {
  200. sourcemap: false,
  201. // 方便非h5端调试
  202. // sourcemap: VITE_SHOW_SOURCEMAP === 'true', // 默认是false
  203. target: 'es6',
  204. // 开发环境不用压缩
  205. minify: mode === 'development' ? false : 'esbuild',
  206. },
  207. })
  208. })