Skip to content

Bundle for edge runtimes; per-domain library entries - #6

Merged
fibanachos merged 2 commits into
cookiechain:mainfrom
Vicape7:feat/subpath-exports
Oct 1, 2026
Merged

fibanachos merged 2 commits into
cookiechain:mainfrom
Vicape7:feat/subpath-exports

Conversation

@Vicape7

@Vicape7 Vicape7 commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Makes the library usable from edge runtimes (Cloudflare Workers, Vercel Edge) and lets an app import one flow without bundling every venue SDK. Two commits, so the second can be dropped on its own.

1. fix: import Anchor by name

import anchorPkg from "@coral-xyz/anchor" only works where Anchor resolves to its CommonJS build. Under the workerd / worker / browser conditions it resolves to dist/browser, which has no default export, so esbuild refuses the whole barrel:

src/core/limitOrders.ts:16  No matching export in "@coral-xyz/anchor/dist/browser/index.js" for import "default"

Now named imports in limitOrders, dca, curveOrders, clmm and cpAmm (+ the four tests that copied the pattern). cpAmm also used Anchor's Wallet (NodeWallet), which the browser build does not ship; the throwaway provider wallet is now a plain object that never signs (the cp-amm SDK never signs through it). This commit alone makes import { trade } from "cookie-mcp" bundle for Workers.

2. feat: per-domain library entries

cookie-mcp/trade, /transfer, /stake, /bridge, /bridge-routes, /nft, /domains, /launchpad, /limit-orders, /dca, /liquidity, /quote, /balances, /signer, /context, /submit, /errors. Built from the same tsup config with splitting, so they share module state with the barrel and ./server. The barrel and ./server are unchanged.

Consumer bundle, minified + gzipped, workerd conditions:

import size
{ trade } from "cookie-mcp" 451 KB
{ trade } from "cookie-mcp/trade" 104 KB
{ stake } from "cookie-mcp/stake" 100 KB
{ bridge } from "cookie-mcp/bridge" 106 KB
{ makeOffer } from "cookie-mcp/nft" 109 KB
{ deployToken } from "cookie-mcp/launchpad" 117 KB
{ submitSignedTransaction } from "cookie-mcp/submit" 89 KB

The honest cost: each subpath becomes public API you would be committing to. If you would rather expose fewer (or none), the first commit stands on its own.

src/subpaths.test.ts bundles the barrel and every entry under the workerd conditions and checks exports against the tsup entries, so a future default import (or a subpath added to one side only) fails CI. Reverting commit 1 makes it fail on . and ./dca. esbuild becomes an explicit dev dependency at the version the lockfile already resolves (yarn.lock unchanged).

README ("As a library") documents the subpaths and what Workers need: nodejs_compat, and process.env populated (default from compatibility date 2025-04-01), since config is read at import time. node:net / node:dns used by the launchpad image fetch are available under nodejs_compat.

Checked

  • yarn lint, format:check, typecheck, smoke (57 tools), build pass; test:unit 671 pass. The two failures in imageFile.test.ts are on main too and Windows-only (path separators).
  • From a separate consumer package under Node: subpath and barrel return the same trade / SignatureRequired, and a context set via cookie-mcp/context is read through cookie-mcp.
  • buildCpAmmDeps / buildClmmClient still construct under Node with the Cookie program ids.
  • In workerd (miniflare, nodejs_compat, compatibility date 2024-12-30): node:net, node:dns/promises, node:async_hooks and node:path load and work.

@fibanachos

Copy link
Copy Markdown
Collaborator

LGTM

@fibanachos
fibanachos enabled auto-merge (squash) October 1, 2026 10:05
…wsers

`import anchorPkg from "@coral-xyz/anchor"` only works where Anchor resolves to its CommonJS build.
Under the `workerd` / `worker` / `browser` conditions (Cloudflare Workers, Vercel Edge, any browser
bundler) it resolves to `dist/browser`, which has no default export, so esbuild refuses the whole
barrel. Named imports resolve under both. `cpAmm` also used Anchor's `Wallet` (NodeWallet), which the
browser build does not ship; the throwaway provider wallet is now a plain object that never signs.
The barrel pulls in every venue SDK, so an app that only needs one flow bundles ~450 KB gzipped;
`cookie-mcp/trade` alone is ~100 KB. Each domain gets its own entry in `exports` and tsup, built
with the same `splitting`, so the subpaths share module state (request context, wallet cache) with
the barrel and the server factory. The barrel and `./server` are unchanged.

`subpaths.test.ts` bundles the barrel and every entry under the `workerd` / `worker` / `browser`
conditions, the way an edge consumer would, and checks `exports` against the tsup entries. esbuild
becomes an explicit dev dependency at the version the lockfile already resolves.
auto-merge was automatically disabled October 1, 2026 13:31

Head branch was pushed to by a user without write access

@Vicape7
Vicape7 force-pushed the feat/subpath-exports branch from 75b8efb to 73a94eb Compare October 1, 2026 13:31
@fibanachos
fibanachos merged commit 27dd53e into cookiechain:main Oct 1, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants