A starter template for melonJS plugins, in TypeScript, built with esbuild and tested in a headless browser with Vitest.
-
Use this template (or clone it)
-
Install the dependencies, and the browser the tests run in:
npm install npx playwright install chromium -
In
package.json, set your plugin'sname,description,homepageandrepository, and the lowest melonJS version it needs inpeerDependencies -
Rename
MyPluginand write your plugin insrc/index.ts
import { plugin } from "melonjs";
import { MyPlugin } from "my-plugin";
// once the application is created
plugin.register(MyPlugin, "myPlugin");
// then, anywhere
plugin.cache.myPlugin.hello();plugin.register passes any extra arguments to the plugin's constructor. The template takes the application it belongs to, so on a page with several applications:
plugin.register(MyPlugin, "myPlugin", app);| Script | What it does |
|---|---|
npm run build |
bundles src/ into dist/index.js and writes the type declarations |
npm run dev |
the same, rebuilt on every change |
npm test |
runs the tests in tests/ in headless Chromium |
npm run test:types |
type-checks the sources, tests and scripts |
npm run clean |
removes dist/ |
npm publish cleans, builds and tests first.
src/
βββ index.ts # the plugin
tests/
βββ plugin.spec.ts # its tests, run in a real browser
scripts/
βββ build.ts # esbuild configuration
βββ clean.ts
dist/ # generated output (gitignored)
These are the ones the official plugins follow:
- One version to maintain. The lowest melonJS version the plugin runs on is read from
peerDependencies.melonjs, andplugin.registerrefuses to run it on an older engine. The peer range alone only warns at install time, and never when melonJS comes from a CDN. - Use
this.app, not the globalgame: it is the application the plugin was registered for. - melonJS stays external. The build leaves
melonjsout of the bundle, so the game and the plugin share one engine. - Real browser tests. melonJS needs a canvas and a DOM, so the tests create a real
Application(await app.init()) in headless Chromium.
The official plugins in the melonJS repository (debug-plugin, spine-plugin, capacitor-plugin, tiled-inflate-plugin) are good examples to read.
