-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathfixUnityBuild.js
More file actions
84 lines (72 loc) · 2.61 KB
/
Copy pathfixUnityBuild.js
File metadata and controls
84 lines (72 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env node
/* eslint-disable */
/**
* Post-build script to fix the Unity HTML bundle.
*
* ROOT CAUSE:
* viem uses dynamic import() for lazy-loading CCIP (Cross-Chain Interoperability Protocol) code:
* const { offchainLookup } = await import('../../utils/ccip.js');
* (see: viem/_esm/actions/public/call.js line 126)
*
* When Parcel builds the HTML target with <script type="module">, it:
* 1. Creates a separate ccip.*.js chunk for the dynamic import
* 2. Uses absolute paths in script src (e.g., /game-bridge.*.js) which don't work offline
*
* Parcel's bundler configuration (minBundles, manualSharedBundles) only affects *shared* bundles
* between multiple entry points, NOT async bundles from dynamic imports.
*
* WORKAROUND:
* This script inlines all external JS files into the HTML to create a self-contained bundle.
* This ensures the Unity embedded browser (which runs offline) can load everything.
*/
const fs = require('fs');
const path = require('path');
const UNITY_DIST_DIR = path.join(__dirname, '..', 'dist', 'unity');
const HTML_FILE = path.join(UNITY_DIST_DIR, 'index.html');
console.log('Fixing Unity build...');
// Read all JS files in the dist directory (sorted so main bundle comes first)
const jsFiles = fs.readdirSync(UNITY_DIST_DIR)
.filter(f => f.endsWith('.js') && !f.endsWith('.map'))
.sort((a, b) => {
// Main bundle should be first
if (a.startsWith('game-bridge')) return -1;
if (b.startsWith('game-bridge')) return 1;
return a.localeCompare(b);
});
console.log(`Found ${jsFiles.length} JS file(s):`, jsFiles);
// Combine all JS files
let combinedJs = '';
for (const jsFile of jsFiles) {
const jsPath = path.join(UNITY_DIST_DIR, jsFile);
const jsContent = fs.readFileSync(jsPath, 'utf8');
combinedJs += jsContent + '\n';
console.log(` - ${jsFile}: ${jsContent.length} bytes`);
}
// Create a self-contained HTML file
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GameSDK Bridge</title>
<script>${combinedJs}</script>
</head>
<body>
</body>
</html>`;
// Write the fixed HTML
fs.writeFileSync(HTML_FILE, html, 'utf8');
console.log('Unity build fixed successfully!');
console.log(`Output: ${HTML_FILE} (${html.length} bytes)`);
// Clean up JS files (now inlined)
for (const jsFile of jsFiles) {
const jsPath = path.join(UNITY_DIST_DIR, jsFile);
fs.unlinkSync(jsPath);
console.log(`Removed ${jsFile}`);
// Also remove the map file if it exists
const mapPath = jsPath + '.map';
if (fs.existsSync(mapPath)) {
fs.unlinkSync(mapPath);
console.log(`Removed ${jsFile}.map`);
}
}
console.log('Done!');