import fs from 'node:fs';
|
|
const srcPath = process.argv[2];
|
const outPath = process.argv[3];
|
|
let s = fs.readFileSync(srcPath, 'utf8');
|
const H = 'window.location.host';
|
|
// 每条规则:[原串, 替换串]
|
const rules = [
|
// 主服务域名
|
['"//api.map.baidu.com', `"//" + ${H} + "/bmap-api`],
|
// 瓦片域名(裸域名,数组元素)→ 带括号表达式
|
['"apimaponline0.bdimg.com"', `(${H} + "/bmap-tile0")`],
|
['"apimaponline1.bdimg.com"', `(${H} + "/bmap-tile1")`],
|
['"apimaponline2.bdimg.com"', `(${H} + "/bmap-tile2")`],
|
['"apimaponline3.bdimg.com"', `(${H} + "/bmap-tile3")`],
|
// 瓦片域名带路径(卫星图 starpic):保留后续路径
|
['"apimaponline0.bdimg.com/', `(${H} + "/bmap-tile0/`],
|
['"apimaponline1.bdimg.com/', `(${H} + "/bmap-tile1/`],
|
['"apimaponline2.bdimg.com/', `(${H} + "/bmap-tile2/`],
|
['"apimaponline3.bdimg.com/', `(${H} + "/bmap-tile3/`],
|
// 静态资源
|
['"//webmap0.bdimg.com', `"//" + ${H} + "/bmap-webmap`],
|
['"//maponline0.bdimg.com', `"//" + ${H} + "/bmap-webmap`],
|
];
|
|
function syntaxError(code) {
|
try {
|
new Function(code);
|
return null;
|
} catch (e) {
|
return e.message;
|
}
|
}
|
|
console.log('初始语法: ' + (syntaxError(s) || 'OK'));
|
|
for (const [from, to] of rules) {
|
const n = s.split(from).length - 1;
|
if (n === 0) {
|
continue;
|
}
|
const before = s;
|
s = s.split(from).join(to);
|
const err = syntaxError(s);
|
if (err) {
|
console.log(` X ${from} (${n} 处) -> ${err}`);
|
s = before; // 回滚
|
} else {
|
console.log(` OK ${from} (${n} 处)`);
|
}
|
}
|
|
console.log('最终语法: ' + (syntaxError(s) || 'OK'));
|
|
const tile = (s.match(/\/bmap-tile/g) || []).length;
|
const api = (s.match(/\/bmap-api/g) || []).length;
|
console.log(` 改写点: bmap-tile ${tile} 处, bmap-api ${api} 处`);
|
console.log(' 主瓦片用 qt=tile: ' + (s.includes('qt=tile') ? '是' : '否'));
|
console.log(' 含 qt=vtile: ' + (s.includes('qt=vtile') ? '是' : '否'));
|
|
fs.writeFileSync(outPath, s);
|
console.log('已写出: ' + outPath);
|