vite.config.ts 4.6 KB

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