Files
copilotkit__copilotkit/packages/shared/tsdown.config.ts
Benjamin Taylor a7d889772e fix(shared): keep Node-only telemetry out of browser build graphs
`@copilotkit/shared` re-exported `telemetry/telemetry-client.ts` from its
root entry. That module imports `@segment/analytics-node`, which imports
`node-fetch`, which imports the Node built-ins `stream`, `http`, `https`
and `zlib`. Browser bundlers resolve the whole static module graph before
they tree-shake, so every browser build of a dependent package printed
"Module ... has been externalized for browser compatibility" warnings,
even when the consumer never touched telemetry.

Measured with Vite 7.3.2 against a consumer that imports only
browser-safe symbols: 663 modules and 4 warnings before, 451 modules and
0 warnings after. `vite optimize` no longer pre-bundles
`@segment/analytics-node` either.

Deferring the import does not fix this. A dynamic import defers
evaluation but keeps the graph edge, so the resolve step still reaches
`node-fetch`. The edge itself has to stay out of the browser entry.

- `isTelemetryDisabled` moves to its own module so the root entry can
  keep exporting it without reaching the client.
- The root entry keeps `isTelemetryDisabled`, the `lambdaClient` surface,
  the sampling helpers, and the `TelemetryCapture` / `TelemetryIdentity`
  types (type-only, so no runtime edge).
- `TelemetryClient` is now reachable at `@copilotkit/shared/telemetry`
  instead of the root. It is our internal metrics client, so no
  application code is expected to import it. A runtime re-export from
  the root would reintroduce the bug, so there is no shim.
- A test walks the value-level import graph from `src/index.ts` and fails
  if it reaches a Node-only package.

Fixes #4151

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-02 08:58:10 -05:00

48 lines
1.2 KiB
TypeScript

import { defineConfig } from "tsdown";
export default defineConfig([
{
// `src/telemetry/index.ts` is a second entry so that the Node-only
// telemetry client is reachable as `@copilotkit/shared/telemetry`. The
// root entry deliberately does not re-export it (#4151).
entry: ["src/index.ts", "src/telemetry/index.ts"],
format: ["esm", "cjs"],
dts: true,
sourcemap: true,
target: "es2022",
outDir: "dist",
unbundle: true,
exports: true,
},
{
entry: ["src/index.ts"],
format: ["umd"],
globalName: "CopilotKitShared",
sourcemap: true,
target: "es2018",
outDir: "dist",
external: [
"zod",
"graphql",
"uuid",
"@ag-ui/core",
"@ag-ui/client",
"partial-json",
],
outputOptions(options) {
options.entryFileNames = "[name].umd.js";
options.globals = {
zod: "Zod",
graphql: "GraphQL",
uuid: "UUID",
"@ag-ui/core": "AgUICore",
"@ag-ui/client": "AgUIClient",
"@segment/analytics-node": "SegmentAnalyticsNode",
chalk: "chalk",
"partial-json": "PartialJSON",
};
return options;
},
},
]);