vite.config.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Plugins
  2. import Components from 'unplugin-vue-components/vite'
  3. import Vue from '@vitejs/plugin-vue'
  4. import Vuetify, { transformAssetUrls } from 'vite-plugin-vuetify'
  5. // import ViteFonts from 'unplugin-fonts/vite'
  6. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  7. import AutoImport from 'unplugin-auto-import/vite'
  8. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  9. import { terser } from 'rollup-plugin-terser'
  10. import path from 'path'
  11. import compression from 'vite-plugin-compression'
  12. // Utilities
  13. import { defineConfig } from 'vite'
  14. import { fileURLToPath, URL } from 'node:url'
  15. // https://vitejs.dev/config/
  16. export default defineConfig({
  17. plugins: [
  18. Vue({
  19. template: { transformAssetUrls }
  20. }),
  21. // https://github.com/vuetifyjs/vuetify-loader/tree/master/packages/vite-plugin#readme
  22. Vuetify(),
  23. Components({
  24. dts: true,
  25. resolvers: [
  26. (name) => {
  27. if (name.startsWith('Base')) {
  28. return { importName: name.slice(4), path: `@/components/CtVuetify/${name}.vue` }
  29. }
  30. },
  31. ElementPlusResolver(),
  32. ]
  33. }),
  34. compression({
  35. verbose: true,
  36. disable: false,
  37. threshold: 10240, // 只会压缩大于 10kb 的文件
  38. algorithm: 'gzip', // 可以选择 gzip 或 brotli
  39. ext: '.gz', // 输出后缀
  40. }),
  41. AutoImport({
  42. resolvers: [ElementPlusResolver()],
  43. }),
  44. createSvgIconsPlugin({
  45. iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
  46. // symbolId: 'icon-[dir]-[name]',
  47. })
  48. // ViteFonts({
  49. // google: {
  50. // // families: [{
  51. // // name: 'Roboto',
  52. // // styles: 'wght@100;300;400;500;700;900',
  53. // // }],
  54. // },
  55. // }),
  56. ],
  57. define: { 'process.env': {} },
  58. resolve: {
  59. alias: {
  60. '@': fileURLToPath(new URL('./src', import.meta.url))
  61. },
  62. extensions: [
  63. '.js',
  64. '.json',
  65. '.jsx',
  66. '.mjs',
  67. '.ts',
  68. '.tsx',
  69. '.vue',
  70. ],
  71. },
  72. build: {
  73. sourcemap: false, // 生产环境禁用源映射
  74. rollupOptions: {
  75. plugins: [terser({ ecma: 2020 })], // 启用 terser 进行代码压缩
  76. output: {
  77. // 设置静态资源引用的基础路径
  78. assetFileNames: 'assets/[name].[hash].[ext]',
  79. chunkFileNames: 'chunks/[name].[hash].js',
  80. entryFileNames: '[name].[hash].js',
  81. },
  82. watch: true // 允许浏览器在源码变化时自动刷新
  83. },
  84. },
  85. // 配置静态资源处理
  86. publicDir: 'public', // 设置静态资源目录
  87. assetsDir: 'assets', // 设置构建后的静态资源目录
  88. base: '/', // 设置应用的基本URL
  89. server: {
  90. port: 3000,
  91. proxy: {
  92. '/api': {
  93. target: 'http://192.168.3.143:5500',
  94. // target: 'https://company.citupro.com:18183',
  95. secure: false, // 是否支持 https,默认 false
  96. changeOrigin: true, // 是否支持跨域
  97. pathRewrite: {
  98. '^/api': '/api' // 重写路径,确保请求路径正确
  99. }
  100. },
  101. '/app-api': {
  102. target: 'http://192.168.3.80',
  103. changeOrigin: true, // 是否支持跨域
  104. pathRewrite: {
  105. '^/app-api': '/app-api' // 重写路径,确保请求路径正确
  106. }
  107. },
  108. '/admin-api': {
  109. target: 'http://192.168.3.80',
  110. changeOrigin: true, // 是否支持跨域
  111. pathRewrite: {
  112. '^/admin-api': '/admin-api' // 重写路径,确保请求路径正确
  113. }
  114. }
  115. }
  116. }
  117. // configureWebpack: {
  118. // output: {
  119. // filename: 'js/[name].' + new Date().getTime() + '.js',
  120. // chunkFilename: 'js/[name].' + new Date().getTime() + '.js'
  121. // }
  122. // }
  123. })