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

Deno Deploy构建产物异常过大(超100MB)控制方法咨询

问题

用Deno+Vite+React部署到Deno Deploy时,遇到构建产物体积异常膨胀的问题:从最初约101MB涨到150MB,但实际业务资源文件总大小还不到1MB。已经排除node_modules的影响(删除该文件夹后产物仍有101MB),甚至新建极简分支后,产物体积反而跳到150MB。需要解决Deno Deploy构建产物过大的问题。

构建日志

Executing build command
Task  deno task build && rm -rf node_modules
Task build deno run -A --node-modules-dir npm:vite build
vite v6.3.5 building for production...
Generated route tree in 81ms
transforming...
✓ 955 modules transformed.
rendering chunks...
computing gzip size...
ist/assets/index-DRFX6L5t.js            377.20 kB │ gzip: 117.28 kB
dist/assets/Editor-CoIp3zDK.js   1,236.99 kB │ gzip: 415.69 kB
(!) Some chunks are larger than 500 kB after minification. Consider:
- Using dynamic import() to code-split the application
- Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/configuration-options/#output-manualchunks
- Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.
✓ built in 5.82s
Caching dependencies for the entrypoint
Preparing build artifact
[===============================================|            ] 15700/19393  80%
[===========================================================-] 19393/19393 100%
Uploading build artifact (101 MB)
Build finished

配置文件

deno.json

{
  "nodeModulesDir": "auto",
  "tasks": {
    "install": "deno install",
    "dev": "deno run -A --node-modules-dir npm:vite",
    "build": "deno run -A --node-modules-dir npm:vite build",
    "analyze": "deno run -A --node-modules-dir npm:vite build --mode analyze",
    "preview": "deno run -A --node-modules-dir npm:vite preview",
    "serve": "deno run --allow-net --allow-read jsr:@std/http@1/file-server dist/",
    "start": "deno run --allow-net --allow-read --allow-env --env main.ts"
  },
  "compilerOptions": {
    "lib": ["ES2021", "DOM", "DOM.Iterable"],
    "jsx": "react-jsx",
    "jsxImportSource": "react",
    "jsxImportSourceTypes": "@types/react"
  },
}

vite.config.ts

import { defineConfig } from "vite";
import deno from "@deno/vite-plugin";
import react from "@vitejs/plugin-react-swc";
import { tanstackRouter } from "@tanstack/router-plugin/vite";

export default defineConfig({
  plugins: [
    deno(),
    tanstackRouter({
      target: "react",
      autoCodeSplitting: true,
    }),
    react(),
  ],
});

解决方案

1. 清理项目根目录的冗余文件

Deno Deploy默认会打包根目录下所有未被.gitignore排除的文件,旧构建产物、缓存目录、临时文件都会被算进体积:

  • 检查根目录,把.deno、旧dist文件夹、日志文件等加入.gitignore;
  • 修改构建命令,在打包后清理无关文件,比如:
    deno task build && rm -rf node_modules .deno dist-backup
    

2. 优化Vite构建输出配置

确保Vite只生成必要的生产文件,避免冗余:

  • 在vite.config.ts中明确输出目录并开启清空:
    export default defineConfig({
      // ...其他配置
      build: {
        outDir: "dist",
        emptyOutDir: true, // 每次构建自动清空dist
        sourcemap: false, // 关闭sourcemap,避免额外体积
      },
    });
    

3. 调整Deno缓存策略

Deno的本地缓存目录(.deno)可能被误打包,通过配置隔离缓存:

  • 在deno.json中把缓存目录移到项目外:
    {
      "cacheDir": "../.deno-cache",
      "nodeModulesDir": "auto"
    }
    
  • 构建前强制刷新并清理缓存:
    deno cache --reload && deno task build && rm -rf node_modules .deno
    

4. 检查Deno Deploy控制台配置

在Deno Deploy项目设置里:

  • 确认构建上下文是项目根目录;
  • 在忽略路径里添加.deno、node_modules、*.log等无关文件。

5. 分析产物构成定位问题

用工具排查到底是什么文件在占体积:

  • 运行deno run -A --node-modules-dir npm:vite build --mode analyze,生成Vite构建分析报告,查看是否有意外包含的大依赖或文件;
  • 用du -sh *(Linux/macOS)或dir /s(Windows)查看根目录各文件夹大小,定位体积来源。

内容的提问来源于stack exchange,提问作者Arseniy-II

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 07:53:23