versionUpdatePlugin.js
const fs = require('fs')
const path = require('path')
const writeVersion = (versionFile, content) => {
fs.writeFile(versionFile, content, (err) => {
if (err) throw err
})
}
export default (options) => {
let config
return {
name: 'version-update',
configResolved(resolvedConfig) {
config = resolvedConfig
},
buildStart() {
const file = config.publicDir + path.sep + 'version.json'
const content = JSON.stringify({ version: options.version })
if (fs.existsSync(config.publicDir)) {
writeVersion(file, content)
} else {
fs.mkdir(config.publicDir, (err) => {
if (err) throw err
writeVersion(file, content)
})
}
},
}
}
配置vite
import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import svgLoader from "vite-svg-loader";
import vue from "@vitejs/plugin-vue";
import viteCompression from 'vite-plugin-compression'
import versionUpdatePlugin from './versionUpdatePlugin.js'
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
const path = require("path");
const version = new Date().getTime()
export default defineConfig(
({mode})=>{
return {
define: {
__APP_VERSION__: version,
},
esbuild: {
drop: ["console", "debugger"],
},
plugins: [
vue(),
svgLoader(),
createSvgIconsPlugin({
iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
symbolId: "icon-[name]",
}),
viteCompression({
verbose: true,
disable: false,
deleteOriginFile: false,
threshold: 5120,
algorithm: 'gzip',
ext: '.gz'
}),
versionUpdatePlugin({
version: version
}),
],
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
},
},
base: "/",
server: {
host: "0.0.0.0",
strictPort: false,
port: 6173,
proxy: {
"/base_api": {
target: "https://sit-co-server.exchangs.top",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/base_api/, ""),
},
"/report_api": {
target: 'https://uat-report-server.exchangs.top',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/push_api/, ""),
},
"/push_api": {
target: 'https://sit-push-server.exchangs.top',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/push_api/, ""),
},
"/order_api": {
target: "https://sit-settle-center.exchangs.top",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/order_api/, ""),
},
"/orderbus_api": {
target: 'https://sit-order-bus.exchangs.top',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/orderbus_api/, ""),
},
"/advance_api": {
target: 'https://sit-advance-settle.exchangs.top',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/advance_api/, ""),
},
"/cojob_api": {
target: 'https://sit-co-jobs.exchangs.top',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/cojob_api/, ""),
},
"/market_api": {
target: 'https://sit-market-server.exchangs.top/',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/market_api/, ""),
},
},
},
build: {
sourcemap: !mode === 'production',
minify: true,
reportCompressedSize: false,
cssCodeSplit: true,
rollupOptions: {
output: {
chunkFileNames: 'js/[name]-[hash].js',
entryFileNames: 'js/[name]-[hash].js',
assetFileNames: '[ext]/[name]-[hash].[ext]',
manualChunks(id) {
if (id.includes("node_modules")) {
if (id.includes('vue-router')) {
return 'vue';
}
else{
return id
.toString()
.split("node_modules/")[1]
.split("/")[0]
.toString();
}
}
},
}
}
}
}
}
);
路由配置生效
const debounceVersionUpdate = _.debounce(()=>{
showUpdateInfo()
},1000,{
leading: true,
})
const showUpdateInfo = ()=>{
console.log('showUpdateInfo')
const info = window.localStorage.getItem('lang') === 'zhHK' ? '发现新版本,自动更新中...' : 'Discovering new version, automatically updating...'
message.info(`${info}`,2).then(()=>{
window.location.reload(true);
})
}
router.beforeEach(async (to, from, next) => {
let response = null
response = await axios.get(`/version.json`,{
headers: {
'Cache-Control': 'no-cache'
}
})
if (process.env.NODE_ENV === "development"||(process.env.NODE_ENV !== "development"&&__APP_VERSION__ == response.data.version)) {
if (to.path !== '/Login' && !localStorage.getItem('token')) {
next({path: '/Login', replace: true})
} else {
if (to.path !== from.path) {
resetTableData()
resetPage()
}
if (to.path !== '/' && to.path !== '/Login') {
useIsPermission()
const { addTab } = useNavTab()
const systemStore = useSystemStore()
const { routeData, menuMap } = storeToRefs(systemStore)
for (const key in menuMap) {
const element = menuMap[key];
if (element&&element.children&&element.children.length>0) {
element.children.forEach(child=>{
menuMap[child.path] = child
})
}
}
const { initRouteData } = systemStore
if (!routeData.value || routeData.value.length === 0) {
await initRouteData()
next({path: to.path, query: to.query, replace: true})
} else {
next()
}
addTab({
name: (menuMap[to.path] || {}).title,
path: to.path,
query: to.query,
fixed: false
})
} else {
next()
}
}
}else{
debounceVersionUpdate()
console.log('debounceVersionUpdate')
next()
}
})
export default router