1.自动更新版本号,创建buildVersion文件
let fs = require('fs')
const getPackageJson = () => {
let data = fs.readFileSync('./package.json') //fs读取文件
return JSON.parse(data) //转换为json对象
}
let packageData = getPackageJson()
const updateVersion = () => {
let version = packageData.version.split(".");
version[version.length - 1]++;
packageData.version = version.join(".");
}
updateVersion()
fs.writeFile('./package.json', JSON.stringify(packageData, null, '\t'), err => {
if (err) {
console.log('写入失败', err)
} else {
console.log('写入成功 ' + packageData.version)
}
})
2.修改pakage.json中build的内容
node ./buildVersion.js && vue-cli-service build
3.main.js中加入代码,在localsession中存储pakage.json中version版本号,版本号不同则清除缓存
//解决部署项目时自动缓存
const VUE_APP_VERSION = require("../package.json").version;
const vers = window.localStorage.getItem("appVersion");
if (VUE_APP_VERSION != vers) {
localStorage.clear();
sessionStorage.clear();
window.localStorage.setItem("appVersion", VUE_APP_VERSION);
location.reload();
}