Skip to content

Extract the dialog box into the @workadventure/npc-dialog-box package - #4

Open
moufmouf wants to merge 4 commits into
npc-dialog-typewriterfrom
npc-dialog-library
Open

Extract the dialog box into the @workadventure/npc-dialog-box package#4
moufmouf wants to merge 4 commits into
npc-dialog-typewriterfrom
npc-dialog-library

Conversation

@moufmouf

Copy link
Copy Markdown

Stacked on #2.

What

The dialog box moves out of the map's src/ into packages/npc-dialog-box, an npm workspace named @workadventure/npc-dialog-box. The map consumes it exactly like the future published package will be consumed; when it moves to its own GitHub repository, only the workspace wiring in the root package.json changes to a regular version.

How the HTML problem is solved

The hard part was shipping npc-dialog.html in the library while getting it into the consumer's Vite build. The answer is twofold:

  1. The package's own build makes the page self-contained: build.mjs bundles the iframe-side code (typewriter UI, styles, player-variable protocol) into a minified IIFE and inlines it into the HTML template. dist/npc-dialog.html has zero build-time dependencies on the consumer.
  2. A Vite plugin ships in the package (@workadventure/npc-dialog-box/vite): in build it emitFiles the page under the stable name npc-dialog.html at the dist root — exactly what openDialog's default dialogUrl resolves to relative to the map — and in dev it serves the identical bytes at the same URL via a configureServer middleware. Optional assets: [...] copies map-specific files (the avatar) alongside.

Consumer integration is one line per Vite config:

plugins: [ npcDialogBox({ assets: ["npc-avatar.png"] }), ...getMapsOptimizers(...) ]

Consequences for the map

  • Root npc-dialog.html and the npcDialog rollup input entry are gone; the wrapper page and its iframe_api.js referrer-loader now live in the package template.
  • The default avatar <template> trick is gone: the map passes avatar: "npc-avatar.png" to openDialog instead.
  • prepare builds the package on npm install, so CI (npm install && npm run buildmap) keeps working unchanged.
  • A manual escape hatch exists (@workadventure/npc-dialog-box/npc-dialog.html export + the dialogUrl option) for setups that can't use the plugin.

Testing

  • npm install builds the workspace package; tsc and npm run buildmap pass; dist/ contains npc-dialog.html (self-contained) and npc-avatar.png with stable names.
  • Dev server verified on a second port: the plugin serves /npc-dialog.html (correct content-type) and /npc-avatar.png; /src/main.ts bundling resolves the bare package import.
  • The built page was exercised in a browser harness with a mocked WA API: typewriter, click-anywhere, space key, avatar loading and step events all behave as before.
  • Note for local dev: restart npm run dev after checking out this branch so the dev server picks up the new plugin.

🤖 Generated with Claude Code

moufmouf and others added 3 commits August 20, 2026 16:00
The dialog box now lives in packages/npc-dialog-box, an npm workspace
consumed by the map exactly like the future standalone package (it will
move to its own repository once published).

The package builds three artifacts (build.mjs, run on install via
prepare):
- dist/api.js: the main-script API (openDialog/closeDialog)
- dist/npc-dialog.html: the dialog page, fully self-contained - the
  iframe-side code is bundled and inlined, so the page no longer depends
  on the consumer's build to compile it
- dist/vite.js: a Vite plugin that emits npc-dialog.html (stable,
  un-hashed name at the dist root, matching the default dialogUrl) plus
  optional extra assets, and serves the same files on the dev server, so
  dev and production use identical URLs

The map now integrates with one plugin line per Vite config:
npcDialogBox({ assets: ["npc-avatar.png"] }). The wrapper npc-dialog.html
and the rollup input entry are gone from the map project, and the default
avatar moved from the wrapper's <template> to the avatar option.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Lead with what the package does and a screenshot of the result, then go
straight to usage. Implementation details and the plugin-less escape
hatch are dropped.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The package now lives in its own repository
(github.com/workadventure/npc-dialog-box) and is published on npm, so
the packages/ workspace is removed and the dependency resolves to the
published 0.1.1 release.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@moufmouf
moufmouf force-pushed the npc-dialog-library branch from 61611a6 to dd9f629 Compare August 20, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant