#!/usr/bin/env node /** * 根据 Cocos 构建出的 index.html,生成可嵌入主站的片段说明 * * node tools/patch-main-index.js build/web-desktop */ const fs = require('fs'); const path = require('path'); const buildDir = process.argv[2]; if (!buildDir) { console.error('Usage: node patch-main-index.js '); process.exit(1); } const indexPath = path.join(buildDir, 'index.html'); if (!fs.existsSync(indexPath)) { console.error('Not found:', indexPath); process.exit(1); } const html = fs.readFileSync(indexPath, 'utf8'); // 提取所有 script 标签(Cocos 启动链) const scripts = []; const re = /]*src=["']([^"']+)["'][^>]*><\/script>/gi; let m; while ((m = re.exec(html)) !== null) { scripts.push(m[1]); } // 提取内联 script(System.import 等) const inline = html.match(/\n'); console.log(''); for (const src of scripts) { const rel = src.startsWith('http') ? src : `cocos/${src.replace(/^\.\//, '')}`; console.log(``); } console.log('\n'); const canvasMatch = html.match(/]*id=["']([^"']+)["'][^>]*>/i); const divMatch = html.match(/id=["'](GameDiv|Cocos3dGameContainer)["']/i); console.log(''); if (inline.length) { console.log('\n'); inline.forEach((block) => console.log(block)); } console.log('\n=== 删除原 Unity 段 ==='); console.log('- Build/build.loader.js'); console.log('- createUnityInstance(...)');