Phaser import side effects

Any thoughts on making the Phaser module import pure? As in, not requiring window or canvas etc?

The main reason I ask is that I’d like to run my simple non-dom related classes without jsdom and jest-canvas-mock (ie: faster). However, I do use non-dom utilities from Phaser even in some of these classes (eg: Phaser.Math.Clamp). At this point I have to extract those calls to another component that can then be mocked or use jsdom and the canvas mock just to test the simplest of functions because import Phaser relies on the dom and canvas being available.

I’d expect any code that relies on window or canvas operations to only run once Phaser.Game(config) is called. Maybe I’m missing something or doing something silly?

Phaser v3.53 may make that easier, see the notes on web workers in change log.

If you have some classes that use Phaser.Math only I think you can just import that by itself.

Good call on importing only Math. Not sure how I didn’t think of that. Would still kinda like to be able to import Phaser without the side effects so I’ll look into 3.53. Thanks

Just importing the Math namespace still requires window to be available :cry:

What about

import Clamp from 'phaser/src/math/Clamp';

const Phaser = { Math: { Clamp } };

Yup, importing the individual module’s explicit path works. Looking forward to trying 3.53 to see if even components that rely on dom-specific code can be run without jsdom with mocking