vite.config.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 ServerUrlCopy from 'vite-plugin-url-copy'
  12. import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
  13. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  14. import { createStyleImportPlugin, ElementPlusResolve } from 'vite-plugin-style-import'
  15. import UnoCSS from 'unocss/vite'
  16. import { visualizer } from 'rollup-plugin-visualizer'
  17. // https://vitejs.dev/config/
  18. const root = process.cwd()
  19. function pathResolve(dir: string) {
  20. return resolve(root, '.', dir)
  21. }
  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. plugins: [
  33. Vue({
  34. script: {
  35. // 开启defineModel
  36. defineModel: true
  37. }
  38. }),
  39. VueJsx(),
  40. ServerUrlCopy(),
  41. progress(),
  42. env.VITE_USE_ALL_ELEMENT_PLUS_STYLE === 'false'
  43. ? createStyleImportPlugin({
  44. resolves: [ElementPlusResolve()],
  45. libs: [
  46. {
  47. libraryName: 'element-plus',
  48. esModule: true,
  49. resolveStyle: (name) => {
  50. if (name === 'click-outside') {
  51. return ''
  52. }
  53. return `element-plus/es/components/${name.replace(/^el-/, '')}/style/css`
  54. }
  55. }
  56. ]
  57. })
  58. : undefined,
  59. EslintPlugin({
  60. cache: false,
  61. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  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. ],
  91. css: {
  92. preprocessorOptions: {
  93. less: {
  94. additionalData: '@import "./src/styles/variables.module.less";',
  95. javascriptEnabled: true
  96. }
  97. }
  98. },
  99. resolve: {
  100. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
  101. alias: [
  102. {
  103. find: 'vue-i18n',
  104. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  105. },
  106. {
  107. find: /\@\//,
  108. replacement: `${pathResolve('src')}/`
  109. }
  110. ]
  111. },
  112. esbuild: {
  113. pure: env.VITE_DROP_CONSOLE === 'true' ? ['console.log'] : undefined,
  114. drop: env.VITE_DROP_DEBUGGER === 'true' ? ['debugger'] : undefined
  115. },
  116. build: {
  117. target: 'es2015',
  118. outDir: env.VITE_OUT_DIR || 'dist',
  119. sourcemap: env.VITE_SOURCEMAP === 'true',
  120. // brotliSize: false,
  121. rollupOptions: {
  122. plugins: env.VITE_USE_BUNDLE_ANALYZER === 'true' ? [visualizer()] : undefined,
  123. // 拆包
  124. output: {
  125. manualChunks: {
  126. 'vue-chunks': ['vue', 'vue-router', 'pinia', 'vue-i18n'],
  127. 'element-plus': ['element-plus'],
  128. 'wang-editor': ['@wangeditor/editor', '@wangeditor/editor-for-vue'],
  129. echarts: ['echarts', 'echarts-wordcloud']
  130. }
  131. }
  132. },
  133. cssCodeSplit: !(env.VITE_USE_CSS_SPLIT === 'false')
  134. },
  135. server: {
  136. port: 4000,
  137. proxy: {
  138. // 选项写法
  139. '/api': {
  140. target: 'http://127.0.0.1:8000',
  141. changeOrigin: true,
  142. rewrite: (path) => path.replace(/^\/api/, '')
  143. }
  144. },
  145. hmr: {
  146. overlay: false
  147. },
  148. host: '0.0.0.0'
  149. },
  150. optimizeDeps: {
  151. include: [
  152. 'vue',
  153. 'vue-router',
  154. 'vue-types',
  155. 'element-plus/es/locale/lang/zh-cn',
  156. 'element-plus/es/locale/lang/en',
  157. '@iconify/iconify',
  158. '@vueuse/core',
  159. 'axios',
  160. 'qs',
  161. 'echarts',
  162. 'echarts-wordcloud',
  163. 'qrcode',
  164. '@wangeditor/editor',
  165. '@wangeditor/editor-for-vue',
  166. 'vue-json-pretty',
  167. '@zxcvbn-ts/core',
  168. 'dayjs',
  169. 'cropperjs'
  170. ]
  171. }
  172. }
  173. }