vite.config.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 progress from 'vite-plugin-progress'
  7. import EslintPlugin from 'vite-plugin-eslint'
  8. import { ViteEjsPlugin } from "vite-plugin-ejs"
  9. import { viteMockServe } from 'vite-plugin-mock'
  10. import PurgeIcons from 'vite-plugin-purge-icons'
  11. import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite"
  12. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  13. import { createStyleImportPlugin, ElementPlusResolve } from 'vite-plugin-style-import'
  14. import UnoCSS from 'unocss/vite'
  15. import { visualizer } from 'rollup-plugin-visualizer'
  16. // https://vitejs.dev/config/
  17. const root = process.cwd()
  18. function pathResolve(dir: string) {
  19. return resolve(root, '.', dir)
  20. }
  21. export default ({ command, mode }: ConfigEnv): UserConfig => {
  22. let env = {} as any
  23. const isBuild = command === 'build'
  24. if (!isBuild) {
  25. env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
  26. } else {
  27. env = loadEnv(mode, root)
  28. }
  29. return {
  30. base: env.VITE_BASE_PATH,
  31. plugins: [
  32. Vue({
  33. script: {
  34. // 开启defineModel
  35. defineModel: true
  36. }
  37. }),
  38. VueJsx(),
  39. progress(),
  40. env.VITE_USE_ALL_ELEMENT_PLUS_STYLE === 'false'
  41. ? createStyleImportPlugin({
  42. resolves: [ElementPlusResolve()],
  43. libs: [
  44. {
  45. libraryName: 'element-plus',
  46. esModule: true,
  47. resolveStyle: (name) => {
  48. if (name === 'click-outside') {
  49. return ''
  50. }
  51. return `element-plus/es/components/${name.replace(/^el-/, '')}/style/css`
  52. }
  53. }
  54. ]
  55. })
  56. : undefined,
  57. EslintPlugin({
  58. cache: false,
  59. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'],
  60. failOnError: false,
  61. failOnWarning: false
  62. }),
  63. VueI18nPlugin({
  64. runtimeOnly: true,
  65. compositionOnly: true,
  66. include: [resolve(__dirname, 'src/locales/**')]
  67. }),
  68. createSvgIconsPlugin({
  69. iconDirs: [pathResolve('src/assets/svgs')],
  70. symbolId: 'icon-[dir]-[name]',
  71. svgoOptions: true
  72. }),
  73. PurgeIcons(),
  74. env.VITE_USE_MOCK === 'true'
  75. ? viteMockServe({
  76. ignore: /^\_/,
  77. mockPath: 'mock',
  78. localEnabled: !isBuild,
  79. prodEnabled: isBuild,
  80. injectCode: `
  81. import { setupProdMockServer } from '../mock/_createProductionServer'
  82. setupProdMockServer()
  83. `
  84. })
  85. : undefined,
  86. ViteEjsPlugin({
  87. title: env.VITE_APP_TITLE
  88. }),
  89. UnoCSS(),
  90. // sveltekit(),
  91. ],
  92. css: {
  93. preprocessorOptions: {
  94. less: {
  95. additionalData: '@import "./src/styles/variables.module.less";',
  96. javascriptEnabled: true
  97. }
  98. }
  99. },
  100. resolve: {
  101. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
  102. alias: [
  103. {
  104. find: 'vue-i18n',
  105. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  106. },
  107. {
  108. find: /\@\//,
  109. replacement: `${pathResolve('src')}/`
  110. }
  111. ]
  112. },
  113. esbuild: {
  114. pure: env.VITE_DROP_CONSOLE === 'true' ? ['console.log'] : undefined,
  115. drop: env.VITE_DROP_DEBUGGER === 'true' ? ['debugger'] : undefined
  116. },
  117. build: {
  118. target: 'es2015',
  119. outDir: env.VITE_OUT_DIR || 'dist',
  120. sourcemap: env.VITE_SOURCEMAP === 'true',
  121. // brotliSize: false,
  122. rollupOptions: {
  123. plugins: env.VITE_USE_BUNDLE_ANALYZER === 'true' ? [visualizer()] : undefined,
  124. // 拆包
  125. output: {
  126. manualChunks: {
  127. 'vue-chunks': ['vue', 'vue-router', 'pinia', 'vue-i18n'],
  128. 'element-plus': ['element-plus'],
  129. 'wang-editor': ['@wangeditor/editor', '@wangeditor/editor-for-vue']
  130. }
  131. }
  132. },
  133. cssCodeSplit: !(env.VITE_USE_CSS_SPLIT === 'false')
  134. },
  135. server: {
  136. port: 3005,
  137. proxy: {
  138. // 选项写法
  139. '/web/v1': {
  140. target: 'http://127.0.0.1:8765',
  141. changeOrigin: true,
  142. },
  143. '/api/v1': {
  144. target: 'http://127.0.0.1:8765',
  145. changeOrigin: true,
  146. },
  147. '/internal/': {
  148. target: 'http://127.0.0.1:8765',
  149. changeOrigin: true,
  150. }
  151. },
  152. hmr: {
  153. overlay: false
  154. },
  155. host: '0.0.0.0'
  156. },
  157. optimizeDeps: {
  158. include: [
  159. 'vue',
  160. 'vue-router',
  161. 'vue-types',
  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. '@zxcvbn-ts/core',
  169. 'dayjs',
  170. 'xgplayer'
  171. ]
  172. }
  173. }
  174. }