vite.config.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. if (command === 'serve') {
  22. env = loadEnv(process.argv[3], root)
  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. viteMockServe({
  58. ignore: /^\_/,
  59. mockPath: 'mock',
  60. localEnabled: !(command === 'serve'),
  61. prodEnabled: command !== 'serve',
  62. injectCode: `
  63. import { setupProdMockServer } from '../mock/_createProductionServer'
  64. setupProdMockServer()
  65. `
  66. })
  67. ],
  68. css: {
  69. preprocessorOptions: {
  70. less: {
  71. additionalData: '@import "./src/styles/variables.module.less";',
  72. javascriptEnabled: true
  73. }
  74. }
  75. },
  76. resolve: {
  77. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
  78. alias: [
  79. {
  80. find: 'vue-i18n',
  81. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  82. },
  83. {
  84. find: /\@\//,
  85. replacement: `${pathResolve('src')}/`
  86. }
  87. ]
  88. },
  89. build: {
  90. minify: 'terser',
  91. outDir: env.VITE_OUT_DIR || 'dist',
  92. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  93. brotliSize: false,
  94. terserOptions: {
  95. compress: {
  96. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  97. drop_console: env.VITE_DROP_CONSOLE === 'true'
  98. }
  99. }
  100. },
  101. server: {
  102. proxy: {
  103. // 字符串简写写法
  104. '/foo': 'http://localhost:4567/foo',
  105. // 选项写法
  106. '/api': {
  107. target: 'http://jsonplaceholder.typicode.com',
  108. changeOrigin: true,
  109. rewrite: path => path.replace(/^\/api/, '')
  110. },
  111. // 正则表达式写法
  112. '^/fallback/.*': {
  113. target: 'http://jsonplaceholder.typicode.com',
  114. changeOrigin: true,
  115. rewrite: path => path.replace(/^\/fallback/, '')
  116. }
  117. },
  118. hmr: {
  119. overlay: false
  120. }
  121. },
  122. optimizeDeps: {
  123. include: [
  124. 'vue',
  125. 'vue-router',
  126. 'vue-types',
  127. 'element-plus/lib/locale/lang/zh-cn',
  128. 'element-plus/lib/locale/lang/en',
  129. '@iconify/iconify',
  130. '@vueuse/core'
  131. ]
  132. }
  133. }
  134. }