vite.config.js 2.5 KB

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