vite.config.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // sveltekit(),
  89. ],
  90. css: {
  91. preprocessorOptions: {
  92. less: {
  93. additionalData: '@import "./src/styles/variables.module.less";',
  94. javascriptEnabled: true
  95. }
  96. }
  97. },
  98. resolve: {
  99. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
  100. alias: [
  101. {
  102. find: 'vue-i18n',
  103. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  104. },
  105. {
  106. find: /\@\//,
  107. replacement: `${pathResolve('src')}/`
  108. }
  109. ]
  110. },
  111. esbuild: {
  112. pure: env.VITE_DROP_CONSOLE === 'true' ? ['console.log'] : undefined,
  113. drop: env.VITE_DROP_DEBUGGER === 'true' ? ['debugger'] : undefined
  114. },
  115. build: {
  116. target: 'es2015',
  117. outDir: env.VITE_OUT_DIR || 'dist',
  118. sourcemap: env.VITE_SOURCEMAP === 'true',
  119. // brotliSize: false,
  120. rollupOptions: {
  121. plugins: env.VITE_USE_BUNDLE_ANALYZER === 'true' ? [visualizer()] : undefined,
  122. // 拆包
  123. output: {
  124. manualChunks: {
  125. 'vue-chunks': ['vue', 'vue-router', 'pinia', 'vue-i18n'],
  126. 'element-plus': ['element-plus'],
  127. 'wang-editor': ['@wangeditor/editor', '@wangeditor/editor-for-vue']
  128. }
  129. }
  130. },
  131. cssCodeSplit: !(env.VITE_USE_CSS_SPLIT === 'false')
  132. },
  133. server: {
  134. port: 3005,
  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. '@zxcvbn-ts/core',
  160. 'dayjs',
  161. 'xgplayer'
  162. ]
  163. }
  164. }
  165. }