本文目录导读:

可以创建一个自动清理VuePress缓存的脚本,以下是几个不同场景的解决方案:
Shell脚本(Linux/Mac)
创建 clean-vuepress.sh 文件:
#!/bin/bash
echo "🧹 开始清理 VuePress 缓存..."
# 清理 .temp 目录
if [ -d ".temp" ]; then
rm -rf .temp
echo "✅ 已清理 .temp 目录"
else
echo "ℹ️ .temp 目录不存在,跳过"
fi
# 清理 .cache 目录
if [ -d ".cache" ]; then
rm -rf .cache
echo "✅ 已清理 .cache 目录"
else
echo "ℹ️ .cache 目录不存在,跳过"
fi
# 清理 dist 目录(可选)
if [ -d "dist" ]; then
rm -rf dist
echo "✅ 已清理 dist 目录"
else
echo "ℹ️ dist 目录不存在,跳过"
fi
# 清理 node_modules/.cache(可选)
if [ -d "node_modules/.cache" ]; then
rm -rf node_modules/.cache
echo "✅ 已清理 node_modules/.cache 目录"
fi
echo "🎉 清理完成!"
# 询问是否重新安装依赖
read -p "是否重新安装依赖?(y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
npm install
fi
Windows批处理脚本
创建 clean-vuepress.bat 文件:
@echo off
echo 🧹 开始清理 VuePress 缓存...
:: 清理 .temp 目录
if exist ".temp" (
rmdir /s /q ".temp"
echo ✅ 已清理 .temp 目录
) else (
echo ℹ️ .temp 目录不存在,跳过
)
:: 清理 .cache 目录
if exist ".cache" (
rmdir /s /q ".cache"
echo ✅ 已清理 .cache 目录
) else (
echo ℹ️ .cache 目录不存在,跳过
)
:: 清理 dist 目录(可选)
if exist "dist" (
rmdir /s /q "dist"
echo ✅ 已清理 dist 目录
) else (
echo ℹ️ dist 目录不存在,跳过
)
:: 清理 node_modules/.cache(可选)
if exist "node_modules\.cache" (
rmdir /s /q "node_modules\.cache"
echo ✅ 已清理 node_modules/.cache 目录
)
echo 🎉 清理完成!
:: 询问是否重新安装依赖
set /p choice="是否重新安装依赖?(y/n): "
if /i "%choice%"=="y" (
npm install
)
pause
Node.js 脚本(跨平台)
创建 clean-cache.js 文件:
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const dirsToClean = ['.temp', '.cache', 'dist', 'node_modules/.cache'];
console.log('🧹 开始清理 VuePress 缓存...\n');
dirsToClean.forEach((dir) => {
const fullPath = path.join(process.cwd(), dir);
if (fs.existsSync(fullPath)) {
try {
fs.rmSync(fullPath, { recursive: true, force: true });
console.log(`✅ 已清理 ${dir} 目录`);
} catch (error) {
console.error(`❌ 清理 ${dir} 失败:`, error.message);
}
} else {
console.log(`ℹ️ ${dir} 目录不存在,跳过`);
}
});
console.log('\n🎉 清理完成!\n');
// 询问是否重新安装依赖
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
readline.question('是否重新安装依赖?(y/n): ', (answer) => {
if (answer.toLowerCase() === 'y') {
console.log('\n📦 正在重新安装依赖...');
try {
execSync('npm install', { stdio: 'inherit' });
console.log('✅ 依赖安装完成!');
} catch (error) {
console.error('❌ 依赖安装失败:', error.message);
}
} else {
console.log('ℹ️ 跳过依赖安装');
}
readline.close();
});
package.json 脚本
在 package.json 中添加脚本:
{
"scripts": {
"clean": "node clean-cache.js",
"clean:all": "rm -rf .temp .cache dist node_modules/.cache && npm install",
"clean:cache": "rm -rf .temp .cache",
"clean:dist": "rm -rf dist",
"rebuild": "npm run clean:all && npm run build"
}
}
自动化清理(开发时监听)
创建 auto-clean.js 文件,在文件变化时自动清理:
const chokidar = require('chokidar');
const fs = require('fs');
const path = require('path');
// 需要清理的目录
const cacheDirs = ['.temp', '.cache'];
function cleanCache() {
console.log('🔄 检测到文件变化,清理缓存...');
cacheDirs.forEach((dir) => {
const fullPath = path.join(process.cwd(), dir);
if (fs.existsSync(fullPath)) {
fs.rmSync(fullPath, { recursive: true, force: true });
console.log(`✅ 已清理 ${dir}`);
}
});
console.log('✅ 缓存清理完成\n');
}
// 监听 src 目录下的文件变化
console.log('👀 开始监听文件变化...');
const watcher = chokidar.watch('src/**/*', {
ignored: /(^|[\/\\])\../,
persistent: true,
ignoreInitial: true
});
watcher
.on('change', cleanCache)
.on('add', cleanCache)
.on('unlink', cleanCache);
使用说明
-
Linux/Mac用户:
chmod +x clean-vuepress.sh ./clean-vuepress.sh
-
Windows用户:
clean-vuepress.bat
-
Node.js脚本:
node clean-cache.js
-
npm脚本:
npm run clean npm run rebuild
建议
- 将清理脚本放在项目根目录
- 在
.gitignore中添加.temp、.cache、dist目录 - 开发时遇到奇怪的问题,可以先运行清理脚本
- 可以创建
clean-and-dev脚本组合清理和启动开发服务器
选择最适合你需求的方案即可!