vite.config.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 Components from 'unplugin-vue-components/vite'
  7. // import AutoImport from 'unplugin-auto-import/vite'
  8. import VueJsx from '@vitejs/plugin-vue-jsx'
  9. import EslintPlugin from 'vite-plugin-eslint'
  10. // import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  11. import VueI18n from '@intlify/vite-plugin-vue-i18n'
  12. import Icons from 'unplugin-icons/vite'
  13. import StyleImport, { ElementPlusResolve } from 'vite-plugin-style-import'
  14. // https://vitejs.dev/config/
  15. const root = process.cwd()
  16. function pathResolve(dir: string) {
  17. return resolve(root, '.', dir)
  18. }
  19. // https://vitejs.dev/config/
  20. export default ({ command, mode }: ConfigEnv): UserConfig => {
  21. let env = null
  22. if (command === 'serve') {
  23. env = loadEnv(process.argv[4], root)
  24. }
  25. else {
  26. env = loadEnv(mode, root)
  27. }
  28. return {
  29. base: env.VITE_BASE_PATH,
  30. plugins: [
  31. Vue(),
  32. VueJsx(),
  33. WindiCSS(),
  34. StyleImport({
  35. resolves: [ElementPlusResolve()]
  36. }),
  37. // AutoImport({
  38. // imports: [
  39. // 'vue',
  40. // 'vue-router',
  41. // 'vue-i18n',
  42. // '@vueuse/core'
  43. // ],
  44. // dts: 'src/types/auto-imports.d.ts'
  45. // }),
  46. Icons({
  47. compiler: 'vue3',
  48. autoInstall: true
  49. }),
  50. // Components({
  51. // dirs: ['src/components'],
  52. // extensions: ['vue', 'md'],
  53. // include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
  54. // // custom resolvers
  55. // resolvers: [
  56. // ElementPlusResolver(),
  57. // IconsResolver({
  58. // prefix: false,
  59. // enabledCollections : ['ep']
  60. // })
  61. // ],
  62. // dts: 'src/types/components.d.ts'
  63. // }),
  64. EslintPlugin({
  65. cache: false,
  66. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  67. }),
  68. VueI18n({
  69. runtimeOnly: true,
  70. compositionOnly: true,
  71. include: [resolve(__dirname, 'src/locales/**')]
  72. })
  73. ],
  74. css: {
  75. preprocessorOptions: {
  76. less: {
  77. additionalData: '@import "./src/styles/variables.less";',
  78. javascriptEnabled: true
  79. }
  80. }
  81. },
  82. resolve: {
  83. alias: [
  84. {
  85. find: /\@\//,
  86. replacement: `${pathResolve('src')}/`
  87. }
  88. ]
  89. },
  90. build: {
  91. minify: 'terser',
  92. outDir: env.VITE_OUT_DIR,
  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. '/foo': 'http://localhost:4567/foo',
  106. // 选项写法
  107. '/api': {
  108. target: 'http://jsonplaceholder.typicode.com',
  109. changeOrigin: true,
  110. rewrite: path => path.replace(/^\/api/, '')
  111. },
  112. // 正则表达式写法
  113. '^/fallback/.*': {
  114. target: 'http://jsonplaceholder.typicode.com',
  115. changeOrigin: true,
  116. rewrite: path => path.replace(/^\/fallback/, '')
  117. }
  118. }
  119. },
  120. optimizeDeps: {
  121. include: [
  122. 'vue',
  123. 'vue-router',
  124. 'vue-types'
  125. ]
  126. }
  127. }
  128. }