Files
frontend-miniapp/scripts/verify-arrival-navigation-build.cjs
lyf e006333c0a
Some checks failed
CI / verify (push) Has been cancelled
修复 H5 来馆第三方导航构建边界
2026-07-12 00:30:18 +08:00

108 lines
3.7 KiB
JavaScript

const fs = require('node:fs')
const path = require('node:path')
const projectRoot = path.resolve(__dirname, '..')
const h5Root = path.resolve(projectRoot, process.argv[2] || path.join('dist', 'build', 'h5'))
const mpRoot = path.resolve(projectRoot, process.argv[3] || path.join('dist', 'build', 'mp-weixin'))
const readText = (filePath) => fs.readFileSync(filePath, 'utf8')
const assertContains = (source, value, label) => {
if (!source.includes(value)) {
throw new Error(`${label} 缺少:${value}`)
}
}
const assertNotContains = (source, value, label) => {
if (source.includes(value)) {
throw new Error(`${label} 不应包含:${value}`)
}
}
const collectJavaScriptFiles = (root) => fs.readdirSync(root, { withFileTypes: true })
.flatMap((entry) => {
const entryPath = path.join(root, entry.name)
if (entry.isDirectory()) return collectJavaScriptFiles(entryPath)
return entry.isFile() && entry.name.endsWith('.js') ? [entryPath] : []
})
const htmlPath = path.join(h5Root, 'index.html')
const html = readText(htmlPath)
const entryMatch = html.match(/<script[^>]+src="([^"]+\.js)"/)
if (!entryMatch) throw new Error('H5 index.html 未找到入口 JS')
const entryPath = path.join(h5Root, entryMatch[1].replace(/^\//, ''))
const entryCode = readText(entryPath)
const pageChunkMatch = entryCode.match(/pages-index-index\.[A-Za-z0-9_-]+\.js/)
if (!pageChunkMatch) throw new Error('H5 入口 JS 未找到首页页面 chunk')
const pageChunkPath = path.join(path.dirname(entryPath), pageChunkMatch[0])
const pageChunkCode = readText(pageChunkPath)
const h5JavaScriptFiles = collectJavaScriptFiles(h5Root)
const h5JavaScriptCode = h5JavaScriptFiles
.map((filePath) => readText(filePath))
.join('\n')
for (const required of ['第三方导航', 'third-party-providers']) {
assertContains(pageChunkCode, required, 'H5 首页 chunk')
}
for (const forbidden of [
'打开地图导航',
'native-location',
'jweixin',
'wx.miniProgram',
'宿主地图',
'/pages/open-location/index'
]) {
assertNotContains(h5JavaScriptCode, forbidden, 'H5 全量 JS')
}
if (h5JavaScriptFiles.some((filePath) => path.basename(filePath).includes('pages-open-location-index'))) {
throw new Error('H5 构建不应生成 open-location 页面 chunk')
}
const mpArrivalPanelPath = path.join(
mpRoot,
'components',
'navigation',
'ArrivalPanel.js'
)
const mpArrivalPanelCode = readText(mpArrivalPanelPath)
const mpArrivalPanelTemplatePath = path.join(
mpRoot,
'components',
'navigation',
'ArrivalPanel.wxml'
)
const mpArrivalPanelTemplate = readText(mpArrivalPanelTemplatePath)
assertContains(mpArrivalPanelCode, '打开地图导航', 'mp-weixin 来馆组件')
assertContains(
mpArrivalPanelCode,
'../../services/WechatOpenLocationService.js',
'mp-weixin 来馆组件'
)
assertNotContains(mpArrivalPanelCode, 'third-party-providers', 'mp-weixin 来馆组件')
assertNotContains(mpArrivalPanelTemplate, 'provider-sheet', 'mp-weixin 来馆模板')
assertNotContains(mpArrivalPanelTemplate, '第三方导航', 'mp-weixin 来馆模板')
const mpAppConfigPath = path.join(mpRoot, 'app.json')
const mpAppConfig = JSON.parse(readText(mpAppConfigPath))
if (!mpAppConfig.pages?.includes('pages/open-location/index')) {
throw new Error('mp-weixin app.json 未注册 open-location 页面')
}
console.log(JSON.stringify({
h5: {
html: path.relative(projectRoot, htmlPath),
entry: path.relative(projectRoot, entryPath),
pageChunk: path.relative(projectRoot, pageChunkPath),
navigationMode: 'third-party-providers'
},
mpWeixin: {
component: path.relative(projectRoot, mpArrivalPanelPath),
template: path.relative(projectRoot, mpArrivalPanelTemplatePath),
navigationMode: 'native-location'
}
}, null, 2))