vite.config.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. }),
  61. VueI18nPlugin({
  62. runtimeOnly: true,
  63. compositionOnly: true,
  64. include: [resolve(__dirname, 'src/locales/**')]
  65. }),
  66. createSvgIconsPlugin({
  67. iconDirs: [pathResolve('src/assets/svgs')],
  68. symbolId: 'icon-[dir]-[name]',
  69. svgoOptions: true
  70. }),
  71. PurgeIcons(),
  72. env.VITE_USE_MOCK === 'true'
  73. ? viteMockServe({
  74. ignore: /^\_/,
  75. mockPath: 'mock',
  76. localEnabled: !isBuild,
  77. prodEnabled: isBuild,
  78. injectCode: `
  79. import { setupProdMockServer } from '../mock/_createProductionServer'
  80. setupProdMockServer()
  81. `
  82. })
  83. : undefined,
  84. ViteEjsPlugin({
  85. title: env.VITE_APP_TITLE
  86. }),
  87. UnoCSS()
  88. ],
  89. css: {
  90. preprocessorOptions: {
  91. less: {
  92. additionalData: '@import "./src/styles/variables.module.less";',
  93. javascriptEnabled: true
  94. }
  95. }
  96. },
  97. resolve: {
  98. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
  99. alias: [
  100. {
  101. find: 'vue-i18n',
  102. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  103. },
  104. {
  105. find: /\@\//,
  106. replacement: `${pathResolve('src')}/`
  107. }
  108. ]
  109. },
  110. esbuild: {
  111. pure: env.VITE_DROP_CONSOLE === 'true' ? ['console.log'] : undefined,
  112. drop: env.VITE_DROP_DEBUGGER === 'true' ? ['debugger'] : undefined
  113. },
  114. build: {
  115. target: 'es2015',
  116. outDir: env.VITE_OUT_DIR || 'dist',
  117. sourcemap: env.VITE_SOURCEMAP === 'true',
  118. // brotliSize: false,
  119. rollupOptions: {
  120. plugins: env.VITE_USE_BUNDLE_ANALYZER === 'true' ? [visualizer()] : undefined,
  121. // 拆包
  122. output: {
  123. manualChunks: {
  124. 'vue-chunks': ['vue', 'vue-router', 'pinia', 'vue-i18n'],
  125. 'element-plus': ['element-plus'],
  126. 'wang-editor': ['@wangeditor/editor', '@wangeditor/editor-for-vue'],
  127. echarts: ['echarts', 'echarts-wordcloud']
  128. }
  129. }
  130. },
  131. cssCodeSplit: !(env.VITE_USE_CSS_SPLIT === 'false')
  132. },
  133. server: {
  134. port: 4000,
  135. proxy: {
  136. // 选项写法
  137. '/api': {
  138. target: 'http://127.0.0.1:8000',
  139. changeOrigin: true,
  140. rewrite: (path) => path.replace(/^\/api/, '')
  141. }
  142. },
  143. hmr: {
  144. overlay: false
  145. },
  146. host: '0.0.0.0'
  147. },
  148. optimizeDeps: {
  149. include: [
  150. 'vue',
  151. 'vue-router',
  152. 'vue-types',
  153. 'element-plus/es/locale/lang/zh-cn',
  154. 'element-plus/es/locale/lang/en',
  155. '@iconify/iconify',
  156. '@vueuse/core',
  157. 'axios',
  158. 'qs',
  159. 'echarts',
  160. 'echarts-wordcloud',
  161. 'qrcode',
  162. '@wangeditor/editor',
  163. '@wangeditor/editor-for-vue',
  164. 'vue-json-pretty',
  165. '@zxcvbn-ts/core',
  166. 'dayjs',
  167. 'cropperjs'
  168. ]
  169. }
  170. }
  171. }