vite.config.js 2.6 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.213:19003',
  42. // target: 'http://192.168.1.235:9003',
  43. // target: 'http://192.168.31.15:9003',
  44. // target: 'http://192.168.31.15:9003',
  45. ws: false,
  46. changeOrigin: true
  47. // rewrite: (path) => path.replace(/^\/api/, '')
  48. }
  49. }
  50. },
  51. resolve: {
  52. alias
  53. },
  54. // 解决警告You are running the esm-bundler build of vue-i18n.
  55. define: {
  56. __VUE_I18N_FULL_INSTALL__: true,
  57. __VUE_I18N_LEGACY_API__: true,
  58. __VUE_I18N_PROD_DEVTOOLS__: true
  59. },
  60. build: {
  61. // sourcemap: true,
  62. manifest: true,
  63. brotliSize: false,
  64. rollupOptions: {
  65. output: {
  66. manualChunks: {
  67. echarts: ['echarts'],
  68. 'ant-design-vue': ['ant-design-vue'],
  69. vue: ['vue', 'vue-router', 'pinia', 'vue-i18n']
  70. }
  71. }
  72. },
  73. chunkSizeWarningLimit: 1000
  74. },
  75. plugins: [
  76. vue({
  77. script: {
  78. refTransform: true
  79. }
  80. }),
  81. viteCompression(),
  82. vueSetupExtend(),
  83. VueJSX(),
  84. AutoImport({
  85. imports: ['vue'],
  86. dirs: ['./src/utils/permission'],
  87. dts: r('src/auto-imports.d.ts')
  88. }),
  89. // 组件按需引入
  90. Components({
  91. dirs: [r('src/components')],
  92. dts: false,
  93. resolvers: []
  94. }),
  95. visualizer()
  96. ],
  97. css: {
  98. preprocessorOptions: {
  99. less: {
  100. javascriptEnabled: true,
  101. plugins: [new Less2CssVariablePlugin()]
  102. }
  103. }
  104. },
  105. optimizeDeps: {
  106. esbuildOptions: {
  107. loader: {
  108. '.js': 'jsx'
  109. }
  110. }
  111. }
  112. }
  113. })