-
Notifications
You must be signed in to change notification settings - Fork 0
fix: publish dual esm cjs package #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| { | ||
| "profile": "node16", | ||
| "ignoreRules": ["cjs-resolves-to-esm"] | ||
| "profile": "node16" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,11 +35,12 @@ | |
| "type": "module", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.mts", | ||
| "import": "./dist/index.mjs" | ||
| } | ||
| "import": "./dist/index.mjs", | ||
| "require": "./dist/index.cjs" | ||
| }, | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "types": "./dist/index.d.mts", | ||
| "types": "./dist/index.d.cts", | ||
|
Comment on lines
36
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial | 🏗️ Heavy lift 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify tsdown output and type resolution
# Check what declaration files are generated
echo "=== Generated declaration files ==="
fd -e d.ts -e d.mts -e d.cts . dist/ 2>/dev/null || echo "dist/ directory not found"
# Verify attw can validate the package structure
echo -e "\n=== `@arethetypeswrong/cli` analysis ==="
if command -v attw &> /dev/null; then
attw --pack . 2>&1 || echo "attw validation failed or not installed"
else
echo "attw not available in sandbox"
fi
# Check package.json exports resolution
echo -e "\n=== Package exports structure ==="
cat package.json | jq '.exports, .types, .main, .module'Repository: hbmartin/attio-ts-sdk Length of output: 414 🏁 Script executed: # Check for build configuration and tsdown setup
find . -maxdepth 2 -type f \( -name "tsconfig*.json" -o -name "tsdown.config.*" -o -name "build.config.*" \)Repository: hbmartin/attio-ts-sdk Length of output: 100 🏁 Script executed: # Check package.json more thoroughly for build/type setup
cat package.json | head -50Repository: hbmartin/attio-ts-sdk Length of output: 1070 🏁 Script executed: # Look at the actual source structure to understand the project layout
fd -t f -e ts -e tsx -e js . src/ --max-depth 2 2>/dev/null | head -20Repository: hbmartin/attio-ts-sdk Length of output: 506 🌐 Web query:
💡 Result: When building dual ESM and CommonJS (CJS) packages with tsdown, you may encounter the "dual module type hazard," where TypeScript treats types from the ESM and CJS builds as distinct, incompatible entities [1]. This often leads to errors like TS2352 when consumers mix resolution paths [1]. To resolve this and ensure correct type resolution, follow these practices: 1. Use the cjsReexport Option: Recent updates to tsdown (as of March 2026) introduced the dts.cjsReexport: true option [1]. When enabled, tsdown skips the separate CJS declaration compilation pass and instead generates a.d.cts stub that re-exports types from the.d.mts file [1]. This ensures that both ESM and CJS consumers resolve to the same underlying type definitions, effectively eliminating the hazard [1]. 2. Configure package.json Exports: Properly define your package.json to guide consumers to the correct files [2][3]. Use the exports field to map import and require conditions to their respective types and entry points [2][3]: { "exports": { ".": { "import": { "types": "./dist/index.d.ts", "default": "./dist/index.js" }, "require": { "types": "./dist/index.d.cts", "default": "./dist/index.cjs" } } } } 3. Validate with Tooling: Use the Citations:
🏁 Script executed: cat tsdown.config.tsRepository: hbmartin/attio-ts-sdk Length of output: 347 🏁 Script executed: cat tsconfig.jsonRepository: hbmartin/attio-ts-sdk Length of output: 591 🏁 Script executed: # Search for any tsdown or type resolution related discussions/config
rg "cjsReexport|dual module|type.*hazard" --type ts --type jsonRepository: hbmartin/attio-ts-sdk Length of output: 47 🏁 Script executed: # Check if there are any build or type-related issues documented
fd -e md . -maxdepth 3 -type f | xargs grep -l "tsdown\|type.*resolution\|types.*field" 2>/dev/null | head -10Repository: hbmartin/attio-ts-sdk Length of output: 296 🏁 Script executed: # Check the actual exports being auto-generated - is types in the exports conditions?
rg "types.*:" package.json -A 2 -B 2Repository: hbmartin/attio-ts-sdk Length of output: 744 Consider modernizing type declaration configuration for dual packages. The top-level Modern tsdown supports the {
"dts": {
"cjsReexport": true
}
}Alternatively, move the "exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./package.json": "./package.json"
}🤖 Prompt for AI Agents |
||
| "sideEffects": false, | ||
| "files": [ | ||
| "dist" | ||
|
|
@@ -113,5 +114,7 @@ | |
| "**/*.yml" | ||
| ], | ||
| "absolute": false | ||
| } | ||
| }, | ||
| "main": "./dist/index.cjs", | ||
| "module": "./dist/index.mjs" | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
typescondition is missing from theexportsmap. For a dual ESM/CJS package, it is essential to provide specific type definitions for each format to ensure correct resolution by TypeScript and to avoid the 'Dual Package Hazard'. By using nested conditions forimportandrequire, you can explicitly point to the appropriate.d.mtsand.d.ctsfiles. Note that thetypescondition must always be the first entry within each block to be correctly recognized.