webpackglobal-variableswebpack-5fivem

Webpack bundling breaks global object


I'm currently building FiveM resources using TS and Webpack. (For anyone unfamiliar, it's basically building a mod for a game, where you have your enviroment + athe game API and in this case, a global object)

My issue is that, the global object has some very usefull functionalities, like the object exports, where we can share functions between resources.

When I build using tsc alone the exports work as intended, but if I build with webpack, the compiler breaks the object by minimizing it, so it's no longer called exports and so the engine no longer recognizes it.

I managed to temporarily fix it by using global.exports("name", function) instead of plain exports("name", function)

This fix works for small bundles, but when bundling bigger resources (mods) it's completley lost again.

is there any way to force webpack to keep the global object intact and not minified or something?


Solution

  • Turns out I was executing webpack on a folder that did not contain node_modules within it, and so it was compiling all the packages used aswell.

    I'm still completley lost as why that caused to swallow up the global object, but If I execute webpack at that same level as node_modules it does not compile them anymore and the global object is intact.

    If anyone has more info to fill me in I'd appreciate it, but consider this matter closed!