vite.config.ts 5.3 KB

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