本文目录导读:

Linux/macOS 版本 (Bash)
#!/bin/bash
# Docusaurus 缓存清理脚本
# 适用于 Linux 和 macOS
echo "🧹 开始清理 Docusaurus 缓存..."
# 清理 .docusaurus 目录
if [ -d ".docusaurus" ]; then
rm -rf .docusaurus
echo "✅ 已删除 .docusaurus 目录"
else
echo "⚠️ .docusaurus 目录不存在"
fi
# 清理 node_modules/.cache
if [ -d "node_modules/.cache" ]; then
rm -rf node_modules/.cache
echo "✅ 已删除 node_modules/.cache"
else
echo "⚠️ node_modules/.cache 目录不存在"
fi
# 清理 build 目录(可选)
if [ -d "build" ]; then
rm -rf build
echo "✅ 已删除 build 目录"
fi
# 可选:清理 node_modules 重新安装(谨慎使用)
# if [ -d "node_modules" ]; then
# rm -rf node_modules
# echo "✅ 已删除 node_modules"
# fi
echo "🎉 缓存清理完成!"
echo "💡 提示:运行 'npm install' 重新安装依赖"
Windows 版本 (PowerShell)
# Docusaurus 缓存清理脚本
# 适用于 Windows PowerShell
Write-Host "🧹 开始清理 Docusaurus 缓存..." -ForegroundColor Cyan
# 清理 .docusaurus 目录
if (Test-Path ".docusaurus") {
Remove-Item -Recurse -Force ".docusaurus"
Write-Host "✅ 已删除 .docusaurus 目录" -ForegroundColor Green
} else {
Write-Host "⚠️ .docusaurus 目录不存在" -ForegroundColor Yellow
}
# 清理 node_modules/.cache
if (Test-Path "node_modules/.cache") {
Remove-Item -Recurse -Force "node_modules/.cache"
Write-Host "✅ 已删除 node_modules/.cache" -ForegroundColor Green
} else {
Write-Host "⚠️ node_modules/.cache 目录不存在" -ForegroundColor Yellow
}
# 清理 build 目录(可选)
if (Test-Path "build") {
Remove-Item -Recurse -Force "build"
Write-Host "✅ 已删除 build 目录" -ForegroundColor Green
}
# 可选:清理 node_modules 重新安装(谨慎使用)
# if (Test-Path "node_modules") {
# Remove-Item -Recurse -Force "node_modules"
# Write-Host "✅ 已删除 node_modules" -ForegroundColor Green
# }
Write-Host "🎉 缓存清理完成!" -ForegroundColor Cyan
Write-Host "💡 提示:运行 'npm install' 重新安装依赖" -ForegroundColor Yellow
Windows 版本 (CMD/Batch)
@echo off
echo 🧹 开始清理 Docusaurus 缓存...
:: 清理 .docusaurus 目录
if exist ".docusaurus" (
rmdir /s /q ".docusaurus"
echo ✅ 已删除 .docusaurus 目录
) else (
echo ⚠️ .docusaurus 目录不存在
)
:: 清理 node_modules/.cache
if exist "node_modules\.cache" (
rmdir /s /q "node_modules\.cache"
echo ✅ 已删除 node_modules/.cache
) else (
echo ⚠️ node_modules/.cache 目录不存在
)
:: 清理 build 目录(可选)
if exist "build" (
rmdir /s /q "build"
echo ✅ 已删除 build 目录
)
echo 🎉 缓存清理完成!
echo 💡 提示:运行 'npm install' 重新安装依赖
pause
使用说明
-
保存脚本:
- Linux/macOS:保存为
clean-cache.sh - Windows:保存为
clean-cache.ps1或clean-cache.bat
- Linux/macOS:保存为
-
赋予执行权限(Linux/macOS):
chmod +x clean-cache.sh
-
运行脚本:
- Linux/macOS:
./clean-cache.sh - Windows PowerShell:
.\clean-cache.ps1 - Windows CMD:双击运行
clean-cache.bat
- Linux/macOS:
-
运行后重建:
npm install npm run build
自动化集成
添加到 package.json 脚本
{
"scripts": {
"clear": "bash clean-cache.sh",
"rebuild": "npm run clear && npm install && npm run build"
}
}
这样你就可以使用 npm run clear 或 npm run rebuild 一键完成清理和重建。