bun run build turns src/ and debug/ into the files games and the npm package use, and the repository commits those files. This page goes through the script, each output, and the package that ships them.

What bun run build runs

package.json chains four scripts:

build  = bun run clean && bun run lint && bun run check && bun build.ts
clean  = bun run rm -rf dist/engine/core dist/types && rm -rf lib
lint   = bunx eslint .
check  = bunx tsc --noEmit
  1. clean deletes dist/engine/core/, dist/types/ and lib/. It leaves dist/engine/debug/, which the build overwrites.
  2. lint runs ESLint with eslint.config.mjs on every hand-written script: src/, debug/, the specs, the build scripts and the starter game’s. The built bundles, the declarations and vendored code are left out.
  3. check type-checks src/ with tsconfig.json (strict) without writing anything.
  4. bun build.ts builds the five outputs: the ES module, the browser bundle, the stylesheet and the debug module at the same time, then the type declarations.

Any failing step stops the build, after clean has already deleted the outputs: a lint error leaves you without dist/engine/core/ until you fix it and build again (or git checkout -- dist lib to get the committed files back).

What build.ts writes

Function Entry Output Format
buildModule() src/index.ts lib/monogatari.module.js and .map ES module, minified, for browsers
buildBrowser() src/browser.ts dist/engine/core/monogatari.js and .map IIFE, minified, for a <script> tag
buildCSS() src/index.css dist/engine/core/monogatari.css minified CSS (no map file is written)
buildDebug() debug/index.js dist/engine/debug/debug.js and .map IIFE, minified
buildTypes() src/ dist/types/**/*.d.ts and .d.ts.map tsc --emitDeclarationOnly --noEmit false --rootDir ./src --declarationDir ./dist/types

All five use Bun.build, except the declarations, which run tsc. Each Bun build bundles its dependencies: Artemis, Pandora (and the Lit it carries), tsParticles, Luxon, Mousetrap, deeply, modern-screenshot and Font Awesome’s icon script are inside monogatari.js; animate.css, csshake, Font Awesome’s styles and Kayros are inside monogatari.css. So a game needs only the two files, and the ES module in lib/ has no runtime dependencies either. At 2.9.0, monogatari.js is 2.7 MB, monogatari.css 145 KB and debug.js 331 KB.

src/browser.ts exists for the browser bundle only: it imports everything from src/index.ts and sets window.Monogatari (the module) and window.monogatari (its default export, the engine). The ES module doesn’t set any global.

buildTypes() checks its own result: if dist/types/index.d.ts is missing afterwards, the build fails with ❌ Type declarations build emitted no dist/types/index.d.ts. That check came with 2.8.1, after tsc had silently written nothing for every earlier build (tsconfig.json sets noEmit, and the old command didn’t override it).

One part at a time

bun build.ts --module, --browser, --css, --debug or --types builds only that part, with no clean, lint or type check. The browser bundle and the stylesheet rebuild in under a second, which makes them the dev loop (Setting Up the Repository).

bun build.ts --watch (bun run watch) builds the browser bundle, the stylesheet and the debug module, then watches src/ and debug/ with fs.watch and rebuilds the one a changed file belongs to: a .css file under src/ rebuilds monogatari.css, anything else under src/ rebuilds monogatari.js, anything under debug/ rebuilds debug.js. Changes within 100 ms make one rebuild, and rebuilds of the same output never overlap. Adding --browser, --css or --debug watches only those (watch:js and watch:css in package.json). A failed rebuild prints its error and the watch goes on; the full build still exits with 1 on the first error. Until 2.9.0, --watch ran bun build --watch on src/browser.ts, which wrote unminified rebuilds to dist/engine/core/browser.js, a file nothing loads, and never watched the stylesheet.

buildTypes() runs the project’s TypeScript (typescript/bin/tsc, resolved from build.ts and run with Bun), not the first tsc on your PATH: a global TypeScript 7 here failed on the vendored random-js and rewrote 101 declaration maps when bun build.ts --types ran it directly.

