overlookmotel / livepack

Serialize live running code to Javascript
MIT License
30 stars 0 forks source link

Cannot serialize top-level function called `module` in CommonJS file #566

Closed overlookmotel closed 6 months ago

overlookmotel commented 6 months ago

Input:

// CommonJS
function module() {}
exports.x = module;

Output:

export default {x: {exports: void 0}};

The reason is that instrumentation passes module to Livepack's init function, and it adds it to globals as a module object.

This problem only applies to function declarations because they're hoisted to top of scope. const module, let module and class module would all be syntax errors, and var module would still leave the value of module unchanged at start of the file.

Could try to modify the source to prevent this, but probably better to patch Module.prototype.load to capture the module object there.