108 lines
3.7 KiB
JavaScript
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')
|
|
|
|
// H5 uses the embedded Tencent JS map and the backend route service. The
|
|
// historical external-map handoff must not be required by this bundle check.
|
|
for (const required of [
|
|
'/gis/outdoor-map',
|
|
'/routes/plan',
|
|
'/places/suggestions',
|
|
'/geocoding/reverse',
|
|
'重新检测',
|
|
'手动选择起点',
|
|
'地图选点',
|
|
'semanticEnterBuilding'
|
|
]) {
|
|
assertContains(h5JavaScriptCode, required, 'H5 全量 JS')
|
|
}
|
|
|
|
for (const forbidden of [
|
|
'OutdoorNavigationPanel',
|
|
'打开地图导航',
|
|
'baidumap://',
|
|
'iosamap://',
|
|
'androidamap://'
|
|
]) {
|
|
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, 'typeChange', 'mp-weixin 来馆组件')
|
|
assertContains(mpArrivalPanelTemplate, '去这里', 'mp-weixin 来馆模板')
|
|
assertNotContains(mpArrivalPanelCode, 'third-party-providers', 'mp-weixin 来馆组件')
|
|
assertNotContains(mpArrivalPanelTemplate, 'provider-sheet', 'mp-weixin 来馆模板')
|
|
assertNotContains(mpArrivalPanelTemplate, '第三方导航', 'mp-weixin 来馆模板')
|
|
|
|
console.log(JSON.stringify({
|
|
h5: {
|
|
html: path.relative(projectRoot, htmlPath),
|
|
entry: path.relative(projectRoot, entryPath),
|
|
pageChunk: path.relative(projectRoot, pageChunkPath),
|
|
navigationMode: 'embedded-tencent-map'
|
|
},
|
|
mpWeixin: {
|
|
component: path.relative(projectRoot, mpArrivalPanelPath),
|
|
template: path.relative(projectRoot, mpArrivalPanelTemplatePath),
|
|
navigationMode: 'arrival-target-selection'
|
|
}
|
|
}, null, 2))
|