vite.config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 WindiCSS from 'vite-plugin-windicss'
  6. import VueJsx from '@vitejs/plugin-vue-jsx'
  7. import EslintPlugin from 'vite-plugin-eslint'
  8. import VueI18n from '@intlify/vite-plugin-vue-i18n'
  9. import StyleImport, { ElementPlusResolve } from 'vite-plugin-style-import'
  10. import ViteSvgIcons from 'vite-plugin-svg-icons'
  11. import PurgeIcons from 'vite-plugin-purge-icons'
  12. import { viteMockServe } from 'vite-plugin-mock'
  13. // https://vitejs.dev/config/
  14. const root = process.cwd()
  15. function pathResolve(dir: string) {
  16. return resolve(root, '.', dir)
  17. }
  18. // https://vitejs.dev/config/
  19. export default ({ command, mode }: ConfigEnv): UserConfig => {
  20. let env = null
  21. const isBuild = command === 'build'
  22. if (!isBuild) {
  23. env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
  24. } else {
  25. env = loadEnv(mode, root)
  26. }
  27. return {
  28. base: env.VITE_BASE_PATH,
  29. plugins: [
  30. Vue(),
  31. VueJsx(),
  32. WindiCSS(),
  33. StyleImport({
  34. resolves: [ElementPlusResolve()],
  35. libs: [{
  36. libraryName: 'element-plus',
  37. esModule: true,
  38. resolveStyle: (name) => {
  39. return `element-plus/es/components/${name.substring(3)}/style/css`
  40. }
  41. }]
  42. }),
  43. EslintPlugin({
  44. cache: false,
  45. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  46. }),
  47. VueI18n({
  48. runtimeOnly: true,
  49. compositionOnly: true,
  50. include: [resolve(__dirname, 'src/locales/**')]
  51. }),
  52. ViteSvgIcons({
  53. iconDirs: [pathResolve('src/assets/svgs')],
  54. symbolId: 'icon-[dir]-[name]',
  55. svgoOptions: true
  56. }),
  57. PurgeIcons(),
  58. viteMockServe({
  59. ignore: /^\_/,
  60. mockPath: 'mock',
  61. localEnabled: !isBuild,
  62. prodEnabled: isBuild,
  63. injectCode: `
  64. import { setupProdMockServer } from '../mock/_createProductionServer'
  65. setupProdMockServer()
  66. `
  67. })
  68. ],
  69. css: {
  70. preprocessorOptions: {
  71. less: {
  72. additionalData: '@import "./src/styles/variables.module.less";',
  73. javascriptEnabled: true
  74. }
  75. }
  76. },
  77. resolve: {
  78. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
  79. alias: [
  80. {
  81. find: 'vue-i18n',
  82. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  83. },
  84. {
  85. find: /\@\//,
  86. replacement: `${pathResolve('src')}/`
  87. }
  88. ]
  89. },
  90. build: {
  91. minify: 'terser',
  92. outDir: env.VITE_OUT_DIR || 'dist',
  93. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  94. brotliSize: false,
  95. terserOptions: {
  96. compress: {
  97. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  98. drop_console: env.VITE_DROP_CONSOLE === 'true'
  99. }
  100. }
  101. },
  102. server: {
  103. proxy: {
  104. // 选项写法
  105. // '/api': {
  106. // target: 'http://localhost:3000',
  107. // changeOrigin: true,
  108. // rewrite: path => path.replace(/^\/api/, '')
  109. // }
  110. },
  111. hmr: {
  112. overlay: false
  113. }
  114. },
  115. optimizeDeps: {
  116. include: [
  117. 'vue',
  118. 'vue-router',
  119. 'vue-types',
  120. 'element-plus/lib/locale/lang/zh-cn',
  121. 'element-plus/lib/locale/lang/en',
  122. '@iconify/iconify',
  123. '@vueuse/core',
  124. 'axios',
  125. 'qs'
  126. ]
  127. }
  128. }
  129. }