Sign In Register

How can we help you today?

Start a new topic

Write Cloud Code with CommonJS TypeScript

Cloud Code do not support CommonJS module, so can not easy use TypeScript to write Cloud Code.


You can compile CommonJS(ts) to CommonJS(js), and use the compile script, compile CommonJS(js) to Cloud Code(js)


Compile Script: https://gist.github.com/johnsoncodehk/517bbed85ddd08713c0023195464ad69

Description(Chinese): https://johnsoncodehk.github.io/post/typescripts-for-gamesparks-cloud-code-2


I use TypeScript but instead using CommonJS I just write module less code (without import/export keywords in file global scope). I use improved typings with custom declarations of require and requireOnce - yes it's bug prone but works. Improved typings you can find on GitHub: https://github.com/sebas86/GameSparksTypings

1 person likes this

Cool man, You improve quite completely the typings

And I see you also have fork and update my repository, thank you:)


But module less code has a troublesome problem, is global namespace pollution

If the script does not write export or import, all variables in the script are exposed to the global space

That as long as there is the same variable name in the code of the two events, there will be a repeated definition warning


BTW, In other post have more discuss, this post is like dead

https://support.gamesparks.net/support/discussions/topics/1000083479

To avoid global space pollution I just use namespaces and classes. But yes, this is problematic for simple scripts. For simple requests/responses/callbacks I write all code in code block to avoid false warnings and errors during compilation, but this approach also require declaring variables only using 'let' and 'const' keywords.

But yes, this is my temporary solution to get working environment better than on-line editor.

I tried your way because of the need for more manual checks, so I ended up turning to the present way

 

I have rewritten more reliable build scripts and created an example.

Https://github.com/johnsoncodehk/ts2gamesparks

Https://github.com/johnsoncodehk/gamesparks-cloud-code-typescript-example

 

In addition, I refer to your fork to modify my typings, I hope you do not mind

Login to post a comment