Why the output is committed

dist/engine/core/, dist/engine/debug/, dist/types/ and lib/ are in Git, unlike most projects’ build output:

  • dist/ is the starter game people download, and it must run as it is.
  • The Cypress specs load dist/index.html and test whatever dist/engine/core/ holds.
  • The release zip is made from the dist/ folder at the release tag (Releasing a 2.8.x Patch).

So a change to src/ and its rebuilt files go in together. Run bun run build before you commit, and commit what it changed. The build is reproducible: with the same Bun (1.4.4, which CI pins) and the packages bun.lock names, run on a commit whose output is current, it changes nothing, so git status after a build tells you whether a branch forgot its rebuilt files. CI checks exactly that (Testing the Engine).

WARNING

Bun installs packages as hard links to its own cache (~/.bun/install/cache). Editing a file inside node_modules edits that cache, and every project and worktree that uses the same package version, and the bundles stop matching. To try a change to a dependency, copy it into a scratch project, or install into a separate cache with BUN_INSTALL_CACHE_DIR=<a new folder> bun install.

Until December 2025 a workflow did the rebuilding: on every push to develop it built the engine and committed dist/engine/core as “Update engine core”. Commit 5b16c02e (“Don’t commit core”) stopped the automatic commit and 555f9151 removed the workflow.

The npm package

package.json describes @monogatari/core:

Field Value at 2.9.0 Meaning
main, module ./lib/monogatari.module.js the ES module, for tools that don’t read exports
types ./dist/types/index.d.ts the declarations
exports["."] types ./dist/types/index.d.ts, import and default ./lib/monogatari.module.js import Monogatari from '@monogatari/core'
exports["./browser"] types ./dist/types/browser.d.ts, import and default ./dist/engine/core/monogatari.js the browser bundle. It’s an IIFE with no exports: importing it runs it and sets the globals
exports["./monogatari.css"], exports["./dist/engine/core/monogatari.css"] ./dist/engine/core/monogatari.css the stylesheet
css dist/engine/core/monogatari.css the stylesheet, for tools that read this field
files README.md, LICENSE, package.json, src/, lib/, and each folder and file of the starter game by name: dist/types/, dist/engine/, dist/assets/, dist/js/, dist/style/, dist/index.html, dist/favicon.ico, dist/manifest.json, dist/service-worker.js, dist/package.json, dist/bun.lock, dist/serve.ts, dist/build-web.ts, dist/electrobun.config.ts, dist/jsconfig.json, dist/jsconfig.node.json what the package contains

The package has no prepack script: npm pack and npm publish don’t build. They pack what’s on disk, so build and check git status first.

files names the starter game’s files one by one because npm doesn’t apply .gitignore to what files lists. Until 2.9.0 it listed dist/*, so anything left in dist/ went into the package: a dist/node_modules/ from installing the desktop hosts’ dependencies, a dist/build/ from a desktop build, a zip. The 2.8.1 package on npm contains dist/monogatari-v2.8.0.zip, the previous release’s starter game, 2.8 MB that should not be there. Now those stay out, and a new file in the starter game must be added to files, or the package won’t have it. Before publishing, read the file list of npm pack --dry-run: from a clean checkout it ends with total files: 539.

The 2.8.0 package had the opposite problem: no lib/ folder and no declarations, while main, module and exports pointed at files it didn’t contain. 2.8.1 fixed both, in the same commit as the character positioning fix.

The declarations

dist/types/ mirrors src/: dist/types/monogatari.d.ts, dist/types/actions/Dialog.d.ts, and so on, each with a .d.ts.map (tsconfig.json sets declarationMap). The maps point into src/, which the package also ships, so an editor’s “go to definition” in a game’s code lands in the TypeScript source. The engine’s public shape is the VisualNovelEngine interface in src/lib/types/Monogatari.ts; games can add to it, and to StateMap, HistoryMap and GlobalsMap, by declaration merging (the comments at the top of src/lib/types/index.ts show how).