Griko Nibras

Fixing tsup 'dynamic require' errors

tl;dr: inject shims using inject configuration with this snippet below:

// tsup.config.ts
export default defineConfig({
  inject: ["inject-shim.ts"],
});

// inject-shim.ts
import shim_module from "node:module";
import shim_path from "node:path";
import shim_url from "node:url";

globalThis.require = shim_module.createRequire(import.meta.url);
globalThis.__filename = shim_url.fileURLToPath(import.meta.url);
globalThis.__dirname = shim_path.dirname(globalThis.__filename);

This will inject contents of inject-shim.ts to all entrypoints without the banner hack.

Huge thanks to Wojciech Maj's for providing his solution!