You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vite构建为依赖生成SourceMap致内存溢出问题求助

解决Vite构建时依赖包SourceMap导致的内存溢出与解析错误问题

方案1:排除指定依赖的SourceMap处理

修改vite.config.ts,通过build.sourcemap的exclude选项跳过对antd这类依赖的SourceMap处理,既解决解析报错又减少内存占用:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  build: {
    sourcemap: {
      enabled: true, // 保留项目自身的SourceMap生成
      exclude: ['antd', '@ant-design/icons', 'rc-*'] // 排除antd及相关依赖
    }
  }
});

方案2:仅生成项目源码的SourceMap

如果完全不需要依赖包的SourceMap,直接关闭依赖预构建的SourceMap,并设置项目SourceMap类型:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  build: {
    sourcemap: 'hidden', // 生成SourceMap但不关联到产物,仅用于调试
    optimizeDeps: {
      sourcemap: false // 禁用依赖预构建的SourceMap生成
    }
  }
});

方案3:提升Node.js内存限制

如果必须处理依赖的SourceMap,通过给Node.js分配更多内存来解决溢出问题,修改package.json的build脚本:

{
  "scripts": {
    "build": "node --max-old-space-size=8192 node_modules/vite/bin/vite.js build"
  }
}

这里分配了8GB内存,可根据机器配置调整数值(比如4092对应4GB)。

额外建议

  • 检查antd版本,升级到最新稳定版,部分旧版antd的ES模块产物存在SourceMap缺失问题,升级后可能自动解决解析报错。
  • 确认tsconfig.json里的sourceMap选项仅针对项目源码,不要影响依赖解析。

内容的提问来源于stack exchange,提问作者acanimal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 07:22:11