// main.js —— 从产物文本里提取模块表信息(教学用正则) export function inspectBundle(source) { const ids = [...source.matchAll(/"([^"]+)": function \(module, exports, require\)/g)].map( (m) => m[1], ); return { ids, count: ids.length, bytes: new TextEncoder().encode(source).length, hasRuntime: source.includes('function __require'), bytesPerModule: ids.length ? Math.round(new TextEncoder().encode(source).length / ids.length) : 0, }; } export async function run(log = console.log) { if (typeof document === 'undefined' || typeof fetch !== 'function') { throw new Error('本示例需要浏览器环境:请通过 index.html 打开'); } const res = await fetch('../01-mini-bundler/dist/bundle.js'); if (!res.ok) throw new Error(`加载 bundle 失败:HTTP ${res.status}`); const info = inspectBundle(await res.text()); log(`模块数:${info.count}`); log(`模块 ID:${info.ids.join(', ')}`); log(`产物大小:${info.bytes} 字节(平均每模块 ${info.bytesPerModule} 字节)`); log(`包含运行时:${info.hasRuntime}`); return info; }