melonjs / melonJS

a fresh, modern & lightweight HTML5 game engine
https://melonjs.org
MIT License
5.93k stars 643 forks source link

Support Isomorphic Apps? #1092

Closed L1lith closed 2 years ago

L1lith commented 2 years ago

When you import melonjs in Node it immediately throws an error because it can't access the window before you even call anything at all. I was thinking it would be cool if it wouldn't access the window unless the libraries actually started rendering because when I try to deploy my isomorphic app it can't run my app inside the Node.js server.

I understand this isn't a big issue for Melon.js overall but I do think it would be cool to have better integration with JS frameworks with Isomorphic rendering like Solid.js and React (with Next.js etc)

I also think this goes along with this issue, and is a good design pattern in my opinion because things probably shouldn't run in a global context, but rather when their respective functions are called. That's my 2 cents anyways, feel free to take this suggestion with a grain of salt.

I haven't deployed MelonJS to an isormophic application yet, but I was trying to do some testing of my app inside Node.js and importing MelonJS was causing the app to break during testing which was a little frustrating.

obiot commented 2 years ago

Hi,

as a first step, I will replace reference to window by globalThis, this will fix access to the global instance for browser, node, and also web workers :

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis https://caniuse.com/mdn-javascript_builtins_globalthis

obiot commented 2 years ago

Hi @L1lith any chance for your to test the last version ?

latest 10.7.0 version using globalThis instead of window has been updated here : https://github.com/melonjs/melonJS/tree/master/dist

thanks!

obiot commented 2 years ago

Hi @L1lith , so many changes as you can see over the weekend, but now melonJS does not crash anymore when executed in node.js 🎉 🎉 🎉

I haven't tried it but even though it's being "loaded" it will still require at least jsdom and node-canvas to work properly.

In case you are testing this further, feel free to share any more return of experience with using those two together with melonJS

Thanks again !

obiot commented 2 years ago

Hi,

with the latest 10.7.0 release, that now allows to run melonJS in node.js without crashing, I will close this ticket.

For further information, and maybe more return of experience mixing melonJS & node.js, I invite you to continue the discussion on the topic I created in our new forum : https://melonjs.discourse.group/t/running-melonjs-in-node-js/17

thanks again @L1lith for the feedback and suggestion !