// 可以通过修改Vue3 源码的方式添加删除缓存key以及获取所有缓存信息
// 删除keepalive缓存key 缓存以vue-router的fullPath作为keep-alive的缓存key
// existTabPathList 缓存存续路由全路径列表
KeepAliveInstance.value?.$getCache && Array.from(KeepAliveInstance.value.$getCache().keys()).map(key => {
!existTabPathList.includes(key) && KeepAliveInstance.value.$pruneCacheEntry(key);
})
const path = require("path");
const fs = require(`fs`);
try {
const vue_bundler_file = path.resolve(
__dirname,
"node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js"
);
//使用同步读取文件
const data = fs.readFileSync(vue_bundler_file, "utf8");
//如果未添加过
if (data.indexOf("sharedContext.$pruneCacheEntry") < 0) {
console.log("正在修改源码文件:", vue_bundler_file);
//先找到__v_cache变量的位置
let index = data.indexOf("__v_cache");
if (index >= 0) {
// 继续找下一个大括号 }
index = data.indexOf("}", index);
if (index >= 0) {
//从下一个位置开始
index += 1;
//然后放一个可以释放的函数
const remove = " sharedContext.$pruneCacheEntry = (key) => cache.get(key) && pruneCacheEntry(key);";
const readCache = " sharedContext.$getCache = () => cache;";
//然后拼接
const result =
data.substring(0, index) +
"\r\n" +
remove +
"\r\n" +
readCache +
"\r\n" +
data.substring(index);
fs.writeFileSync(vue_bundler_file, result, "utf8");
}
}
}
} catch (er) {
console.error(er.message);
}
// Vue启动或者打包设置 Vue库指向
config.resolve.alias = {
'vue$': 'vue/dist/vue.esm-bundler.js',
vue: resolve(`/node_modules/vue`)
};
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论