打包修改版本号清除缓存

prod
吕天方 8 months ago
parent acb600d4ad
commit 5dc404e879

@ -1,6 +1,6 @@
{
"name": "ruoyi",
"version": "3.8.7",
"version": "1.0.202405241500",
"description": "金鸡湖现代服务业品牌管理系统",
"author": "若依",
"license": "MIT",

@ -5,6 +5,9 @@ import Cookies from "js-cookie";
import Element from "element-ui";
import "./assets/styles/element-variables.scss";
// 引入package.json
import packageJson from '../package.json'
import "@/assets/styles/index.scss"; // global css
import "@/assets/styles/ruoyi.scss"; // ruoyi css
import App from "./App";
@ -71,6 +74,14 @@ Vue.use(plugins);
Vue.use(VueMeta);
DictData.install();
const version = packageJson.version
const localVersion = localStorage.getItem('projectVersion')
// 判断版本号是否一致
if (!localVersion || version !== localVersion) {
localStorage.clear() // 清除缓存
localStorage.setItem('projectVersion', version)
}
/**
* If you don't want to use mock-server
* you want to use MockJs for mock api

@ -0,0 +1,28 @@
const fs = require('fs')
const path = require('path')
const sep = path.sep
function VersionPlugin (options) {
this.options = options || {}
}
VersionPlugin.prototype.apply = function (compiler) {
var self = this
compiler.plugin('afterPlugins', function (params) { // 在 webpack 完成插件加载后执行
const packageJsonPath = path.join(params.context, sep + 'package.json') // 获取 package.json 文件路径
const dateStr = getDateStr() // 获取日期字符串
let packageJsonStr = fs.readFileSync(packageJsonPath, 'utf8') // 读取 package.json 文件内容
const r = new RegExp('(?<=version\\":\\s*\\")(.*)(?=")') // 构造正则表达式匹配 version 字段
packageJsonStr = packageJsonStr.replace(r, "1.0." + dateStr) // 更新 version 字段 dateStr是当天的日期没有秒获取到秒可能更好 ⌈ 自行判断是否需要 now.getSeconds() ⌋
fs.writeFileSync(packageJsonPath, packageJsonStr, 'utf8') // 将更新后的内容写回 package.json 文件
})
}
function getDateStr () {
const now = new Date()
return now.getFullYear() + format(now.getMonth() + 1) + format(now.getDate()) + format(now.getHours()) + format(now.getMinutes())
function format (num) {
return num < 10 ? '0' + num : '' + num
}
}
module.exports = VersionPlugin

@ -70,6 +70,10 @@ module.exports = {
],
},
chainWebpack(config) {
const VersionPlugin = require('./src/versionPlugin')
config.plugin('version').use(VersionPlugin).tap(args => {
return args
})
config.plugins.delete("preload"); // TODO: need test
config.plugins.delete("prefetch"); // TODO: need test

Loading…
Cancel
Save