vite.config.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. libs: [{
  37. libraryName: 'element-plus',
  38. esModule: true,
  39. resolveStyle: (name) => {
  40. return `element-plus/es/components/${name.substring(3)}/style/css`
  41. }
  42. }]
  43. }),
  44. // AutoImport({
  45. // imports: [
  46. // 'vue',
  47. // 'vue-router',
  48. // 'vue-i18n',
  49. // '@vueuse/core'
  50. // ],
  51. // dts: 'src/types/auto-imports.d.ts'
  52. // }),
  53. Icons({
  54. compiler: 'vue3',
  55. autoInstall: true
  56. }),
  57. // Components({
  58. // dirs: ['src/components'],
  59. // extensions: ['vue', 'md'],
  60. // include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
  61. // // custom resolvers
  62. // resolvers: [
  63. // ElementPlusResolver(),
  64. // IconsResolver({
  65. // prefix: false,
  66. // enabledCollections : ['ep']
  67. // })
  68. // ],
  69. // dts: 'src/types/components.d.ts'
  70. // }),
  71. EslintPlugin({
  72. cache: false,
  73. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  74. }),
  75. VueI18n({
  76. runtimeOnly: true,
  77. compositionOnly: true,
  78. include: [resolve(__dirname, 'src/locales/**')]
  79. })
  80. ],
  81. css: {
  82. preprocessorOptions: {
  83. less: {
  84. additionalData: '@import "./src/styles/variables.less";',
  85. javascriptEnabled: true
  86. }
  87. }
  88. },
  89. resolve: {
  90. alias: [
  91. {
  92. find: 'vue-i18n',
  93. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  94. },
  95. {
  96. find: /\@\//,
  97. replacement: `${pathResolve('src')}/`
  98. }
  99. ]
  100. },
  101. build: {
  102. minify: 'terser',
  103. outDir: env.VITE_OUT_DIR,
  104. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  105. brotliSize: false,
  106. terserOptions: {
  107. compress: {
  108. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  109. drop_console: env.VITE_DROP_CONSOLE === 'true'
  110. }
  111. }
  112. },
  113. server: {
  114. proxy: {
  115. // 字符串简写写法
  116. '/foo': 'http://localhost:4567/foo',
  117. // 选项写法
  118. '/api': {
  119. target: 'http://jsonplaceholder.typicode.com',
  120. changeOrigin: true,
  121. rewrite: path => path.replace(/^\/api/, '')
  122. },
  123. // 正则表达式写法
  124. '^/fallback/.*': {
  125. target: 'http://jsonplaceholder.typicode.com',
  126. changeOrigin: true,
  127. rewrite: path => path.replace(/^\/fallback/, '')
  128. }
  129. }
  130. },
  131. optimizeDeps: {
  132. include: [
  133. 'vue',
  134. 'vue-router',
  135. 'vue-types'
  136. ]
  137. }
  138. }
  139. }