T
TyphonJS•2mo ago
geoidesic

Is there any way to avoid 'game' is not defined lint error for vite build?

7:09:30 PM [vite-plugin-svelte] /Users/noeldacosta/code/foundryvtt-final-fantasy/src/components/organisms/item/shared/EffectsTab.svelte:172:768 'game' is not defined
170:
7:09:30 PM [vite-plugin-svelte] /Users/noeldacosta/code/foundryvtt-final-fantasy/src/components/organisms/item/shared/EffectsTab.svelte:172:768 'game' is not defined
170:
6 Replies
Vauxs
Vauxs•2mo ago
Use window.game
geoidesic
geoidesicOP•2mo ago
I suppose I could also move it out of the template
Vauxs
Vauxs•2mo ago
yes
TyphonJS (Michael)
TyphonJS (Michael)•2mo ago
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.
geoidesic
geoidesicOP•2mo ago
I can confirm that globalThis.game does indeed work. I won't pretend to know why 🙂
TyphonJS (Michael)
TyphonJS (Michael)•2mo ago
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.
Want results from more Discord servers?
Add your server