Repository navigation
Cordova
Apache Cordova wraps a web app as a native iOS / Android binary using each platform's system WebView. melonJS games built with Vite (or any bundler) drop into Cordova with no engine changes — WebGL, Canvas2D, audio, gamepads, and touch input all work the same way they do in a browser.
⚠️ Cordova is in low-maintenance mode. The project is still alive at Apache and produces releases, but most active investment in the JS-to-native-webview space has moved to Capacitor. For new mobile projects we recommend Capacitor (Ionic-maintained, modern plugin ecosystem, melonJS-specific glue via@melonjs/capacitor-plugin). This page is kept for existing Cordova-based projects that are not ready to migrate.
npm install -D cordovaUse npx cordova … to invoke the CLI, or install it globally with npm install -g cordova if you prefer.
The simplest setup uses Cordova's default project layout and copies your built game into the www/ folder it expects.
# 1. Create the Cordova project (sibling of your melonJS project, or inside it)
npx cordova create cordova-app com.example.mygame "My Game"
cd cordova-app
# 2. Add platforms
npx cordova platform add ios # macOS only
npx cordova platform add android
# 3. From your melonJS project, build into dist/
# (run this from the melonJS project root)
npm run build
# 4. Copy the built bundle into Cordova's www folder
rm -rf cordova-app/www/*
cp -R dist/* cordova-app/www/Re-run steps 3 and 4 after every code change. You can wire it into an npm script for convenience:
{
"scripts": {
"build:cordova": "npm run build && rm -rf cordova-app/www/* && cp -R dist/* cordova-app/www/"
}
}cd cordova-app
npx cordova run ios # iOS Simulator (or attached device)
npx cordova run android # Android emulator (or attached device)
# or, build only:
npx cordova build ios
npx cordova build androidFor physical iOS devices you'll need to open the platforms/ios/*.xcworkspace in Xcode and sign with your developer team. Android signing is handled by cordova-android's default debug keystore for dev builds.
Cordova exposes lifecycle events via document events (not a JS plugin like @capacitor/app). The relevant ones for a game:
document.addEventListener("deviceready", () => {
// safe to start the game now
}, false);
document.addEventListener("backbutton", (evt) => {
evt.preventDefault();
state.change(state.MENU); // or navigator.app.exitApp() to quit
}, false);You don't need to handle pause and resume yourself. melonJS already pauses the game when the app goes to the background: the WebView fires visibilitychange, and the application's pauseOnBlur flag (on by default) pauses the game, pauses its music and mutes its sound effects. resumeOnFocus (also on by default) brings it all back.
If a WebView doesn't fire visibilitychange reliably, make Cordova's own events the ones melonJS follows (melonJS 20.15 and later):
import { device, event } from "melonjs";
device.setFocusEvents(false); // the page's focus and visibility no longer count
document.addEventListener("pause", () => event.emit(event.BLUR), false);
document.addEventListener("resume", () => event.emit(event.FOCUS), false);The same flags then decide what happens. Calling state.pause() and state.resume() from these events instead would fight the engine's own pause: the game could resume while a pause menu is still open.
-
No melonJS-specific Cordova plugin. Unlike the Capacitor path, there's no
@melonjs/cordova-plugin— the lifecycle / back-button wiring is just a fewaddEventListenercalls (see above). -
Plugin ecosystem. Cordova has a long-running plugin registry at cordova.apache.org/plugins for camera, filesystem, in-app purchases, etc. They install via
cordova plugin add <name>. - iOS WKWebView vs UIWebView. Modern Cordova (12.x+) uses WKWebView by default — older guides referencing UIWebView are out of date.
-
Asset paths. Cordova serves from
file://on most platforms — use relative paths in your bundler config.
- Apache Cordova docs
- Cordova plugin registry
-
Capacitor — modern alternative with a dedicated
@melonjs/capacitor-plugin - Platforms: the other platforms melonJS runs on