LINE1 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 1601234import { cp, mkdir, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises';5import { dirname, join, resolve } from 'node:path';67import { CASTLE_TEXTURE_DIR, GROUND_TEXTURE, HERO_MODEL_DIR, HERO_MODEL_FILES, MODEL_DIR, MODEL_FILES, PLATFORM_TEXTURE, VILLAGE_WATER_TEXTURE } from '../src/assets.js';8import { CARDS, CHARACTERS } from '../src/catalog.js';910const root = resolve(import.meta.dir, '..');11const dist = join(root, 'dist');1213await rm(dist, { recursive: true, force: true });14await mkdir(dist, { recursive: true });15161718const vendoredThree = {19 name: 'vendored-three',20 setup(build) {21 build.onResolve({ filter: /^three$/ }, () => ({22 path: join(root, 'vendor', 'three.module.js'),23 }));24 build.onResolve({ filter: /^three\/addons\25 path: join(root, 'vendor', 'addons', args.path.slice('three/addons/'.length)),26 }));27 },28};2930const result = await Bun.build({31 entrypoints: [join(root, 'src', 'main.js')],32 outdir: dist,33 target: 'browser',34 format: 'esm',35 minify: true,36 sourcemap: 'none',37 naming: 'app.js',38 plugins: [vendoredThree],39});4041if (!result.success) {42 for (const log of result.logs) console.error(log);43 process.exit(1);44}4546const sourceHtml = await readFile(join(root, 'index.html'), 'utf8');47const bundledHtml = sourceHtml48 .replace(/\s*<script type="importmap">[\s\S]*?<\/script>/, '')49 .replace(50 '<script type="module" src="src/main.js"></script>',51 '<script type="module" src="./app.js"></script>',52 );5354if (bundledHtml.includes('importmap') || bundledHtml.includes('src/main.js')) {55 throw new Error('index.html was not rewritten for the bundle');56}5758await writeFile(join(dist, 'index.html'), bundledHtml);59await cp(join(root, 'styles.css'), join(dist, 'styles.css'));6061626364const usedAssets = new Set([65 GROUND_TEXTURE,66 PLATFORM_TEXTURE,67 VILLAGE_WATER_TEXTURE,68 ...Object.values(CHARACTERS)69 .map((character) => character.portrait)70 .filter(Boolean),71 ...CARDS.map((card) => card.icon),72 ...Object.values(MODEL_FILES).map((file) => MODEL_DIR + file),73 ...Object.values(HERO_MODEL_FILES).map((file) => HERO_MODEL_DIR + file),74]);75for (const file of await readdir(join(root, CASTLE_TEXTURE_DIR))) {76 if (/\.(png|jpe?g|webp)$/i.test(file)) {77 usedAssets.add(`${CASTLE_TEXTURE_DIR}/${file}`);78 }79}8081for (const asset of usedAssets) {82 const destination = join(dist, asset);83 await mkdir(dirname(destination), { recursive: true });84 await cp(join(root, asset), destination);85}8687const bundle = await stat(join(dist, 'app.js'));88console.log(89 `Bundled the game into dist/ (app.js ${(bundle.size / 1024 / 1024).toFixed(1)} MB, ${usedAssets.size} assets).`,90);91console.log('Preview it with: bun run serve:dist');