vite.config.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 } from 'vite-plugin-style-import'
  10. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  11. import PurgeIcons from 'vite-plugin-purge-icons'
  12. import DefineOptions from 'unplugin-vue-define-options/vite'
  13. import { createHtmlPlugin } from 'vite-plugin-html'
  14. import viteCompression from 'vite-plugin-compression'
  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()],
  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. }),
  65. EslintPlugin({
  66. cache: false,
  67. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  68. }),
  69. VueI18n({
  70. runtimeOnly: true,
  71. compositionOnly: true,
  72. include: [resolve(__dirname, 'src/locales/**')]
  73. }),
  74. createSvgIconsPlugin({
  75. iconDirs: [pathResolve('src/assets/svgs')],
  76. symbolId: 'icon-[dir]-[name]',
  77. svgoOptions: true
  78. }),
  79. PurgeIcons(),
  80. DefineOptions(),
  81. viteCompression({
  82. verbose: true, // 是否在控制台输出压缩结果
  83. disable: true, // 是否禁用
  84. threshold: 10240, // 体积大于 threshold 才会被压缩,单位 b
  85. algorithm: 'gzip', // 压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
  86. ext: '.gz', // 生成的压缩包后缀
  87. deleteOriginFile: false //压缩后是否删除源文件
  88. }),
  89. createHtmlPlugin({
  90. inject: {
  91. data: {
  92. title: env.VITE_APP_TITLE,
  93. injectScript: `<script src="./inject.js"></script>`
  94. }
  95. }
  96. })
  97. ],
  98. css: {
  99. preprocessorOptions: {
  100. less: {
  101. additionalData: '@import "./src/styles/variables.module.less";',
  102. javascriptEnabled: true
  103. }
  104. }
  105. },
  106. resolve: {
  107. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
  108. alias: [
  109. {
  110. find: 'vue-i18n',
  111. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  112. },
  113. {
  114. find: /\@\//,
  115. replacement: `${pathResolve('src')}/`
  116. }
  117. ]
  118. },
  119. build: {
  120. minify: 'terser',
  121. outDir: env.VITE_OUT_DIR || 'dist',
  122. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  123. // brotliSize: false,
  124. terserOptions: {
  125. compress: {
  126. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  127. drop_console: env.VITE_DROP_CONSOLE === 'true'
  128. }
  129. }
  130. },
  131. optimizeDeps: {
  132. include: [
  133. 'vue',
  134. 'vue-router',
  135. 'vue-types',
  136. 'vue-i18n',
  137. 'element-plus/es',
  138. 'element-plus/es/locale/lang/zh-cn',
  139. 'element-plus/es/locale/lang/en',
  140. '@iconify/iconify',
  141. '@vueuse/core',
  142. 'axios',
  143. 'qs',
  144. 'dayjs',
  145. 'echarts',
  146. 'echarts-wordcloud',
  147. 'intro.js',
  148. 'qrcode',
  149. 'pinia',
  150. '@wangeditor/editor',
  151. '@wangeditor/editor-for-vue'
  152. ]
  153. }
  154. }
  155. }