When emitting the CJS build, the .cjs / .d.cts files keep the //# sourceMappingURL= comment from the original TS emit, so they reference *.js.map / *.d.ts.map instead of their own *.cjs.map / *.d.cts.map. The companion map files also carry the wrong "file" field (the .js / .d.ts name).
Because the ESM and CJS maps have different mappings, every CJS consumer — Node --enable-source-maps, source-map-support, ts-node/tsx, Jest/Mocha, IDE "go to definition" — resolves to the wrong map and misreports positions.
Repro
zshy's own published package exhibits it:
$ grep sourceMappingURL node_modules/zshy/dist/index.cjs
//# sourceMappingURL=index.js.map # expected index.cjs.map
$ grep '"file"' node_modules/zshy/dist/index.cjs.map
"file":"index.js" # expected "index.cjs"
Same for .d.cts → .d.ts.map.
Cause
In compile.js, the host.writeFile hook renames the output path (.js→.cjs, etc.) but writes data unmodified, so the in-file sourceMappingURL comment and the map "file" field still reference the original names.
Fix
When renaming, also rewrite the contents — the sourceMappingURL comment in .cjs/.d.cts, and the "file" field in .cjs.map/.d.cts.map.
Version
zshy 0.7.3
When emitting the CJS build, the
.cjs/.d.ctsfiles keep the//# sourceMappingURL=comment from the original TS emit, so they reference*.js.map/*.d.ts.mapinstead of their own*.cjs.map/*.d.cts.map. The companion map files also carry the wrong"file"field (the.js/.d.tsname).Because the ESM and CJS maps have different
mappings, every CJS consumer — Node--enable-source-maps,source-map-support, ts-node/tsx, Jest/Mocha, IDE "go to definition" — resolves to the wrong map and misreports positions.Repro
zshy's own published package exhibits it:
Same for
.d.cts→.d.ts.map.Cause
In
compile.js, thehost.writeFilehook renames the output path (.js→.cjs, etc.) but writesdataunmodified, so the in-filesourceMappingURLcomment and the map"file"field still reference the original names.Fix
When renaming, also rewrite the contents — the
sourceMappingURLcomment in.cjs/.d.cts, and the"file"field in.cjs.map/.d.cts.map.Version
zshy 0.7.3