|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * This script bundles superjson and its dependency (copy-anything) into |
| 5 | + * vendored CJS and ESM bundles to avoid the ERR_REQUIRE_ESM error. |
| 6 | + * |
| 7 | + * superjson v2.x is ESM-only, which causes issues on: |
| 8 | + * - Node.js versions before 22.12.0 (require(ESM) not enabled by default) |
| 9 | + * - AWS Lambda (intentionally disables require(ESM)) |
| 10 | + * |
| 11 | + * Run this script after updating the superjson dependency: |
| 12 | + * node scripts/bundle-superjson.mjs |
| 13 | + * |
| 14 | + * The output files are committed to git so the build doesn't need esbuild at runtime. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as esbuild from "esbuild"; |
| 18 | +import { dirname, join } from "node:path"; |
| 19 | +import { fileURLToPath } from "node:url"; |
| 20 | +import { readFileSync } from "node:fs"; |
| 21 | + |
| 22 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 23 | +const packageRoot = join(__dirname, ".."); |
| 24 | +const vendorDir = join(packageRoot, "src", "v3", "vendor"); |
| 25 | + |
| 26 | +// Get the installed superjson version for the banner |
| 27 | +const superjsonPkg = JSON.parse( |
| 28 | + readFileSync(join(packageRoot, "node_modules", "superjson", "package.json"), "utf-8") |
| 29 | +); |
| 30 | +const banner = `/** |
| 31 | + * Bundled superjson v${superjsonPkg.version} |
| 32 | + * |
| 33 | + * This file is auto-generated by scripts/bundle-superjson.mjs |
| 34 | + * Do not edit directly - run the script to regenerate. |
| 35 | + * |
| 36 | + * Original package: https://github.com/flightcontrolhq/superjson |
| 37 | + * License: MIT |
| 38 | + */`; |
| 39 | + |
| 40 | +async function bundle() { |
| 41 | + // Bundle for CommonJS |
| 42 | + await esbuild.build({ |
| 43 | + entryPoints: [join(packageRoot, "node_modules", "superjson", "dist", "index.js")], |
| 44 | + bundle: true, |
| 45 | + format: "cjs", |
| 46 | + platform: "node", |
| 47 | + target: "node18", |
| 48 | + outfile: join(vendorDir, "superjson.cjs"), |
| 49 | + banner: { js: banner }, |
| 50 | + // Don't minify to keep it debuggable |
| 51 | + minify: false, |
| 52 | + }); |
| 53 | + |
| 54 | + // Bundle for ESM |
| 55 | + await esbuild.build({ |
| 56 | + entryPoints: [join(packageRoot, "node_modules", "superjson", "dist", "index.js")], |
| 57 | + bundle: true, |
| 58 | + format: "esm", |
| 59 | + platform: "node", |
| 60 | + target: "node18", |
| 61 | + outfile: join(vendorDir, "superjson.mjs"), |
| 62 | + banner: { js: banner }, |
| 63 | + minify: false, |
| 64 | + }); |
| 65 | + |
| 66 | + console.log("Bundled superjson v" + superjsonPkg.version); |
| 67 | + console.log(" -> src/v3/vendor/superjson.cjs (CommonJS)"); |
| 68 | + console.log(" -> src/v3/vendor/superjson.mjs (ESM)"); |
| 69 | +} |
| 70 | + |
| 71 | +bundle().catch((err) => { |
| 72 | + console.error(err); |
| 73 | + process.exit(1); |
| 74 | +}); |
0 commit comments