PROJECT-SIZE-OPTIMIZATION.md 5.4 KB

项目大小优化总结

📅 完成时间

2025-11-02

🎯 问题描述

项目文件夹占用了80GB的磁盘空间,严重影响开发效率和磁盘使用。

🔍 问题分析

通过检查各文件夹大小,发现问题根源:

文件夹 大小 说明
.angular 79.84 GB ❌ Angular构建缓存(异常巨大)
node_modules 0.67 GB ✅ 正常大小
dist 0.02 GB ✅ 构建输出
src 0.01 GB ✅ 源代码

问题原因: .angular文件夹是Angular CLI的构建缓存目录,通常应该只有几百MB,但由于长期开发和频繁构建,缓存文件累积到了近80GB。

✅ 解决方案

1. 删除.angular缓存文件夹

cd yss-project
Remove-Item -Path ".angular" -Recurse -Force

结果:

  • 删除前: 80 GB
  • 删除后: 0.7 GB
  • 节省空间: 79.3 GB (99.1%)

2. 添加到.gitignore

确保.angular文件夹不会被提交到Git:

# Angular cache
.angular/

📊 优化后的项目结构

yss-project/
├── node_modules/     0.67 GB  (依赖包)
├── dist/             0.02 GB  (构建输出)
├── src/              0.01 GB  (源代码)
├── .angular/         [已删除] (构建缓存)
└── 其他文件          < 0.01 GB
─────────────────────────────────
总计:                 ~0.7 GB

🛡️ 预防措施

1. 定期清理缓存

每月清理一次:

# Windows PowerShell
cd yss-project
Remove-Item -Path ".angular" -Recurse -Force

# 或使用 npm 脚本
npm run clean

2. 添加清理脚本到package.json

{
  "scripts": {
    "clean": "rimraf .angular dist",
    "clean:cache": "rimraf .angular",
    "clean:build": "rimraf dist",
    "clean:all": "rimraf .angular dist node_modules"
  }
}

3. 监控项目大小

创建一个快速检查脚本 check-size.ps1:

# check-size.ps1
$folders = @('.angular', 'node_modules', 'dist')
foreach ($folder in $folders) {
    if (Test-Path $folder) {
        $size = (Get-ChildItem $folder -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB
        Write-Host "$folder : $([math]::Round($size, 2)) GB"
    }
}

📝 其他可清理的文件

1. 构建输出

Remove-Item -Path "dist" -Recurse -Force

2. 测试覆盖率报告

Remove-Item -Path "coverage" -Recurse -Force

3. 临时文件

Remove-Item -Path "*.tmp", "*.log" -Force

🚀 性能优化建议

1. 配置Angular构建缓存限制

angular.json中配置缓存大小限制:

{
  "cli": {
    "cache": {
      "enabled": true,
      "path": ".angular/cache",
      "environment": "all"
    }
  }
}

2. 使用增量构建

ng build --configuration production --build-optimizer

3. 定期更新依赖

# 检查过时的包
npm outdated

# 更新依赖
npm update

# 清理未使用的包
npm prune

⚠️ 注意事项

可以安全删除的文件夹

  • .angular/ - 构建缓存(会自动重新生成)
  • dist/ - 构建输出(可重新构建)
  • coverage/ - 测试覆盖率报告
  • .cache/ - 各种缓存文件

不要删除的文件夹

  • node_modules/ - 除非你要重新安装依赖
  • src/ - 源代码
  • .git/ - Git版本控制
  • docs/ - 文档
  • rules/ - 业务规则

📈 长期维护建议

1. 每周检查

# 检查 .angular 文件夹大小
$size = (Get-ChildItem .angular -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB
if ($size -gt 5) {
    Write-Host "警告: .angular 文件夹已超过 5GB,建议清理"
}

2. 每月清理

  • 删除.angular缓存
  • 清理dist构建输出
  • 检查并删除临时文件

3. 每季度审查

  • 审查node_modules依赖
  • 删除未使用的包
  • 更新过时的依赖

🎯 最佳实践

1. 开发环境

# 开发时使用缓存加速构建
ng serve

# 定期清理缓存
npm run clean:cache

2. 生产构建

# 构建前清理
npm run clean

# 生产构建
ng build --configuration production

3. CI/CD环境

# .gitlab-ci.yml 或 .github/workflows/build.yml
before_script:
  - rm -rf .angular dist
  - npm ci

build:
  script:
    - ng build --configuration production

📚 相关资源

✅ 验证清理结果

运行以下命令验证清理效果:

# 检查项目总大小
cd yss-project
$totalSize = (Get-ChildItem -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB
Write-Host "项目总大小: $([math]::Round($totalSize, 2)) GB"

# 检查各文件夹大小
Get-ChildItem -Directory | ForEach-Object {
    $size = (Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB
    "$($_.Name): $([math]::Round($size, 2)) GB"
}

🎉 总结

  • 问题已解决: 项目大小从80GB降至0.7GB
  • 节省空间: 79.3GB (99.1%)
  • 性能提升: 构建速度更快,磁盘IO更少
  • 维护建议: 定期清理,监控大小

优化完成时间: 2025-11-02
优化效果: 节省 79.3 GB 磁盘空间
状态: ✅ 已完成