vue打包自动生成版本号

业务场景: 执行 npm run build 打包部署的时候自动生成版本号,方便部署后客户端提示更新。

1. 创建读取写入流文件

// 创建changeVersion.ts

let fs = require('fs')
//拿package的json数据
const getPackageJson = () => {
  let data = fs.readFileSync('./package.json') //fs读取文件
  return JSON.parse(data) //转换为json对象
}
let packageData = getPackageJson()
const getCurrentDate = () => {
  let time = new Date()
  let a = time.toLocaleDateString()

  let x = a.split('/')
  if (x[1].length < 2) {
    x[1] = '0' + x[1]
  }
  if (x[2].length < 2) {
    x[2] = '0' + x[2]
  }
  return x.join('')
}
const changeVersion = () => {
  //  getCurrentDate().substring(0, 6)   == 202305 ; 
  //  月份不相同时,直接更改。
  if (getCurrentDate().substring(0, 6) === String(packageData.version.split('.')[0])) {
    let x = Number(packageData.version.split('.')[2])
    x = x + 1
    let d = getCurrentDate().substring(0, 6) + '.' + getCurrentDate().substring(6);
    packageData.version = `${d}.${x}`
  } else {
    let d = getCurrentDate().substring(0, 6) + '.' + getCurrentDate().substring(6);
    packageData.version = `${d}.1`
  }
}
changeVersion()
fs.writeFile(
  './package.json',
  JSON.stringify(packageData, null, '\t'),
  (err) => {
    if (err) {
      console.log('写入失败', err)
    } else {
      console.log('版本号写入成功',packageData.version)
      //  packageData.version   --->   202305.11.7
    }
  }
)

2. 更改package.json文件

  • 更改build中的执行命令 node ./version/changeVersion.ts && node build/build.js
"name": "cmsv9",
"version": "202305.11.7",
"description": "cmsv9-cli",
"author": "hq",
"private": true,
"scripts": {
		"dev": "webpack-dev-server --inline --progress --public --config build/webpack.dev.conf.js",
		"start": "npm run dev",
		"install": "cnpm install",
		//   This!!!!!!
		"build": "node ./version/changeVersion.ts && node build/build.js",
		"analyz": "NODE_ENV=production npm_config_report=true npm run deploy:prod"
	},

3. 在version.js中使用版本号(在哪使用在哪引入)

import packageVersion from '../package.json'
const version = packageVersion.version;
export default { version }

4. 在App.vue中引入版本号,并处理缓存

import _v from '../version/version.js'

  created() {
    this.checkForUpdate()
  },
  methods: {
	
  //检查版本更新
  checkForUpdate() {
    let currentVersion = _v.version;
    if(currentVersion !== localStorage.getItem("version")){
      this.loading = true;
      this.$alert('检测到新版本,请刷新网页以加载新版本','更新提示', {
        confirmButtonText: '确定',
        showClose: false,
        callback: () => {
          // 更新本地缓存的版本
          window.sessionStorage.clear();
          window.localStorage.clear();
          window.location.reload(true);
          localStorage.setItem("version", currentVersion);
          this.loading = true;
        }
      })
    }
  }
}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尤山海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值