add dev server (bun run dev → http://localhost:3000)

This commit is contained in:
Volodia
2026-06-18 12:58:22 +03:00
parent b947fbb7fb
commit 46dc9f2fad
4 changed files with 1145 additions and 8 deletions
+3 -4
View File
@@ -7,15 +7,14 @@ Built with TypeScript and Canvas 2D, bundled with **Bun**.
```bash ```bash
bun install # install deps (none currently required) bun install # install deps (none currently required)
bun start # build + prepare dist/ bun start # production build → dist/
open dist/index.html open dist/index.html
``` ```
Or for development with watch mode: Or for development with live reload:
```bash ```bash
bun run dev # watches src/ and rebuilds dist/main.js bun run dev # starts dev server at http://localhost:3000 + watch mode
# then open dist/index.html manually
``` ```
## Controls ## Controls
+51
View File
@@ -0,0 +1,51 @@
import { spawn } from "bun";
// Ensure dist/ has the HTML shell
await Bun.write(
"./dist/index.html",
`<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dungeon Crawl — Ranged / Melee</title>
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{background:#0a0a0a;display:flex;justify-content:center;align-items:center;height:100vh;font-family:monospace;overflow:hidden;user-select:none}
canvas{display:block;border:1px solid #222;border-radius:2px;cursor:none}
</style>
</head>
<body>
<canvas id="game" width="880" height="660"></canvas>
<script src="main.js"></script>
</body>
</html>`
);
// Start build watcher
const builder = spawn(["bun", "build", "src/main.ts", "--outdir", "dist", "--target", "browser", "--watch"], {
stdout: "inherit",
stderr: "inherit",
});
// Start dev server
const port = 3000;
const server = Bun.serve({
port,
async fetch(req) {
const url = new URL(req.url);
let path = url.pathname === "/" ? "/index.html" : url.pathname;
const file = Bun.file("./dist" + path);
return new Response(file);
},
});
console.log(`\n Dev server: http://localhost:${port}`);
console.log(` Watching: src/\n`);
// Graceful shutdown
process.on("SIGINT", () => {
builder.kill();
server.stop();
process.exit(0);
});
+1090 -2
View File
File diff suppressed because one or more lines are too long
+1 -2
View File
@@ -4,9 +4,8 @@
"private": true, "private": true,
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "bun build src/main.ts --outdir dist --target browser --watch", "dev": "bun run dev.ts",
"build": "bun run build.ts", "build": "bun run build.ts",
"serve": "bun build src/main.ts --outdir dist --target browser && cp src/index.html dist/",
"start": "bun run build && echo 'open dist/index.html'" "start": "bun run build && echo 'open dist/index.html'"
} }
} }