vite.config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 VueSetupExtend from 'vite-plugin-vue-setup-extend'
  10. import EslintPlugin from 'vite-plugin-eslint'
  11. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  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[4], 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. VueSetupExtend(),
  33. AutoImport({
  34. imports: [
  35. 'vue',
  36. 'vue-router',
  37. // 'vue-i18n',
  38. '@vueuse/core'
  39. ],
  40. dts: 'src/auto-imports.d.ts'
  41. }),
  42. Components({
  43. // allow auto load markdown components under `./src/components/`
  44. dirs: ['src/components', 'src/layout'],
  45. extensions: ['vue', 'md'],
  46. // allow auto import and register components used in markdown
  47. include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
  48. // custom resolvers
  49. resolvers: [
  50. [ElementPlusResolver()]
  51. ],
  52. dts: 'src/components.d.ts'
  53. }),
  54. EslintPlugin({
  55. cache: false,
  56. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  57. })
  58. ],
  59. css: {
  60. preprocessorOptions: {
  61. less: {
  62. // additionalData: '@import "./src/styles/variables.less";',
  63. javascriptEnabled: true
  64. }
  65. }
  66. },
  67. resolve: {
  68. alias: [
  69. {
  70. find: /\@\//,
  71. replacement: `${pathResolve('src')}/`
  72. }
  73. ]
  74. },
  75. build: {
  76. minify: 'terser',
  77. outDir: env.VITE_OUT_DIR,
  78. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  79. brotliSize: false,
  80. terserOptions: {
  81. compress: {
  82. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  83. drop_console: env.VITE_DROP_CONSOLE === 'true'
  84. }
  85. }
  86. },
  87. server: {
  88. proxy: {
  89. // 字符串简写写法
  90. '/foo': 'http://localhost:4567/foo',
  91. // 选项写法
  92. '/api': {
  93. target: 'http://jsonplaceholder.typicode.com',
  94. changeOrigin: true,
  95. rewrite: path => path.replace(/^\/api/, '')
  96. },
  97. // 正则表达式写法
  98. '^/fallback/.*': {
  99. target: 'http://jsonplaceholder.typicode.com',
  100. changeOrigin: true,
  101. rewrite: path => path.replace(/^\/fallback/, '')
  102. }
  103. }
  104. },
  105. optimizeDeps: {
  106. include: [
  107. 'vue',
  108. 'vue-router'
  109. ]
  110. }
  111. }
  112. }