"runtime error during function validation" when using a bundler for atlas functions

Hi, I use typescript and prefer to develop locally on my machine rather than the usual editor. To fix this issue, I check and bundle all the typescript and leave all dependencies out, only local typescript files are bundled. I’ve developed a custom build process that produces code that “works” for atlas functions:

const __mongo_modules__ = { exports = {} };
// place bundled code here
exports = __mongo_modules__.exports.handler;

The bundled code uses esbuild and acorn to identify cjs “module.exports” and replace it with “mongo_modules.exports” to prevent any naming conflicts with the atlas runtime. The code sometimes will validate and run successfully on atlas but other times when I try to save it I get a “runtime error during function validation”. Is there is any way to bypass this error and force upload. Typescript and the bundler guarantees there is no dependencies & everything is correct.

If you want to simulate this, use esbuild and mark everything as external using this plugin NodeExternals, use banner and footer to add the __mongo_modules logic at the beginning and end of output file. From there keep the bundle in memory and take the output code and use a ast tool like acorn to remove the “cjs” “exports” references with “mongo_modules” to prevent possible node js problems in the atlas runtime.