vite.config.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { defineConfig, loadEnv } from 'vite'
  2. import path from 'path'
  3. import vue from '@vitejs/plugin-vue'
  4. const rewritePlugin = (path, excludes) => ({
  5. name: "rewrite-middleware",
  6. configureServer(serve) {
  7. serve.middlewares.use((req, res, next) => {
  8. if (req.url.startsWith(path)) {
  9. const isRaw = excludes.some((dir) => req.url.includes(dir))
  10. if (!isRaw) req.url = path
  11. }
  12. next()
  13. })
  14. },
  15. })
  16. export default ({ mode, command }) => {
  17. const env = loadEnv(mode, process.cwd())
  18. const { VITE_APP_BASE } = env
  19. return defineConfig({
  20. base:'./',
  21. plugins: [vue(),
  22. rewritePlugin('/datav.html/', ['/datav.html/src', '/datav.html/static'])
  23. ],
  24. resolve: {
  25. // https://cn.vitejs.dev/config/#resolve-alias
  26. alias: {
  27. // 设置路径
  28. '~': path.resolve(__dirname, './'),
  29. // 设置别名
  30. '@': path.resolve(__dirname, './src')
  31. },
  32. // https://cn.vitejs.dev/config/#resolve-extensions
  33. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  34. },
  35. build: {
  36. rollupOptions: {
  37. input: {
  38. index: path.resolve(__dirname, 'index.html'),
  39. datav: path.resolve(__dirname, 'datav.html')
  40. }
  41. }
  42. },
  43. // 开发服务器依赖预构建:提前打包"业务页面/表单常用、但 main.js 未全局引入"的第三方库,
  44. // 避免切到懒加载页面时 Vite 现场"发现新依赖 -> 重新预构建 -> 整页刷新"(会误触发路由 onError 的版本更新刷新)。
  45. // 仅影响 dev(vite serve);Vite 3 build 阶段默认禁用依赖优化器,生产打包产物不受影响。
  46. // 新增页面若仍偶发首次刷新,把该页面用到的库补进本列表即可。
  47. optimizeDeps: {
  48. include: [
  49. 'xe-utils',
  50. 'lodash.pick',
  51. 'lodash.omit',
  52. 'dayjs',
  53. 'echarts',
  54. 'vue-echarts',
  55. 'sortablejs',
  56. 'cropperjs',
  57. '@wangeditor/editor',
  58. '@wangeditor/editor-for-vue'
  59. ]
  60. },
  61. // vite 相关配置
  62. server: {
  63. port: 2800,
  64. host: true,
  65. open: true,
  66. proxy: {
  67. '/api': {
  68. target: VITE_APP_BASE,
  69. changeOrigin: true,
  70. rewrite: (p) => p.replace(/^\/api/, '')
  71. },
  72. '/file-server': {
  73. target: VITE_APP_BASE,
  74. changeOrigin: true,
  75. rewrite: (p) => p.replace(/^\/file-server/, /file-server/)
  76. }
  77. },
  78. },
  79. css: {
  80. postcss: {
  81. plugins: [
  82. {
  83. postcssPlugin: 'internal:charset-removal',
  84. AtRule: {
  85. charset: (atRule) => {
  86. if (atRule.name === 'charset') {
  87. atRule.remove();
  88. }
  89. }
  90. }
  91. }
  92. ],
  93. },
  94. },
  95. })
  96. }