Using a Bundler
IMPORTANT
This page describes an optional way to work, and a game needs none of it. The usual Monogatari game is the starter game: its index.html loads the engine’s browser script, and your own js/*.js files call the monogatari object it creates. That works with a browser and a text editor, on a computer, a tablet or a phone. Start Here walks you through it, and the Engine API describes the monogatari object the way those files use it.
A bundler (Vite, webpack, Bun’s bundler and others) is a tool that reads your JavaScript files and the packages they import, and writes the files a browser loads. Building your game this way needs a computer with Node.js or Bun, a package manager such as npm, a bundler, and a terminal to run them in.
You might choose it when you already build web projects with a bundler, when you want to split your game into modules that import each other or write it in TypeScript, or when your game uses other npm packages.
Get the package
The engine is published on npm as @monogatari/core. Version 2.9.0 is the one npm installs by default:
npm install @monogatari/coreWhat the package holds:
| Path | What it is |
|---|---|
lib/monogatari.module.js |
The engine as one ES module, with everything it needs bundled inside: its libraries, the built-in actions, components and translations, and Font Awesome’s icons. |
dist/engine/core/monogatari.js |
The browser script, the same file as the starter game’s engine/core/monogatari.js. |
dist/engine/core/monogatari.css |
The engine’s stylesheet. |
dist/engine/debug/debug.js |
The debug module. |
dist/types/ |
The TypeScript declarations. |
dist/ |
The rest of the starter game, and src/, the engine’s TypeScript source. Your build doesn’t use them. |
Start the engine from your code
The package’s default export is the engine: the object the starter game’s files know as the monogatari global (and as Monogatari.default). Its named exports are what the browser script puts on the Monogatari global, so const { Action, $_ } = Monogatari; in the starter game is import { Action, $_ } from '@monogatari/core'; in a bundled one.
// main.js
import monogatari from '@monogatari/core';
import '@monogatari/core/monogatari.css';
monogatari.settings({
'ServiceWorkers': false,
});
monogatari.assets('scenes', {
'hall': 'hall.png',
});
monogatari.script({
'Start': [
'show scene hall with fadeIn',
'Hello from a bundled game.',
'end',
],
});
monogatari.init('#monogatari');Built with Vite, the page shows the main menu; Start fades in the scene and shows the line.
The stylesheet import works with a bundler that handles CSS imports (Vite and Bun’s bundler do). If yours doesn’t, load the file with a <link rel="stylesheet"> tag instead.
Everything else works as the Engine API describes, with one difference: the page has no monogatari global. Browsers give each element with an id a global name, so in a bundled game the name monogatari in the console is the <div id="monogatari"> element, not the engine. Import the engine in every module that needs it.
Your code sets every setting the starter game’s js/options.js would have set. A setting you don’t give keeps the engine’s default, and some of those differ from the starter game’s: the storage engine is LocalStorage, not IndexedDB, and save screenshots are off.
The page your game runs in
Your page needs the game’s markup: the <visual-novel> element and the screens inside it. Copy this block from the starter game’s index.html into your page’s <body>, and load your code after it:
<div id="monogatari">
<visual-novel>
<language-selection-screen></language-selection-screen>
<loading-screen></loading-screen>
<main-screen>
<main-menu></main-menu>
</main-screen>
<game-screen>
<dialog-log></dialog-log>
<text-box></text-box>
<quick-menu></quick-menu>
</game-screen>
<gallery-screen></gallery-screen>
<credits-screen></credits-screen>
<load-screen></load-screen>
<save-screen></save-screen>
<settings-screen></settings-screen>
<help-screen></help-screen>
</visual-novel>
</div>
<script type="module" src="./main.js"></script>A module script runs once the page has been read, so calling monogatari.init('#monogatari') and monogatari.on() at the top level of your code finds the markup. The selector you pass to init() is the element that holds <visual-novel>.
Two parts of the starter game need a second look:
- Assets. The engine loads images, music and videos by address while the game runs, from the folder the
AssetsPathsetting names (assets, next to the page, by default). Keep theassetsfolder as plain files that your bundler copies unchanged into its output (Vite’spublicfolder, for example:public/assets/scenes/hall.png), not files your code imports. - The service worker. With the
ServiceWorkerssetting on (the default), the engine registers./service-worker.jsfrom the page’s folder. The starter game’s worker caches the starter game’s own files, which a bundled build doesn’t have. Turn the setting off, as the example does, or serve a worker of your own under that name.
Entry points
| Import | What it gives you |
|---|---|
@monogatari/core |
The engine as the default export, with every built-in action, component and translation registered. The named exports: Action, Component, ShadowComponent, ScreenComponent, MenuComponent, AudioPlayer, FancyError, Artemis’s helpers ($_, $_ready, $_create, DOM, Util, Text, Platform, Preload, Request, Form, FileSystem, Space, SpaceAdapter, Debug, DebugLevel, the storage adapters and their error classes), Luxon, RandomJS, tsParticles and loadSlim. |
@monogatari/core/browser |
The browser script. It exports nothing: importing it creates the monogatari and Monogatari globals, as the starter game’s script tag does. |
@monogatari/core/monogatari.css |
The engine’s stylesheet. @monogatari/core/dist/engine/core/monogatari.css is the same file. |
@monogatari/core/package.json |
The package’s package.json. |
Use one of the two engine entry points, not both: each holds its own copy of the engine, and what you register on one is invisible to the other.
The package has no entry point per action or component: importing @monogatari/core always brings the whole engine (about 2.6 MB before compression, 850 kB with gzip).
Debug mode in a bundled game
The package doesn’t offer the debug module as an import, and it registers its error dialogs on the Monogatari global, which a bundled game doesn’t have. To get the error dialogs while you develop:
Copy
node_modules/@monogatari/core/dist/engine/debug/debug.jsinto the folder your bundler copies as it is (Vite’spublic).Load it in your page’s
<head>, before your code:<script src="./debug.js"></script>Give it the global it looks for, and a debug level, at the top of your code:
import * as Monogatari from '@monogatari/core'; window.Monogatari = Monogatari; const monogatari = Monogatari.default; monogatari.debug.level(5);
Leave the script tag out of your release build. Debugging explains the levels and the dialogs.
TypeScript
The package ships its declarations (dist/types/index.d.ts), and they type-check with "moduleResolution": "Bundler". monogatari.state('label') is a string, monogatari.settings() a typed object, and so on. They need TypeScript 5.0 or later.
The engine’s types come from the package’s entry point too, so your code can import them by name, and a class that extends Action goes to registerAction() as it is, with strict on:
import Monogatari, { Action } from '@monogatari/core';
import type { ActionApplyResult, StateMap, StaticAction } from '@monogatari/core';
class Stamp extends Action {
static override id = 'Stamp';
static override matchString(statement: string[]): boolean {
return statement[0] === 'stamp';
}
stamp: string;
constructor([, stamp]: string[]) {
super();
this.stamp = stamp;
}
override async didApply(): Promise<ActionApplyResult> {
return { advance: true };
}
}
Monogatari.registerAction(Stamp);To give your own state, history or storage keys a type, add them to the engine’s maps (StateMap, HistoryMap, GlobalsMap) with declaration merging, in any .ts file of your project:
declare module '@monogatari/core' {
interface StateMap {
stamps: string[];
}
}monogatari.state('stamps') is then a string[].
Before 2.9.0, these types weren’t exported, and strict refused a class that extends Action in registerAction() unless it was cast (Stamp as unknown as Parameters<typeof monogatari.registerAction>[0]).