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。
.angular缓存文件夹cd yss-project
Remove-Item -Path ".angular" -Recurse -Force
结果:
.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
每月清理一次:
# Windows PowerShell
cd yss-project
Remove-Item -Path ".angular" -Recurse -Force
# 或使用 npm 脚本
npm run clean
package.json{
"scripts": {
"clean": "rimraf .angular dist",
"clean:cache": "rimraf .angular",
"clean:build": "rimraf dist",
"clean:all": "rimraf .angular dist node_modules"
}
}
创建一个快速检查脚本 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"
}
}
Remove-Item -Path "dist" -Recurse -Force
Remove-Item -Path "coverage" -Recurse -Force
Remove-Item -Path "*.tmp", "*.log" -Force
在angular.json中配置缓存大小限制:
{
"cli": {
"cache": {
"enabled": true,
"path": ".angular/cache",
"environment": "all"
}
}
}
ng build --configuration production --build-optimizer
# 检查过时的包
npm outdated
# 更新依赖
npm update
# 清理未使用的包
npm prune
.angular/ - 构建缓存(会自动重新生成)dist/ - 构建输出(可重新构建)coverage/ - 测试覆盖率报告.cache/ - 各种缓存文件node_modules/ - 除非你要重新安装依赖src/ - 源代码.git/ - Git版本控制docs/ - 文档rules/ - 业务规则# 检查 .angular 文件夹大小
$size = (Get-ChildItem .angular -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB
if ($size -gt 5) {
Write-Host "警告: .angular 文件夹已超过 5GB,建议清理"
}
.angular缓存dist构建输出node_modules依赖# 开发时使用缓存加速构建
ng serve
# 定期清理缓存
npm run clean:cache
# 构建前清理
npm run clean
# 生产构建
ng build --configuration production
# .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"
}
优化完成时间: 2025-11-02
优化效果: 节省 79.3 GB 磁盘空间
状态: ✅ 已完成