|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const DIST_DIR = path.join(__dirname, '..', 'dist'); |
| 7 | +const HTML_FILE = path.join(DIST_DIR, 'index.html'); |
| 8 | + |
| 9 | +console.log('🔧 Fixing Unity build...'); |
| 10 | +console.log(`📁 Dist directory: ${DIST_DIR}`); |
| 11 | + |
| 12 | +if (!fs.existsSync(DIST_DIR)) { |
| 13 | + console.error('❌ Dist directory not found!'); |
| 14 | + process.exit(1); |
| 15 | +} |
| 16 | + |
| 17 | +// Find all JS files (excluding .map files) |
| 18 | +const jsFiles = fs.readdirSync(DIST_DIR) |
| 19 | + .filter(f => f.endsWith('.js') && !f.endsWith('.map')) |
| 20 | + .sort((a, b) => { |
| 21 | + // Main bundle first |
| 22 | + if (a.startsWith('game-bridge')) return -1; |
| 23 | + if (b.startsWith('game-bridge')) return 1; |
| 24 | + return a.localeCompare(b); |
| 25 | + }); |
| 26 | + |
| 27 | +console.log(`📦 Found ${jsFiles.length} JS file(s):`, jsFiles); |
| 28 | + |
| 29 | +if (jsFiles.length === 0) { |
| 30 | + console.error('❌ No JS files found to inline!'); |
| 31 | + process.exit(1); |
| 32 | +} |
| 33 | + |
| 34 | +// Combine all JS files |
| 35 | +let combinedJs = ''; |
| 36 | +for (const jsFile of jsFiles) { |
| 37 | + const jsPath = path.join(DIST_DIR, jsFile); |
| 38 | + const jsContent = fs.readFileSync(jsPath, 'utf8'); |
| 39 | + combinedJs += jsContent + '\n'; |
| 40 | + console.log(` ✅ ${jsFile}: ${jsContent.length} bytes`); |
| 41 | +} |
| 42 | + |
| 43 | +console.log(`📊 Total combined JavaScript: ${combinedJs.length} bytes`); |
| 44 | + |
| 45 | +// Create new HTML with inlined JavaScript |
| 46 | +const html = `<!DOCTYPE html> |
| 47 | +<html lang="en"> |
| 48 | +<head> |
| 49 | + <meta charset="utf-8"> |
| 50 | + <title>GameSDK Bridge</title> |
| 51 | + <script>${combinedJs}</script> |
| 52 | +</head> |
| 53 | +<body> |
| 54 | +</body> |
| 55 | +</html>`; |
| 56 | + |
| 57 | +// Write the new HTML file |
| 58 | +fs.writeFileSync(HTML_FILE, html, 'utf8'); |
| 59 | +console.log(`✅ Unity build fixed successfully!`); |
| 60 | +console.log(`📄 Output: ${HTML_FILE} (${html.length} bytes)`); |
| 61 | + |
| 62 | +// Clean up: remove JS files and source maps |
| 63 | +console.log('🧹 Cleaning up external JS files...'); |
| 64 | +for (const jsFile of jsFiles) { |
| 65 | + const jsPath = path.join(DIST_DIR, jsFile); |
| 66 | + fs.unlinkSync(jsPath); |
| 67 | + console.log(` 🗑️ Removed ${jsFile}`); |
| 68 | + |
| 69 | + const mapPath = jsPath + '.map'; |
| 70 | + if (fs.existsSync(mapPath)) { |
| 71 | + fs.unlinkSync(mapPath); |
| 72 | + console.log(` 🗑️ Removed ${jsFile}.map`); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +console.log('✨ Done!'); |
| 77 | + |
0 commit comments