import { copyFileSync, cpSync, existsSync, readFileSync, realpathSync, writeFileSync, } from "node:fs"; import { resolve, dirname } from "node:path"; import { fileURLToPath } from "node:url"; import { execFileSync } from "node:child_process"; const __dirname = dirname(fileURLToPath(import.meta.url)); const src = resolve(__dirname, "../build/web"); const dest = resolve(__dirname, "../../../apps/web/public/flutter"); cpSync(src, dest, { recursive: true }); console.log(`Copied Flutter build: ${src} → ${dest}`); // Copy flutter.js.map from the Flutter SDK (not included in build output) const flutterBin = execFileSync("which", ["flutter"], { encoding: "utf-8", }).trim(); const sdkBinDir = dirname(realpathSync(flutterBin)); const flutterJsMap = resolve( sdkBinDir, "cache/flutter_web_sdk/flutter_js/flutter.js.map", ); if (existsSync(flutterJsMap)) { copyFileSync(flutterJsMap, resolve(dest, "flutter.js.map")); console.log("Copied flutter.js.map from SDK"); } else { console.warn(`flutter.js.map not found at ${flutterJsMap}`); } // Extract buildConfig from flutter_bootstrap.js so the React app can fetch it const bootstrap = readFileSync(resolve(dest, "flutter_bootstrap.js"), "utf-8"); const match = bootstrap.match(/_flutter\.buildConfig\s*=\s*({.*?});/); if (match) { writeFileSync(resolve(dest, "build_config.json"), match[1]); console.log("Extracted build_config.json"); } else { console.warn("Could not extract buildConfig from flutter_bootstrap.js"); }