#!/usr/bin/env node /* 验证 4:模板里的内联事件函数名,必须全部能在 bundle 里找到 window.X = X。 * * node src-ts/tools/verify-globals.mjs * * 做法: * 1) 从 templates/map.html 抓所有 on*="..." 内联事件,抽出被调用的标识符 * 2) 从 static/dist/app.js 抓 `window.X = X` 的赋值集合 * 3) 从**仍然单独加载**的静态脚本里抓它们的 window 赋值(ui-motion.js 等), * 这些不是 bundle 的责任,单独列出来 * 4) 差集必须为空;同时反查 bundle 里多挂的全局(仅供参考) * * 退出码:差集非空 → 1 */ import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const ROOT = path.resolve(HERE, "..", ".."); const TEMPLATE = path.join(ROOT, "templates", "map.html"); const BUNDLE = path.join(ROOT, "static", "dist", "app.js"); const ORIGIN_SCRIPTS = [ "static/js/map.js", "static/js/navigation.js", "static/js/share.js", "static/js/subscribe.js", ]; /** 切换到 bundle 之后仍然保留的独立脚本(它们的全局不算 bundle 的缺口) */ const STILL_LOADED = [ "static/js/ui-motion.js", "static/js/coordtransform.js", "static/js/settings-pages.js", "static/js/bike-clusters.js", "static/js/qrcode.min.js", "static/js/src/core/geo.js", ]; const EVENT_ATTR = /on(?:click|input|change|submit|keydown|keyup|keypress|blur|focus|dblclick|contextmenu|mouseover|mouseout|mouseenter|mouseleave|mousedown|mouseup|touchstart|touchmove|touchend|scroll|load|error|reset|select)\s*=\s*"([^"]*)"/g; const JS_KEYWORDS = new Set([ "if", "else", "for", "while", "do", "switch", "case", "break", "continue", "return", "new", "delete", "typeof", "instanceof", "in", "of", "var", "let", "const", "function", "true", "false", "null", "undefined", "this", "void", "try", "catch", "finally", "throw", "await", "async", "yield", "class", "super", "default", "export", "import", "static", ]); const DOM_GLOBALS = new Set([ "window", "document", "event", "this", "navigator", "location", "console", "JSON", "Math", "Date", "Number", "String", "Boolean", "Array", "Object", "Map", "Set", "Promise", "Error", "parseInt", "parseFloat", "isNaN", "setTimeout", "clearTimeout", "setInterval", "clearInterval", "fetch", "alert", "confirm", "prompt", "localStorage", "sessionStorage", "encodeURIComponent", "decodeURIComponent", "history", "BMapGL", "QRCode", "SettingsPages", "BikeClusters", "coordtransform", "Geo", ]); const read = (rel) => fs.readFileSync(path.join(ROOT, rel), "utf8"); /* 1) 模板里的内联事件名 */ const html = read("templates/map.html"); const handlerCalls = new Set(); const handlerAny = new Set(); let handlerCount = 0; { let m; EVENT_ATTR.lastIndex = 0; while ((m = EVENT_ATTR.exec(html))) { handlerCount++; const code = m[1]; for (const c of code.matchAll(/(?:^|[^\w$.'"-])([A-Za-z_$][\w$]*)\s*\(/g)) handlerCalls.add(c[1]); for (const c of code.matchAll(/(?:^|[^\w$.'"-])([A-Za-z_$][\w$]*)(?![\w$(])/g)) handlerAny.add(c[1]); } } /* 2) bundle 里的 window 赋值 */ const bundle = read("static/dist/app.js"); const bundleGlobals = new Set(); for (const m of bundle.matchAll(/window\.([A-Za-z_$][\w$]*)\s*=/g)) bundleGlobals.add(m[1]); /* 3) 仍单独加载的脚本提供的全局 */ const externalGlobals = new Map(); for (const rel of STILL_LOADED) { let src; try { src = read(rel); } catch { continue; } for (const m of src.matchAll(/(?:window|root|global)\.([A-Za-z_$][\w$]*)\s*=/g)) { if (!externalGlobals.has(m[1])) externalGlobals.set(m[1], rel); } for (const m of src.matchAll(/^(?:function\s+|const\s+|var\s+|let\s+)([A-Za-z_$][\w$]*)/gm)) { if (!externalGlobals.has(m[1])) externalGlobals.set(m[1], rel); } } /* 4) 原 4 个脚本里声明过的顶层函数名(判断某个内联名到底该谁提供) */ const originNames = new Set(); for (const rel of ORIGIN_SCRIPTS) { for (const m of read(rel).matchAll(/^\s{0,6}(?:async\s+)?function\s+([A-Za-z_$][\w$]*)/gm)) originNames.add(m[1]); } const relevant = (n) => !JS_KEYWORDS.has(n) && !DOM_GLOBALS.has(n); const wanted = new Set([...handlerCalls, ...handlerAny].filter(relevant)); const missing = []; const fromExternal = []; const unknown = []; for (const n of [...wanted].sort()) { if (bundleGlobals.has(n)) continue; if (externalGlobals.has(n)) { fromExternal.push(`${n} (<- ${externalGlobals.get(n)})`); continue; } if (originNames.has(n)) { missing.push(n); continue; } unknown.push(n); } const extras = [...bundleGlobals].filter((n) => !wanted.has(n)).sort(); console.log("=== 全局函数核对 ==="); console.log(`模板内联事件处数 : ${handlerCount}`); console.log(`其中被调用的不同标识符 : ${handlerCalls.size}`); console.log(`连裸标识符一起算 : ${handlerAny.size}`); console.log(`筛选后需要全局的候选名 : ${wanted.size}`); console.log(`bundle 里 window.X = X : ${bundleGlobals.size}`); console.log(""); console.log(`由 bundle 提供的 : ${[...wanted].filter((n) => bundleGlobals.has(n)).length}`); console.log(`由仍加载的静态脚本提供 : ${fromExternal.length}`); fromExternal.forEach((n) => console.log(" " + n)); console.log(""); console.log(`★ 差集(模板要、bundle 没挂):${missing.length}`); missing.forEach((n) => console.log(" MISSING " + n)); console.log(""); console.log(`模板里既不在原脚本、也不在静态脚本里的名字(人工确认,多为 DOM/事件变量):${unknown.length}`); unknown.forEach((n) => console.log(" ? " + n)); console.log(""); console.log(`bundle 多挂的全局(原实现也是全局,属正常):${extras.length}`); console.log(" " + extras.join(", ")); if (missing.length) { console.error("\n失败:有内联事件函数名没有挂到 window 上"); process.exitCode = 1; } else { console.log("\n通过:模板内联事件所需函数名,bundle + 仍加载脚本合起来无缝覆盖(差集为空)"); }