vite.config.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { resolve } from 'path'
  2. import { loadEnv } from 'vite'
  3. import type { UserConfig, ConfigEnv } from 'vite'
  4. import Vue from '@vitejs/plugin-vue'
  5. import VueJsx from '@vitejs/plugin-vue-jsx'
  6. import VueI18n from '@intlify/vite-plugin-vue-i18n'
  7. import WindiCSS from 'vite-plugin-windicss'
  8. import progress from 'vite-plugin-progress'
  9. import EslintPlugin from 'vite-plugin-eslint'
  10. import PurgeIcons from 'vite-plugin-purge-icons'
  11. import { createHtmlPlugin } from 'vite-plugin-html'
  12. import viteCompression from 'vite-plugin-compression'
  13. import vueSetupExtend from 'vite-plugin-vue-setup-extend'
  14. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  15. import { createStyleImportPlugin, ElementPlusResolve, VxeTableResolve } from 'vite-plugin-style-import'
  16. // 当前执行node命令时文件夹的地址(工作目录)
  17. const root = process.cwd()
  18. // 路径查找
  19. function pathResolve(dir: string) {
  20. return resolve(root, '.', dir)
  21. }
  22. // https://vitejs.dev/config/
  23. export default ({ command, mode }: ConfigEnv): UserConfig => {
  24. let env = {} as any
  25. const isBuild = command === 'build'
  26. if (!isBuild) {
  27. env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
  28. } else {
  29. env = loadEnv(mode, root)
  30. }
  31. return {
  32. base: env.VITE_BASE_PATH,
  33. root: root,
  34. // 服务端渲染
  35. server: {
  36. // 是否开启 https
  37. https: false,
  38. // 端口号
  39. port: env.VITE_PORT,
  40. host: "0.0.0.0",
  41. open: env.VITE_OPEN,
  42. // 本地跨域代理
  43. proxy: {
  44. ['/dev-api']: {
  45. target: env.VITE_BASE_URL,
  46. ws: false,
  47. changeOrigin: true,
  48. rewrite: (path) => path.replace(new RegExp(`^/dev-api`), ''),
  49. },
  50. },
  51. },
  52. plugins: [
  53. Vue(),
  54. VueJsx(),
  55. WindiCSS(),
  56. progress(),
  57. PurgeIcons(),
  58. vueSetupExtend(),
  59. createStyleImportPlugin({
  60. resolves: [ElementPlusResolve(),VxeTableResolve()],
  61. libs: [{
  62. libraryName: 'element-plus',
  63. esModule: true,
  64. resolveStyle: (name) => {
  65. return `element-plus/es/components/${name.substring(3)}/style/css`
  66. }
  67. },{
  68. libraryName: 'vxe-table',
  69. esModule: true,
  70. resolveStyle: (name) => {
  71. return `vxe-table/es/${name}/style.css`
  72. }
  73. }]
  74. }),
  75. EslintPlugin({
  76. cache: false,
  77. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  78. }),
  79. VueI18n({
  80. runtimeOnly: true,
  81. compositionOnly: true,
  82. include: [resolve(__dirname, 'src/locales/**')]
  83. }),
  84. createSvgIconsPlugin({
  85. iconDirs: [pathResolve('src/assets/svgs')],
  86. symbolId: 'icon-[dir]-[name]',
  87. svgoOptions: true
  88. }),
  89. viteCompression({
  90. verbose: true, // 是否在控制台输出压缩结果
  91. disable: false, // 是否禁用
  92. threshold: 10240, // 体积大于 threshold 才会被压缩,单位 b
  93. algorithm: 'gzip', // 压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
  94. ext: '.gz', // 生成的压缩包后缀
  95. deleteOriginFile: true //压缩后是否删除源文件
  96. }),
  97. createHtmlPlugin({
  98. inject: {
  99. data: {
  100. title: env.VITE_APP_TITLE,
  101. injectScript: `<script src="./inject.js"></script>`
  102. }
  103. }
  104. })
  105. ],
  106. css: {
  107. preprocessorOptions: {
  108. scss: {
  109. additionalData: '@import "./src/styles/variables.scss";',
  110. javascriptEnabled: true
  111. }
  112. }
  113. },
  114. resolve: {
  115. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
  116. alias: [
  117. {
  118. find: 'vue-i18n',
  119. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  120. },
  121. {
  122. find: /\@\//,
  123. replacement: `${pathResolve('src')}/`
  124. }
  125. ]
  126. },
  127. build: {
  128. minify: 'terser',
  129. outDir: env.VITE_OUT_DIR || 'dist',
  130. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  131. // brotliSize: false,
  132. terserOptions: {
  133. compress: {
  134. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  135. drop_console: env.VITE_DROP_CONSOLE === 'true'
  136. }
  137. }
  138. },
  139. optimizeDeps: {
  140. include: [
  141. 'vue',
  142. 'vue-router',
  143. 'vue-types',
  144. 'vue-i18n',
  145. 'vxe-table',
  146. 'xe-utils',
  147. 'lodash-es',
  148. 'element-plus/es',
  149. 'element-plus/es/locale/lang/zh-cn',
  150. 'element-plus/es/locale/lang/en',
  151. '@iconify/iconify',
  152. '@vueuse/core',
  153. 'axios',
  154. 'qs',
  155. 'dayjs',
  156. 'echarts',
  157. 'echarts-wordcloud',
  158. 'intro.js',
  159. 'qrcode',
  160. 'pinia',
  161. 'crypto-js',
  162. '@wangeditor/editor',
  163. '@wangeditor/editor-for-vue'
  164. ]
  165. }
  166. }
  167. }