Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/game-bridge/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ module.exports = {
"parserOptions": {
"project": "./tsconfig.json",
"tsconfigRootDir": __dirname
}
},
"ignorePatterns": ["scripts/**"]
}
4 changes: 4 additions & 0 deletions packages/game-bridge/.parcelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@parcel/config-default",
"bundler": "@parcel/bundler-default"
}
11 changes: 9 additions & 2 deletions packages/game-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
"ethers": "^6.13.4"
},
"devDependencies": {
"@parcel/bundler-default": "^2.16.3",
"@parcel/config-default": "^2.16.3",
"eslint": "^8.40.0",
"parcel": "^2.13.3"
},
"scripts": {
"build": "parcel build --no-cache --no-scope-hoist",
"build:local": "parcel build --no-cache --no-scope-hoist && pnpm updateSdkVersion",
"build": "parcel build --no-cache --no-scope-hoist && node scripts/fixUnityBuild.js",
"build:local": "parcel build --no-cache --no-scope-hoist && node scripts/fixUnityBuild.js && pnpm updateSdkVersion",
"lint": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0",
"start": "parcel",
"updateSdkVersion": "./scripts/updateSdkVersion.sh"
Expand All @@ -25,6 +27,9 @@
"unity": {
"context": "browser",
"source": "src/index.html",
"outputFormat": "global",
"scopeHoist": false,
"isLibrary": false,
"engines": {
"browsers": "Chrome 90"
}
Expand All @@ -33,6 +38,8 @@
"outputFormat": "global",
"context": "browser",
"source": "src/index.ts",
"scopeHoist": false,
"isLibrary": false,
"engines": {
"browsers": "Chrome 90"
}
Expand Down
84 changes: 84 additions & 0 deletions packages/game-bridge/scripts/fixUnityBuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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!');
5 changes: 1 addition & 4 deletions packages/game-bridge/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
<head>
<meta charset="utf-8">
<title>GameSDK Bridge</title>
<script type="module">
import "./index.ts";
</script>
<script type="module" src="./index.ts"></script>
</head>

<body>
<h1>Bridge Running</h1>
</body>

</html>
Loading