vite.config.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 MockDevServerPlugin from 'vite-plugin-mock-dev-server'
  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. createStyleImportPlugin({
  41. resolves: [ElementPlusResolve()],
  42. libs: [{
  43. libraryName: 'element-plus',
  44. esModule: true,
  45. resolveStyle: (name) => {
  46. if (name === 'click-outside') {
  47. return ''
  48. }
  49. return `element-plus/es/components/${name.replace(/^el-/, '')}/style/css`
  50. }
  51. }]
  52. }),
  53. EslintPlugin({
  54. cache: false,
  55. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  56. }),
  57. VueI18nPlugin({
  58. runtimeOnly: true,
  59. compositionOnly: true,
  60. include: [resolve(__dirname, 'src/locales/**')]
  61. }),
  62. createSvgIconsPlugin({
  63. iconDirs: [pathResolve('src/assets/svgs')],
  64. symbolId: 'icon-[dir]-[name]',
  65. svgoOptions: true
  66. }),
  67. PurgeIcons(),
  68. // viteMockServe({
  69. // ignore: /^\_/,
  70. // mockPath: 'mock',
  71. // localEnabled: !isBuild,
  72. // prodEnabled: isBuild,
  73. // injectCode: `
  74. // import { setupProdMockServer } from '../mock/_createProductionServer'
  75. // setupProdMockServer()
  76. // `
  77. // }),
  78. MockDevServerPlugin({
  79. prefix: '/mock'
  80. }),
  81. ViteEjsPlugin({
  82. title: env.VITE_APP_TITLE
  83. }),
  84. UnoCSS(),
  85. // sveltekit(),
  86. ],
  87. css: {
  88. preprocessorOptions: {
  89. less: {
  90. additionalData: '@import "./src/styles/variables.module.less";',
  91. javascriptEnabled: true
  92. }
  93. }
  94. },
  95. resolve: {
  96. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
  97. alias: [
  98. {
  99. find: 'vue-i18n',
  100. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  101. },
  102. {
  103. find: /\@\//,
  104. replacement: `${pathResolve('src')}/`
  105. }
  106. ]
  107. },
  108. build: {
  109. minify: 'terser',
  110. outDir: env.VITE_OUT_DIR || 'dist',
  111. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  112. // brotliSize: false,
  113. terserOptions: {
  114. compress: {
  115. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  116. drop_console: env.VITE_DROP_CONSOLE === 'true'
  117. }
  118. }
  119. },
  120. server: {
  121. port: 4000,
  122. proxy: {
  123. // 选项写法
  124. '/api': {
  125. target: 'http://127.0.0.1:8000',
  126. changeOrigin: true,
  127. rewrite: path => path.replace(/^\/api/, '')
  128. }
  129. },
  130. hmr: {
  131. overlay: false
  132. },
  133. host: '0.0.0.0'
  134. },
  135. optimizeDeps: {
  136. include: [
  137. 'vue',
  138. 'vue-router',
  139. 'vue-types',
  140. 'element-plus/es/locale/lang/zh-cn',
  141. 'element-plus/es/locale/lang/en',
  142. '@iconify/iconify',
  143. '@vueuse/core',
  144. 'axios',
  145. 'qs',
  146. 'echarts',
  147. 'echarts-wordcloud',
  148. 'qrcode',
  149. '@wangeditor/editor',
  150. '@wangeditor/editor-for-vue',
  151. 'vue-json-pretty',
  152. '@zxcvbn-ts/core',
  153. 'dayjs',
  154. 'cropperjs'
  155. ]
  156. }
  157. }
  158. }