import { NexeCompiler } from "../compiler"; import { wrap } from "../util"; export default async function ( compiler: NexeCompiler, next: () => Promise ) { await next(); compiler.shims.push( wrap( [ "process.__nexe = {};", "const fsPatcher = (function() {", "const module = {exports: {}};", "const exports = module.exports;", '{{file("lib/fs/patch.bundle.js")}}', "return module.exports;", "})()", "fsPatcher.shimFs(process.__nexe);", // Re-apply ENOENT-tolerant realpathSync after shimFs overwrites boot-nexe's patch. // Node 22.22+ / 24 no longer catches ENOENT in ESM finalizeResolution. // ESM resolver captures fs.realpathSync by destructuring at first ESM load, // which happens after the shim runs — so this wrapper is what it captures. "(function(){var _fs=require('fs'),_o=_fs.realpathSync;_fs.realpathSync=function(p,o){try{return _o.call(this,p,o);}catch(e){if(e&&e.code==='ENOENT')return p;throw e;}};_fs.realpathSync.native=_o.native||_o;})()", compiler.options.fs ? "" : "restoreFs();", ].join("\n") //TODO support only restoring specific methods ) ); compiler.shims.push( wrap(` if (process.argv[1] && process.env.NODE_UNIQUE_ID) { const cluster = require('cluster') cluster._setupWorker() delete process.env.NODE_UNIQUE_ID } `) ); compiler.shims.push( wrap(` if (!process.send) { const path = require('path') const entry = path.resolve(path.dirname(process.execPath),${JSON.stringify( compiler.entrypoint )}) process.argv.splice(1,0, entry) } `) ); }