[WIP] ESM codemod for GDJS Runtime
Very dirty and not nearly ready to be merged. This PR is mostly for tracking progress.
- [x] Remove namespace declarations
- [x] Auto-generate imports for referenced gdjs symbols
- [ ] Convert extensions too
- [ ] Pre-bundle dependencies
- [ ] Generate an import map for the dependencies
- [ ] Generate modulepreload tags in HTML (to avoid fetch cascade)
- [ ] Convert tests too
- [ ] Ensure all tests pass
- [ ] Ensure all builds are working
Early feedback welcome, although the codemod was not made with readability in mind and may be of lower quality.
Nice start! What do you think is the most difficult task here?
The conversion itself should not be too difficult using a few regexes. There are a few elements that are not difficult per-se, but require substantial changes (and therefore careful design decisions), since we cannot assume everything include is available in the global scope anymore and must import everything we use:
- The loading screen logo (we cannot store it on the global scope, we need to import it)
- The project data (we cannot store it on the global scope, we need to import it)
- Events code generation (we need to add import statements at the top of included files, change how RuntimeScene finds the correct code to run...)
The most tricky part is retro-compatibilty with JS events :/ Since this is a total switch to ESM, projects and extensions that use the global PIXI, Holwer or gdjs namespaces will be broken...
What we can do is, in the codemod, after each export, set the function on the global namespace too:
export const myFunction = () => {
// ...
}
// Legacy namespace support
gdjs.myFunction = myFunction;
But that kind of side-effect would make it impossible to tree-shake anything...
Now that I think about it, we likely will need to do such a namespace hack either way, with the way GDevelop is made... You cannot tell the include system to import only a specific symbol, only a file. Taking full advantage of ESM (importing only a few symbols instead of "namespaces" of symbols) would require much deeper changes to GDevelop.
I am no longer working on this, so I converted this to draft. I keep the PR open so it can be easily found and the code can be reused as a reference if this is to be attempted again in the future.