vite.config.ts 3.3 KB

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