vite.config.ts 3.2 KB

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