vite.config.ts 4.7 KB

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