Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions lib/codama-generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import JSZip from "jszip";
import fs from "fs";
import path from "path";
import { randomUUID } from "crypto";
import { createRequire } from "module";
import { pathToFileURL } from "url";
import type { Language } from "./codama-types";
import { assertLanguageSupportedForIdl } from "./codama-compat";
import { getCodamaRootNode, getIdlProgramName } from "./idl-utils";
Expand Down Expand Up @@ -91,13 +91,20 @@ export async function generateFromIdl(
case "dart": {
const dartDir = path.join(tmpDir, `${programName}-dart-client`);
fs.mkdirSync(dartDir, { recursive: true });
// codama-renderers-dart@0.4.x mispublishes its ESM entry (exports `import` -> a
// .mjs absent from the tarball); load the working CJS build lazily here so a
// resolution failure can't break the other renderers at module load time.
const renderDart = createRequire(
path.join(process.cwd(), "package.json")
)("codama-renderers-dart")
.renderVisitor as typeof import("codama-renderers-dart").renderVisitor;
// codama-renderers-dart@0.4.x mispublishes its exports map: `import` points at a
// missing .mjs, and its CJS build drags in a large @codama/@solana/.cjs tree Next
// never bundles. Import the real ESM build by path (bundler-ignored so Turbopack
// leaves it as a runtime import) so it reuses the already-bundled .mjs deps the
// other renderers rely on. Lazy so any failure only affects dart.
const dartEntry = pathToFileURL(
path.join(
process.cwd(),
"node_modules/codama-renderers-dart/dist/index.node.js"
)
).href;
const { renderVisitor: renderDart } = (await import(
/* webpackIgnore: true */ /* turbopackIgnore: true */ dartEntry
)) as typeof import("codama-renderers-dart");
Comment on lines +94 to +107

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Check the uncompressed size of the force-included dependency namespaces to evaluate serverless bundle bloat risk.
du -sh node_modules/@codama node_modules/@solana node_modules/@noble node_modules/codama-renderers-dart 2>/dev/null || echo "Run this in the environment where dependencies are installed to see the total size."

Repository: dev-jodee/castaway

Length of output: 272


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the relevant files with line numbers.
sed -n '1,180p' lib/codama-generate.ts | cat -n
printf '\n--- next.config.ts ---\n'
sed -n '1,140p' next.config.ts | cat -n

printf '\n--- package manifest references ---\n'
rg -n 'codama-renderers-dart|`@codama/`|`@solana/`|`@noble/`' package.json pnpm-lock.yaml yarn.lock package-lock.json 2>/dev/null || true

printf '\n--- package size snapshots ---\n'
du -sh node_modules/@codama node_modules/@solana node_modules/@noble node_modules/codama-renderers-dart 2>/dev/null || true

Repository: dev-jodee/castaway

Length of output: 50374


🏁 Script executed:

#!/bin/bash
set -euo pipefail

pkg=node_modules/codama-renderers-dart

printf '--- package.json exports/imports ---\n'
python3 - <<'PY'
import json, pathlib
p = pathlib.Path("node_modules/codama-renderers-dart/package.json")
data = json.loads(p.read_text())
for k in ("name", "version", "type", "main", "module", "exports", "files"):
    if k in data:
        print(f"{k}: {data[k]}")
PY

printf '\n--- dist files ---\n'
find "$pkg/dist" -maxdepth 2 -type f | sed 's#^`#-` #' | sort | head -n 80

printf '\n--- package root files ---\n'
find "$pkg" -maxdepth 1 -type f | sed 's#^`#-` #' | sort

Repository: dev-jodee/castaway

Length of output: 247


🏁 Script executed:

#!/bin/bash
set -euo pipefail

pkg=node_modules/codama-renderers-dart

echo '--- package.json exports/imports ---'
python3 - <<'PY'
import json, pathlib
p = pathlib.Path("node_modules/codama-renderers-dart/package.json")
data = json.loads(p.read_text())
for k in ("name", "version", "type", "main", "module", "exports", "files"):
    if k in data:
        print(f"{k}: {data[k]}")
PY

echo
echo '--- dist files ---'
find "$pkg/dist" -maxdepth 2 -type f | sort | sed 's#^`#-` #'

echo
echo '--- package root files ---'
find "$pkg" -maxdepth 1 -type f | sort | sed 's#^`#-` #'

Repository: dev-jodee/castaway

Length of output: 1520


Patch codama-renderers-dart to remove the runtime import workaround.
codama-renderers-dart@0.4.1 still points node.import at a missing dist/index.node.mjs, so the path-based import is understandable; the follow-on outputFileTracingIncludes globs are the risky part, since they pull in large @codama/@solana/@noble trees. Fixing the package exports would let Next trace only the needed files and drop both the hardcoded path and the broad includes.

📍 Affects 2 files
  • lib/codama-generate.ts#L94-L107 (this comment)
  • next.config.ts#L27-L36
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/codama-generate.ts` around lines 94 - 107, Patch codama-renderers-dart’s
exports so node.import resolves to the existing ESM entry, then simplify
lib/codama-generate.ts by replacing the hardcoded dartEntry path-based import
with the normal package import while preserving lazy loading. Remove the broad
outputFileTracingIncludes workaround in next.config.ts; both sites require
changes, with Next tracing the package’s correctly exported dependencies
directly.

codama.accept(
renderDart(dartDir, {
formatCode: false,
Expand Down
12 changes: 9 additions & 3 deletions next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ const nextConfig: NextConfig = {
"memfs",
],

// codama-renderers-dart's broken exports map defeats Next's dependency tracer, so its
// files are missing from the serverless bundle. Force-include them for the generate route.
// codama-renderers-dart's broken exports map defeats Next's dependency tracer, so it and
// its dependency closure are missing from the serverless bundle. Force-include the ESM
// build's deps (dart + the codama/solana/noble packages it imports) for the generate route.
outputFileTracingIncludes: {
"/api/generate": ["./node_modules/codama-renderers-dart/**/*"],
"/api/generate": [
"./node_modules/codama-renderers-dart/**/*",
"./node_modules/@codama/**/*",
"./node_modules/@solana/**/*",
"./node_modules/@noble/**/*",
],
},

async headers() {
Expand Down
Loading