template-vite-ts icon indicating copy to clipboard operation
template-vite-ts copied to clipboard

Is HMR supported?

Open matheusd opened this issue 9 months ago • 1 comments

In the current version, changes to any file cause a full page/game reload.

Is it possible to use Hot Module Replacement (HMR) facilities to cause only the subset of modified code to be reloaded with this template? Is it feasible to use HMR at all with Phaser?

matheusd avatar Apr 30 '25 13:04 matheusd

In this template is not supported, but try to replace this code inside src/main.ts. However, keep in mind that the game will still reload.

import StartGame from './game/main';

let game: Phaser.Game;

function launchGame() {
    game = StartGame('game-container');
}

if (import.meta.hot) {
    import.meta.hot.accept('./game/main', (mod) => {
        if (game) {
            game.destroy(true);
        }
        game = mod.default('game-container');
    });

    import.meta.hot.dispose(() => {
        if (game) {
            game.destroy(true);
        }
    });
}

document.addEventListener('DOMContentLoaded', launchGame);

gammafp avatar Apr 30 '25 13:04 gammafp