Skip to content
Merged
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions packages/ai-mcp/tsup.bin.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ export default defineConfig({
noExternal: ['json-schema-to-typescript', 'jiti'],
// Keep the heavy SDK + workspace pkg external (installed alongside).
external: ['@modelcontextprotocol/sdk', '@tanstack/ai'],
// json-schema-to-typescript uses CJS dynamic require(); shims injects
// createRequire so those calls work inside this ESM bundle.
shims: true,
Comment on lines +13 to +15

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Verify tsup shims option documentation and behavior

# Check tsup documentation for the shims option
echo "=== Checking tsup package.json for version ==="
cat package.json | jq -r '.devDependencies.tsup // .dependencies.tsup // "not found"'

echo -e "\n=== Searching for tsup shims usage in the codebase ==="
rg -n --type=ts 'shims.*true' -g '!node_modules' -C2

Repository: TanStack/ai

Length of output: 553


🌐 Web query:

What does the tsup shims option do for ESM bundles with dynamic require calls?

💡 Result:

The shims option in tsup is designed to provide compatibility polyfills when building code that assumes the presence of Node.js-specific global variables or module system features in formats where they are not natively available [1][2]. Specifically, it injects code to support dirname and filename in ESM bundles (which natively only provide import.meta.url) and provides import.meta.url in CJS bundles [1][2]. While the shims option is intended to bridge these gaps, it is not a comprehensive solution for "dynamic require" errors in ESM bundles [3][4]. 1. Purpose of Shims: When enabled (shims: true), tsup automatically injects polyfills via esbuild's inject configuration [1]. These polyfills define dirname and filename using node:path and node:url utilities within ESM [1]. 2. Limitations with Dynamic Require: Enabling shims does not automatically provide a global require function in ESM [5]. If your code performs dynamic require() calls (a CJS-specific feature), it will fail in an ESM environment because ESM does not natively support require [3][4]. 3. Recommended Workarounds: - Manual Shim Injection: To support dynamic require in ESM, developers must often manually create a require function using createRequire from the node:module package [5][3]. - Configuration Pattern: A common approach involves using tsup's inject option to point to a custom shim file that defines globalThis.require = createRequire(import.meta.url) [5]. Alternatively, some developers use the banner option to prepend this definition to the generated ESM output [3]. In summary, the shims option handles specific variable compatibility (dirname/filename) but does not resolve dynamic require limitations in ESM [1][2]. Resolving dynamic require calls typically requires manual injection of a createRequire polyfill [5][3].

Citations:


Fix tsup shims explanation/mechanism: shims: true usually doesn’t provide createRequire for ESM dynamic require()

packages/ai-mcp/tsup.bin.config.ts comments claim shims: true “inject[s] createRequire” so CJS require() calls from json-schema-to-typescript work in the ESM bundle, but tsup’s shims option is primarily for ESM/Node global compatibility (e.g., dirname/filename) and is not generally a solution for ESM “Dynamic require” failures. To address that crash, use tsup’s inject/banner to define a require via createRequire(import.meta.url), and update the inline comments to match the actual mechanism.

🤖 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 `@packages/ai-mcp/tsup.bin.config.ts` around lines 13 - 15, The comment
claiming "shims: true injects createRequire" is incorrect; update the inline
comment in tsup config to explain that shims only provide ESM/Node global
compatibility and do not polyfill dynamic require, and then add a tsup
injection/banner that defines a require using Node's createRequire (e.g., use
createRequire(import.meta.url) to set a global require helper) so
json-schema-to-typescript's dynamic require() calls will work in the ESM bundle;
specifically modify the tsup config where "shims: true" is set to replace the
comment and add an "inject" or "banner" entry that imports createRequire from
"module" and assigns const require = createRequire(import.meta.url) (or attaches
it to globalThis), ensuring the bundle provides a working require at runtime.

banner: { js: '#!/usr/bin/env node' },
})