Files
Eric Allam 3925f8cc49 fix(core): vendor superjson to fix ESM/CJS compatibility (#2949)
Bundle superjson and its dependency (copy-anything) during build to
avoid
ERR_REQUIRE_ESM errors on Node.js versions that don't support
require(ESM)
by default (< 22.12.0) and AWS Lambda which intentionally disables it.

- Add scripts/bundle-superjson.mjs to bundle superjson with esbuild
- Update build script to bundle vendor files before tshy compilation
- Move superjson from dependencies to devDependencies
- Update imports to use vendored bundles

Fixes #2937
<!-- devin-review-badge-begin -->

---

<a
href="https://app.devin.ai/review/triggerdotdev/trigger.dev/pull/2949">
  <picture>
<source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-open-in-devin-review-dark.svg?v=1">
<img
src="https://static.devin.ai/assets/gh-open-in-devin-review-light.svg?v=1"
alt="Open with Devin">
  </picture>
</a>
<!-- devin-review-badge-end -->

---------

Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: Eric Allam <ericallam@users.noreply.github.com>
2026-01-30 09:15:02 +00:00

94 lines
3.0 KiB
JavaScript

#!/usr/bin/env node
/**
* This script bundles superjson and its dependency (copy-anything) into
* vendored CJS and ESM bundles to avoid the ERR_REQUIRE_ESM error.
*
* superjson v2.x is ESM-only, which causes issues on:
* - Node.js versions before 22.12.0 (require(ESM) not enabled by default)
* - AWS Lambda (intentionally disables require(ESM))
*
* The output files are gitignored and regenerated during each build.
* This script runs automatically as part of `pnpm run build`.
*
* Usage:
* node scripts/bundle-superjson.mjs # Bundle to src/v3/vendor
* node scripts/bundle-superjson.mjs --copy # Also copy to dist directories
*/
import * as esbuild from "esbuild";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { readFileSync, mkdirSync, copyFileSync } from "node:fs";
const __dirname = dirname(fileURLToPath(import.meta.url));
const packageRoot = join(__dirname, "..");
const vendorDir = join(packageRoot, "src", "v3", "vendor");
// Get the installed superjson version for the banner
const superjsonPkg = JSON.parse(
readFileSync(join(packageRoot, "node_modules", "superjson", "package.json"), "utf-8")
);
const banner = `/**
* Bundled superjson v${superjsonPkg.version}
*
* This file is auto-generated by scripts/bundle-superjson.mjs
* Do not edit directly - run the script to regenerate.
*
* Original package: https://github.com/flightcontrolhq/superjson
* License: MIT
*/`;
async function bundle() {
// Ensure vendor directory exists
mkdirSync(vendorDir, { recursive: true });
// Bundle for CommonJS
await esbuild.build({
entryPoints: [join(packageRoot, "node_modules", "superjson", "dist", "index.js")],
bundle: true,
format: "cjs",
platform: "node",
target: "node18",
outfile: join(vendorDir, "superjson.cjs"),
banner: { js: banner },
// Don't minify to keep it debuggable
minify: false,
});
// Bundle for ESM
await esbuild.build({
entryPoints: [join(packageRoot, "node_modules", "superjson", "dist", "index.js")],
bundle: true,
format: "esm",
platform: "node",
target: "node18",
outfile: join(vendorDir, "superjson.mjs"),
banner: { js: banner },
minify: false,
});
console.log("Bundled superjson v" + superjsonPkg.version);
console.log(" -> src/v3/vendor/superjson.cjs (CommonJS)");
console.log(" -> src/v3/vendor/superjson.mjs (ESM)");
// Copy to dist directories if --copy flag is passed
if (process.argv.includes("--copy")) {
const distCommonjsVendor = join(packageRoot, "dist", "commonjs", "v3", "vendor");
const distEsmVendor = join(packageRoot, "dist", "esm", "v3", "vendor");
mkdirSync(distCommonjsVendor, { recursive: true });
mkdirSync(distEsmVendor, { recursive: true });
copyFileSync(join(vendorDir, "superjson.cjs"), join(distCommonjsVendor, "superjson.cjs"));
copyFileSync(join(vendorDir, "superjson.mjs"), join(distEsmVendor, "superjson.mjs"));
console.log("Copied to dist directories");
}
}
bundle().catch((err) => {
console.error(err);
process.exit(1);
});