vite.config.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 Icons from 'unplugin-icons/vite'
  10. import StyleImport, { ElementPlusResolve } from 'vite-plugin-style-import'
  11. // https://vitejs.dev/config/
  12. const root = process.cwd()
  13. function pathResolve(dir: string) {
  14. return resolve(root, '.', dir)
  15. }
  16. // https://vitejs.dev/config/
  17. export default ({ command, mode }: ConfigEnv): UserConfig => {
  18. let env = null
  19. if (command === 'serve') {
  20. env = loadEnv(process.argv[3], root)
  21. }
  22. else {
  23. env = loadEnv(mode, root)
  24. }
  25. return {
  26. base: env.VITE_BASE_PATH,
  27. plugins: [
  28. Vue(),
  29. VueJsx(),
  30. WindiCSS(),
  31. StyleImport({
  32. resolves: [ElementPlusResolve()],
  33. libs: [{
  34. libraryName: 'element-plus',
  35. esModule: true,
  36. resolveStyle: (name) => {
  37. return `element-plus/es/components/${name.substring(3)}/style/css`
  38. }
  39. }]
  40. }),
  41. Icons({
  42. compiler: 'vue3',
  43. autoInstall: true
  44. }),
  45. EslintPlugin({
  46. cache: false,
  47. include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
  48. }),
  49. VueI18n({
  50. runtimeOnly: true,
  51. compositionOnly: true,
  52. include: [resolve(__dirname, 'src/locales/**')]
  53. })
  54. ],
  55. css: {
  56. preprocessorOptions: {
  57. less: {
  58. additionalData: '@import "./src/styles/variables.module.less";',
  59. javascriptEnabled: true
  60. }
  61. }
  62. },
  63. resolve: {
  64. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
  65. alias: [
  66. {
  67. find: 'vue-i18n',
  68. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  69. },
  70. {
  71. find: /\@\//,
  72. replacement: `${pathResolve('src')}/`
  73. }
  74. ]
  75. },
  76. build: {
  77. minify: 'terser',
  78. outDir: env.VITE_OUT_DIR || 'dist',
  79. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  80. brotliSize: false,
  81. terserOptions: {
  82. compress: {
  83. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  84. drop_console: env.VITE_DROP_CONSOLE === 'true'
  85. }
  86. }
  87. },
  88. server: {
  89. proxy: {
  90. // 字符串简写写法
  91. '/foo': 'http://localhost:4567/foo',
  92. // 选项写法
  93. '/api': {
  94. target: 'http://jsonplaceholder.typicode.com',
  95. changeOrigin: true,
  96. rewrite: path => path.replace(/^\/api/, '')
  97. },
  98. // 正则表达式写法
  99. '^/fallback/.*': {
  100. target: 'http://jsonplaceholder.typicode.com',
  101. changeOrigin: true,
  102. rewrite: path => path.replace(/^\/fallback/, '')
  103. }
  104. },
  105. hmr: {
  106. overlay: false
  107. }
  108. },
  109. optimizeDeps: {
  110. include: [
  111. 'vue',
  112. 'vue-router',
  113. 'vue-types'
  114. ]
  115. }
  116. }
  117. }