vite.config.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { resolve } from 'path'
  2. import { defineConfig, loadEnv } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import Components from 'unplugin-vue-components/vite'
  5. import VueJSX from '@vitejs/plugin-vue-jsx'
  6. import AutoImport from 'unplugin-auto-import/vite'
  7. import vueSetupExtend from 'vite-plugin-vue-setup-extend'
  8. import { visualizer } from 'rollup-plugin-visualizer'
  9. import Less2CssVariablePlugin from 'antd-less-to-css-variable'
  10. import viteCompression from 'vite-plugin-compression'
  11. export const r = (...args) => resolve(__dirname, '.', ...args)
  12. const removeModulePreloadPlugin = (keys) => {
  13. if (!keys || !keys.length) {
  14. return
  15. }
  16. return {
  17. name: 'remove-module-preload',
  18. transformIndexHtml: {
  19. enforce: 'after',
  20. transform(html, ctx) {
  21. let result = html
  22. keys.forEach((key) => {
  23. result = result.replace(new RegExp(`<link rel="modulepreload"?.*${key}?.*`), '')
  24. })
  25. return result
  26. }
  27. }
  28. }
  29. }
  30. export default defineConfig(({ command, mode }) => {
  31. const envConfig = loadEnv(mode, './')
  32. const alias = {
  33. '~': `${resolve(__dirname, './')}`,
  34. '@/': `${resolve(__dirname, 'src')}/`
  35. }
  36. return {
  37. server: {
  38. port: envConfig.VITE_PORT,
  39. proxy: {
  40. '/api': {
  41. target: 'http://192.168.1.245:9003',
  42. // target: 'http://192.168.31.15:9003',
  43. // target: 'http://192.168.31.15:9003',
  44. ws: false,
  45. changeOrigin: true
  46. // rewrite: (path) => path.replace(/^\/api/, '')
  47. }
  48. }
  49. },
  50. resolve: {
  51. alias
  52. },
  53. // 解决警告You are running the esm-bundler build of vue-i18n.
  54. define: {
  55. __VUE_I18N_FULL_INSTALL__: true,
  56. __VUE_I18N_LEGACY_API__: true,
  57. __VUE_I18N_PROD_DEVTOOLS__: true
  58. },
  59. build: {
  60. // sourcemap: true,
  61. manifest: true,
  62. brotliSize: false,
  63. rollupOptions: {
  64. output: {
  65. manualChunks: {
  66. echarts: ['echarts'],
  67. 'ant-design-vue': ['ant-design-vue'],
  68. vue: ['vue', 'vue-router', 'pinia', 'vue-i18n']
  69. }
  70. }
  71. },
  72. chunkSizeWarningLimit: 1000
  73. },
  74. plugins: [
  75. vue({
  76. script: {
  77. refTransform: true
  78. }
  79. }),
  80. viteCompression(),
  81. vueSetupExtend(),
  82. VueJSX(),
  83. AutoImport({
  84. imports: ['vue'],
  85. dirs: ['./src/utils/permission'],
  86. dts: r('src/auto-imports.d.ts')
  87. }),
  88. // 组件按需引入
  89. Components({
  90. dirs: [r('src/components')],
  91. dts: false,
  92. resolvers: []
  93. }),
  94. visualizer()
  95. ],
  96. css: {
  97. preprocessorOptions: {
  98. less: {
  99. javascriptEnabled: true,
  100. plugins: [new Less2CssVariablePlugin()]
  101. }
  102. }
  103. },
  104. optimizeDeps: {
  105. esbuildOptions: {
  106. loader: {
  107. '.js': 'jsx'
  108. }
  109. }
  110. }
  111. }
  112. })