
- 发布:2022-12-23 15:19
- 更新:2022-12-23 15:27
- 阅读:417
产品分类: uniapp/App
PC开发环境操作系统: Mac
PC开发环境操作系统版本号: 13.1 (22C65)
HBuilderX类型: 正式
HBuilderX版本号: 3.6.14
手机系统: 全部
手机厂商: 华为
页面类型: vue
vue版本: vue3
打包方式: 离线
项目创建方式: HBuilderX
测试过的手机:
操作步骤:
import { defineConfig } from 'vite';
import uni from '@dcloudio/vite-plugin-uni';
const fs = require('fs');
const manifestPath = process.env.UNI_INPUT_DIR + '/manifest.json';
let Manifest = fs.readFileSync(manifestPath, { encoding: 'utf-8' });
function getCurrenVersion(){
let ManifestArr = Manifest.split(/\n/);
for (let index = 0; index < ManifestArr.length; index++) {
const item = ManifestArr[index];
if(item.includes("versionCode")){
return item
}
}
console.log("版本号code:",currentVersionCode)
}
function replaceManifest(path, value) {
const arr = path.split('.');
const len = arr.length;
const lastItem = arr[len - 1];
let i = 0;
let ManifestArr = Manifest.split(/\n/);
for (let index = 0; index < ManifestArr.length; index++) {
const item = ManifestArr[index];
if (new RegExp(`"${arr[i]}"`).test(item)) ++i;
if (i === len) {
const hasComma = /,/.test(item);
ManifestArr[index] = item.replace(
new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`),
`"${lastItem}": ${value}${hasComma ? ',' : ''}`
);
break;
}
}
Manifest = ManifestArr.join('\n');
}
export default defineConfig(({mode})=>{
if(mode=='production'){
let currentVersionCode = getCurrenVersion()
const handleVersion = currentVersionCode.match(/\d+/)[0]
let updateVersion = parseInt(handleVersion) + 1
let updateVersionName = String(updateVersion).split('').join('.')
replaceManifest('versionName',`"${updateVersionName}"`);
replaceManifest('versionCode', updateVersion);
fs.writeFileSync(manifestPath, Manifest, {
flag: 'w',
});
console.log("currentName==",updateVersionName)
console.log("currentCode==",updateVersion)
console.log("hello world")
}
return {
plugins: [uni()],
}
})
import { defineConfig } from 'vite';
import uni from '@dcloudio/vite-plugin-uni';
const fs = require('fs');
const manifestPath = process.env.UNI_INPUT_DIR + '/manifest.json';
let Manifest = fs.readFileSync(manifestPath, { encoding: 'utf-8' });
function getCurrenVersion(){
let ManifestArr = Manifest.split(/\n/);
for (let index = 0; index < ManifestArr.length; index++) {
const item = ManifestArr[index];
if(item.includes("versionCode")){
return item
}
}
console.log("版本号code:",currentVersionCode)
}
function replaceManifest(path, value) {
const arr = path.split('.');
const len = arr.length;
const lastItem = arr[len - 1];
let i = 0;
let ManifestArr = Manifest.split(/\n/);
for (let index = 0; index < ManifestArr.length; index++) {
const item = ManifestArr[index];
if (new RegExp(`"${arr[i]}"`).test(item)) ++i;
if (i === len) {
const hasComma = /,/.test(item);
ManifestArr[index] = item.replace(
new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`),
`"${lastItem}": ${value}${hasComma ? ',' : ''}`
);
break;
}
}
Manifest = ManifestArr.join('\n');
}
export default defineConfig(({mode})=>{
if(mode=='production'){
let currentVersionCode = getCurrenVersion()
const handleVersion = currentVersionCode.match(/\d+/)[0]
let updateVersion = parseInt(handleVersion) + 1
let updateVersionName = String(updateVersion).split('').join('.')
replaceManifest('versionName',`"${updateVersionName}"`);
replaceManifest('versionCode', updateVersion);
fs.writeFileSync(manifestPath, Manifest, {
flag: 'w',
});
console.log("currentName==",updateVersionName)
console.log("currentCode==",updateVersion)
console.log("hello world")
}
return {
plugins: [uni()],
}
})
预期结果:
预期只更新一个版本
预期只更新一个版本
实际结果:
实际执行了两次,版本上升了两个
实际执行了两次,版本上升了两个
bug描述:
出现的场景是:在打包时想自动更新manifest.json的版本号,在app端编译时会更新一次版本,构建时又执行更新一次版本,一次会更新两个版本号。如最初版本号为:versionName:'1.0.0' versionCode:100, 编译构建后会变成 versionName: '1.0.2' versionCode:102。
在vite.config.js文件中查看发现,是文件的脚本执行了两次,编译时执行了一次,构建时执行了一次。H5端只执行了一次。
