From 69f639bcc15d271ecbde573e593279c3d28ccc35 Mon Sep 17 00:00:00 2001 From: ImmutableJeffrey Date: Fri, 30 Jan 2026 02:41:47 +0900 Subject: [PATCH 1/2] fix(game-bridge): fix Unity HTML bundle code splitting issue Parcel 2 with viem was creating broken code-split bundles for the Unity HTML target, causing Cannot find module cMvw3 errors. Added post-build script that uses the complete unreal JS bundle to create a self-contained HTML file for Unity. --- packages/game-bridge/.eslintrc.cjs | 3 +- packages/game-bridge/.parcelrc | 4 ++ packages/game-bridge/package.json | 11 ++- packages/game-bridge/scripts/fixUnityBuild.js | 67 +++++++++++++++++++ packages/game-bridge/src/index.html | 1 - 5 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 packages/game-bridge/.parcelrc create mode 100644 packages/game-bridge/scripts/fixUnityBuild.js diff --git a/packages/game-bridge/.eslintrc.cjs b/packages/game-bridge/.eslintrc.cjs index 8b8a821f7e..72bea9abde 100644 --- a/packages/game-bridge/.eslintrc.cjs +++ b/packages/game-bridge/.eslintrc.cjs @@ -4,5 +4,6 @@ module.exports = { "parserOptions": { "project": "./tsconfig.json", "tsconfigRootDir": __dirname - } + }, + "ignorePatterns": ["scripts/**"] } diff --git a/packages/game-bridge/.parcelrc b/packages/game-bridge/.parcelrc new file mode 100644 index 0000000000..b2f87351d6 --- /dev/null +++ b/packages/game-bridge/.parcelrc @@ -0,0 +1,4 @@ +{ + "extends": "@parcel/config-default", + "bundler": "@parcel/bundler-default" +} diff --git a/packages/game-bridge/package.json b/packages/game-bridge/package.json index 1f42651bd0..6ca0945683 100644 --- a/packages/game-bridge/package.json +++ b/packages/game-bridge/package.json @@ -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" @@ -25,6 +27,9 @@ "unity": { "context": "browser", "source": "src/index.html", + "outputFormat": "global", + "scopeHoist": false, + "isLibrary": false, "engines": { "browsers": "Chrome 90" } @@ -33,6 +38,8 @@ "outputFormat": "global", "context": "browser", "source": "src/index.ts", + "scopeHoist": false, + "isLibrary": false, "engines": { "browsers": "Chrome 90" } diff --git a/packages/game-bridge/scripts/fixUnityBuild.js b/packages/game-bridge/scripts/fixUnityBuild.js new file mode 100644 index 0000000000..763600d93a --- /dev/null +++ b/packages/game-bridge/scripts/fixUnityBuild.js @@ -0,0 +1,67 @@ +#!/usr/bin/env node +/* eslint-disable */ +/** + * Post-build script to fix the Unity HTML bundle. + * + * Parcel 2 with viem creates code-split bundles for the HTML target which don't work + * in the Unity embedded browser. This script: + * 1. Takes the complete JS bundle from the unreal target + * 2. Creates a proper self-contained HTML file for Unity + * + * The unreal target uses index.ts as entry point and produces a single complete JS bundle, + * while the HTML target incorrectly splits the code. + */ + +const fs = require('fs'); +const path = require('path'); + +const UNITY_DIST_DIR = path.join(__dirname, '..', 'dist', 'unity'); +const UNREAL_DIST_DIR = path.join(__dirname, '..', 'dist', 'unreal'); +const HTML_FILE = path.join(UNITY_DIST_DIR, 'index.html'); +const UNREAL_JS_FILE = path.join(UNREAL_DIST_DIR, 'index.js'); + +console.log('Fixing Unity build...'); + +// Read the complete JS bundle from unreal target +const jsContent = fs.readFileSync(UNREAL_JS_FILE, 'utf8'); +console.log(`Read unreal bundle: ${jsContent.length} bytes`); + +// Create a self-contained HTML file +const html = ` + + + + GameSDK Bridge + + + + +`; + +// Ensure the unity dist directory exists +if (!fs.existsSync(UNITY_DIST_DIR)) { + fs.mkdirSync(UNITY_DIST_DIR, { recursive: true }); +} + +// 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 any split JS files from the broken HTML build +const jsFiles = fs.readdirSync(UNITY_DIST_DIR).filter(f => f.endsWith('.js')); +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!'); diff --git a/packages/game-bridge/src/index.html b/packages/game-bridge/src/index.html index 1104ae175e..cf9f096c9c 100644 --- a/packages/game-bridge/src/index.html +++ b/packages/game-bridge/src/index.html @@ -10,7 +10,6 @@ -

Bridge Running

\ No newline at end of file From 640d5114cb87c0317e87daa33e8161d372052673 Mon Sep 17 00:00:00 2001 From: ImmutableJeffrey Date: Fri, 30 Jan 2026 02:49:03 +0900 Subject: [PATCH 2/2] fix(game-bridge): improve HTML source structure and inline CCIP code Changed HTML source to use script src instead of inline import, which fixes the broken /index.js reference. Updated fix script to inline all JS files including CCIP code for a fully self-contained bundle. --- packages/game-bridge/scripts/fixUnityBuild.js | 55 ++++++++++++------- packages/game-bridge/src/index.html | 4 +- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/packages/game-bridge/scripts/fixUnityBuild.js b/packages/game-bridge/scripts/fixUnityBuild.js index 763600d93a..85225924d6 100644 --- a/packages/game-bridge/scripts/fixUnityBuild.js +++ b/packages/game-bridge/scripts/fixUnityBuild.js @@ -3,28 +3,51 @@ /** * Post-build script to fix the Unity HTML bundle. * - * Parcel 2 with viem creates code-split bundles for the HTML target which don't work - * in the Unity embedded browser. This script: - * 1. Takes the complete JS bundle from the unreal target - * 2. Creates a proper self-contained HTML file for Unity + * 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) * - * The unreal target uses index.ts as entry point and produces a single complete JS bundle, - * while the HTML target incorrectly splits the code. + * When Parcel builds the HTML target with + `; -// Ensure the unity dist directory exists -if (!fs.existsSync(UNITY_DIST_DIR)) { - fs.mkdirSync(UNITY_DIST_DIR, { recursive: true }); -} - // 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 any split JS files from the broken HTML build -const jsFiles = fs.readdirSync(UNITY_DIST_DIR).filter(f => f.endsWith('.js')); +// Clean up JS files (now inlined) for (const jsFile of jsFiles) { const jsPath = path.join(UNITY_DIST_DIR, jsFile); fs.unlinkSync(jsPath); diff --git a/packages/game-bridge/src/index.html b/packages/game-bridge/src/index.html index cf9f096c9c..6df5930d4d 100644 --- a/packages/game-bridge/src/index.html +++ b/packages/game-bridge/src/index.html @@ -4,9 +4,7 @@ GameSDK Bridge - +