Files
beauty-miniapp-uni/scripts/patch-mp-weixin-vendor.js
T
2026-06-29 10:54:33 +08:00

36 lines
1.2 KiB
JavaScript

const fs = require('fs')
const path = require('path')
const vendorPath = path.join(__dirname, '..', 'unpackage', 'dist', 'dev', 'mp-weixin', 'common', 'vendor.js')
function patchMpWeixinVendor() {
if (!fs.existsSync(vendorPath)) {
return { ok: false, patched: false, reason: 'vendor.js not found' }
}
const s = fs.readFileSync(vendorPath, 'utf8')
const from = 'this.$vm.$mp.query = query; // 兼容 mpvue'
const toLegacy = 'if (this.$vm) {\\n if (!this.$vm.$mp) this.$vm.$mp = {};\\n this.$vm.$mp.query = query;\\n }'
const to = 'if (this.$vm) { if (!this.$vm.$mp) this.$vm.$mp = {}; this.$vm.$mp.query = query; }'
if (s.includes(to)) {
return { ok: true, patched: false, reason: 'already patched' }
}
if (s.includes(toLegacy)) {
fs.writeFileSync(vendorPath, s.replace(toLegacy, to), 'utf8')
return { ok: true, patched: true }
}
if (!s.includes(from)) {
return { ok: false, patched: false, reason: 'target snippet not found' }
}
fs.writeFileSync(vendorPath, s.replace(from, to), 'utf8')
return { ok: true, patched: true }
}
module.exports = patchMpWeixinVendor
if (require.main === module) {
patchMpWeixinVendor()
}