vite.config.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { defineConfig, loadEnv } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import vueJsx from '@vitejs/plugin-vue-jsx'
  4. import path from 'path'
  5. import viteCompression from 'vite-plugin-compression'
  6. import Components from 'unplugin-vue-components/vite'
  7. import AutoImport from 'unplugin-auto-import/vite'
  8. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  9. import { fileURLToPath } from 'url'
  10. import vueDevTools from 'vite-plugin-vue-devtools'
  11. // import { visualizer } from 'rollup-plugin-visualizer'
  12. import tailwindcss from '@tailwindcss/vite'
  13. export default ({ mode }: { mode: string }) => {
  14. const root = process.cwd()
  15. const env = loadEnv(mode, root)
  16. const { VITE_VERSION, VITE_PORT, VITE_BASE_URL, VITE_API_PROXY_URL } = env
  17. return defineConfig({
  18. define: {
  19. __APP_VERSION__: JSON.stringify(VITE_VERSION)
  20. },
  21. base: VITE_BASE_URL,
  22. server: {
  23. port: Number(VITE_PORT),
  24. proxy: {
  25. '/api': {
  26. target: VITE_API_PROXY_URL,
  27. changeOrigin: true,
  28. secure: false,
  29. rewrite: (path) => path.replace(/^\/api/, '')
  30. }
  31. },
  32. host: true
  33. },
  34. // 路径别名
  35. resolve: {
  36. alias: {
  37. '@': fileURLToPath(new URL('./src', import.meta.url)),
  38. '@views': resolvePath('src/views'),
  39. '@imgs': resolvePath('src/assets/img'),
  40. '@icons': resolvePath('src/assets/icons'),
  41. '@utils': resolvePath('src/utils'),
  42. '@stores': resolvePath('src/store'),
  43. '@plugins': resolvePath('src/plugins'),
  44. '@styles': resolvePath('src/assets/styles')
  45. }
  46. },
  47. build: {
  48. target: 'es2015',
  49. outDir: 'dist/course',
  50. chunkSizeWarningLimit: 2000,
  51. minify: 'terser',
  52. terserOptions: {
  53. compress: {
  54. // 生产环境去除 console
  55. drop_console: true,
  56. // 生产环境去除 debugger
  57. drop_debugger: true
  58. }
  59. },
  60. dynamicImportVarsOptions: {
  61. warnOnError: true,
  62. exclude: [],
  63. include: ['src/views/**/*.vue']
  64. }
  65. },
  66. plugins: [
  67. vue(),
  68. vueJsx(),
  69. tailwindcss(),
  70. // 自动导入 components 目录下的组件
  71. Components({
  72. deep: true,
  73. extensions: ['vue'],
  74. dirs: ['src/components/core', 'src/components/business', 'src/components/icon'],
  75. resolvers: [ElementPlusResolver({ importStyle: false })],
  76. dts: 'src/types/components.d.ts'
  77. }),
  78. // 自动导入组件 Api
  79. AutoImport({
  80. imports: ['vue', 'vue-router', '@vueuse/core', 'pinia'],
  81. resolvers: [ElementPlusResolver()],
  82. dts: 'src/types/auto-imports.d.ts',
  83. // ESLint 配置
  84. eslintrc: {
  85. // 首次运行时设置为 true 生成配置文件,之后改为 false
  86. enabled: true,
  87. // ESLint 配置文件路径
  88. filepath: './.auto-import.json',
  89. // 允许全局使用自动导入的 API
  90. globalsPropValue: true
  91. }
  92. }),
  93. // 压缩
  94. viteCompression({
  95. verbose: false, // 是否在控制台输出压缩结果
  96. disable: false, // 是否禁用
  97. algorithm: 'gzip', // 压缩算法
  98. ext: '.gz', // 压缩后的文件名后缀
  99. threshold: 10240, // 只有大小大于该值的资源会被处理 10240B = 10KB
  100. deleteOriginFile: false // 压缩后是否删除原文件
  101. }),
  102. vueDevTools()
  103. // 打包分析
  104. // visualizer({
  105. // open: true,
  106. // gzipSize: true,
  107. // brotliSize: true,
  108. // filename: 'dist/stats.html' // 分析图生成的文件名及路径
  109. // }),
  110. ],
  111. css: {
  112. preprocessorOptions: {
  113. // sass variable and mixin
  114. scss: {
  115. api: 'modern-compiler',
  116. additionalData: `
  117. @use "@styles/variables.scss" as *;
  118. @use "@styles/mixin.scss" as *;
  119. `
  120. }
  121. },
  122. postcss: {
  123. plugins: [
  124. {
  125. postcssPlugin: 'internal:charset-removal',
  126. AtRule: {
  127. charset: (atRule) => {
  128. if (atRule.name === 'charset') {
  129. atRule.remove()
  130. }
  131. }
  132. }
  133. }
  134. ]
  135. }
  136. }
  137. })
  138. }
  139. function resolvePath(paths: string) {
  140. return path.resolve(__dirname, paths)
  141. }