| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- const fs = require('fs');
- const path = require('path');
- const CACHE_DIR = path.join(__dirname, '../node_modules/.cache');
- console.log('🧹 清理 Webpack 缓存...');
- console.log(` 缓存路径: ${CACHE_DIR}`);
- if (fs.existsSync(CACHE_DIR)) {
- try {
- // 获取缓存目录大小信息(可选)
- let totalSize = 0;
- function getDirectorySize(dir) {
- const files = fs.readdirSync(dir, { withFileTypes: true });
- for (const file of files) {
- const filePath = path.join(dir, file.name);
- if (file.isDirectory()) {
- getDirectorySize(filePath);
- } else {
- try {
- const stats = fs.statSync(filePath);
- totalSize += stats.size;
- } catch (err) {
- // 忽略单个文件错误
- }
- }
- }
- }
-
- getDirectorySize(CACHE_DIR);
- const sizeMB = (totalSize / 1024 / 1024).toFixed(2);
- console.log(` 缓存大小: ${sizeMB} MB`);
-
- // 删除缓存目录
- fs.rmSync(CACHE_DIR, { recursive: true, force: true });
- console.log('✅ 缓存已清理');
- } catch (err) {
- console.error('❌ 清理失败:', err.message);
- process.exit(1);
- }
- } else {
- console.log('ℹ️ 没有缓存需要清理');
- }
|