📚 Encyclopedia

Games & Graphics · updated 2026-10-13

Structuring a browser game as plain ES modules (no bundler)

javascriptes-modulesarchitecturegame-staterefactoring

Joyride (a top-down driving game on justinahampton.com) started as a 1,100-line single HTML file and was split into native ES modules with no bundler, TypeScript or framework, because the site is static and the game has no dependencies. Justin wanted "a professional architecture as we will be adding more features". The split was done as a pure move (no behavior change), verified by a harness, then reviewed by architect, pedant and adversarial QA agents; the pedant caught two regressions before shipping.

Layering

Keep the import graph acyclic and layered bottom to top, for example: config / util / state -> dom -> fx / hud -> city / economy / input -> peds -> combat -> physics -> traffic / player -> shop / pause -> render / game -> menu -> controls -> main. A lower layer never imports a higher one. When it needs to cause something higher up, it sets state and lets the higher layer act (the player's death is a flag set by combat that the game loop handles, not a call up).

Shared state convention

Pause ownership

One module owns pausing (S.pausedBy = shop | settings | menu); only the owner resumes. This fixed a bug where Space over the settings panel resumed the game underneath.

Deploy and caching

Serve the module directory with Cache-Control: no-cache so a deploy never mixes stale modules with a new page (Caching static sites and unbundled ES modules (?v= stamps, no-cache, prefetch)).

Review backlog worth knowing (the usual smells)

See also

Linked from

Sources: justinahampton memory (joyride-architecture, arcade-joyride), 2026-09-06