-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathbundle-superjson.mjs
More file actions
93 lines (80 loc) · 3.04 KB
/
bundle-superjson.mjs
File metadata and controls
93 lines (80 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/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);
});