in-fix-localstorage
v1.0.0
Published
火狐浏览器localStorage溢出报错问题
Downloads
4
Readme
Install
npm install in-fix-localstorage --save
Usage
vue-cli3.0
/**
* main.js ⚠️注意:需要在使用 localstorage 方法的文件之前引入
*/
import "in-fix-localstorage"
/**
* vue.config.js
*/
config.module
.rule('firefox')
.test(/\.js|\.vue$/) // 匹配.js | .vue文件
.exclude.add(path.resolve("node_modules/[email protected]@in-fix-localstorage")) // 过滤掉当前插件
.end()
.use("in-fix-localstorage")
.loader("in-fix-localstorage/loader")
.tap((options = {}) =>
Object.assign(options, {
fnName: "window.fixLocalStorage"
})
)
.end();
Explain
说明:文件中可正常使用 localstorage 方法,经过 loader 编译后的内容为 windwo.fixLocalStorage 匹配规则:
// 按window对象调用的关键字 匹配
const windowReg = /\bwindow\.localStorage\b/g;
// 按调用方法的关键字 匹配
const funcReg = /\b(localStorage)\.(setItem|getItem|removeItem|clear)\b/g;
source = source.replace(windowReg, `${fnName}`); // warn! 需要优先对window形式调用的进行替换
source = source.replace(funcReg, `${fnName}.$2`);
for example:
/**
* views/login.vue
*/
localstorage.setItem('temp', 1);
window.localstorage.removeItem('temp');
/**
* dist/js/login.hash.js
*/
window.fixLocalstorage.setItem('temp', 1)
window.fixLocalstorage.removeItem('temp');