Is there any way to avoid 'game' is not defined lint error for vite build?
6 Replies
Use
window.game
I suppose I could also move it out of the template
yes
I suppose that is one way. I'd use
globalThis.game
instead. The problem using game
in a template is that the Svelte compiler has no idea about the global runtime environment that you are running your code in.
There is also gameState
from #runtime/svelte/store/fvtt
. Which is a store wrapping game
.
If globalThis.game
does work then I'll actually remove gameState
.
0.2.0
is all about removing things that are unnecessary in an effort to streamline TRL. So good timing on this question. I wasn't aware offhand that you can use window.XXX
or globalThis.XXX
in a template because I rarely access game
in a template.I can confirm that
globalThis.game
does indeed work. I won't pretend to know why 🙂globalThis
is the ESM equivalent of window
in the browser context. On Node particularly global
is symnonomous with window
, but for the Node runtime. ESM solves the problem of being able to refer to the global reference of the runtime with globalThis
. You can write ESM code that will work on Node and in the browser without specifically referring to one particular runtimes global variable.
The Svelte compiler knows about globalThis
/ window
.. It certainly is a preference on my side to always use globalThis
everywhere instead of window
, etc. I have a lot of code that runs on Node and in the browser, so it's a habit.