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