Files
21/scripts/sync-dice-box-assets.mjs

73 lines
2.4 KiB
JavaScript

/**
* Bundles @3d-dice/dice-box into a single IIFE file and copies
* all required assets into android/app/src/main/assets/ so the
* WebView can load everything offline from file:///android_asset/.
*
* Usage: node scripts/sync-dice-box-assets.mjs
* Called automatically via the "postinstall" npm script.
*/
import { build } from 'esbuild';
import { cpSync, mkdirSync, copyFileSync, rmSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { diceBoxAnimationPlugin } from './patch-dice-box.mjs';
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const ANDROID_ASSETS = resolve(ROOT, 'android/app/src/main/assets');
const DICE_BOX_DIST = resolve(ROOT, 'node_modules/@3d-dice/dice-box/dist');
// 0. Clean previous output
rmSync(resolve(ANDROID_ASSETS, 'dice-box'), { recursive: true, force: true });
// 1. Bundle dice-box ES module → IIFE (DiceBoxModule global)
await build({
entryPoints: [resolve(DICE_BOX_DIST, 'dice-box.es.js')],
bundle: true,
format: 'iife',
globalName: 'DiceBoxModule',
outfile: resolve(ANDROID_ASSETS, 'dice-box/dice-box-bundle.js'),
minify: true,
platform: 'browser',
target: ['es2015'],
logLevel: 'info',
plugins: [diceBoxAnimationPlugin(ROOT)],
});
// 2. Copy static assets (ammo WASM, default theme) preserving directory structure
mkdirSync(resolve(ANDROID_ASSETS, 'dice-box/assets'), { recursive: true });
cpSync(
resolve(DICE_BOX_DIST, 'assets'),
resolve(ANDROID_ASSETS, 'dice-box/assets'),
{ recursive: true },
);
// 2b. Copy all additional themes from @3d-dice/dice-themes
const DICE_THEMES_SRC = resolve(ROOT, 'node_modules/@3d-dice/dice-themes/themes');
cpSync(
DICE_THEMES_SRC,
resolve(ANDROID_ASSETS, 'dice-box/assets/themes'),
{ recursive: true },
);
// Repository-owned themes override bundled themes, never the other way around.
// Includes all three static cosmic variants. Authoring .blend files stay in
// assets/dice-theme-source and are excluded from the APK.
cpSync(
resolve(ROOT, 'assets/dice-themes'),
resolve(ANDROID_ASSETS, 'dice-box/assets/themes'),
{ recursive: true },
);
// 3. Copy the offline HTML template and JS files into android assets.
for (const file of ['dice3d.html', 'webview-polyfills.js', 'dice-bridge.js']) {
copyFileSync(
resolve(ROOT, 'src/html', file),
resolve(ANDROID_ASSETS, file),
);
}
console.log('✓ dice-box assets synced to android/app/src/main/assets/');