niucloud/uni-app/scripts/apply-router-base.cjs
wangchen14709853322 5ed9a0ba81 v2.0-beta-20260626
v2框架公测版测试流程请看v2.0-beta.md
2026-07-01 12:25:30 +08:00

36 lines
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* H5 产物注入站点 ID 动态 router base与 publish.cjs solve 一致)
*/
const fs = require('fs')
const path = require('path')
const ROUTER_MATCH = 'const match = location.href.match(/\\/wap\\/(\\d*)\\//);'
const ROUTER_REPLACE =
'router:{mode:"history",base: match ? `/wap/${match[1]}/` : "/wap/",assets:"assets",routerBase: match ? `/wap/${match[1]}/` : "/wap/"},darkmode'
function patchIndexJs(content) {
if (content.includes(ROUTER_MATCH)) return content
let next = ROUTER_MATCH + content
next = next.replace(/router:{(.*?)},darkmode/s, ROUTER_REPLACE)
return next
}
function applyRouterBase(assetsDir) {
if (!fs.existsSync(assetsDir)) return 0
let patched = 0
for (const file of fs.readdirSync(assetsDir)) {
if (!/^(index-)(\w{8})(\.js)$/.test(file)) continue
const fn = path.join(assetsDir, file)
const content = fs.readFileSync(fn, 'utf-8')
const next = patchIndexJs(content)
if (next !== content) {
fs.writeFileSync(fn, next, 'utf-8')
patched++
console.log(`[apply-router-base] patched assets/${file}`)
}
}
return patched
}
module.exports = { applyRouterBase, patchIndexJs, ROUTER_MATCH }