tsdx: `sources` paths are incorrect in declarationMap files (*.d.ts.map) emitted by tsdx
Current Behavior
When using the declarationMap option in tsconfig.json, the sources array in declaration map files does not contain correct relative paths. Repro steps:
git clone https://github.com/justingrant/ts-declaration-map-repro.gitcd ts-declaration-map-reproyarnyarn run with-tsc- this will simply runtscagainst this repo.- Open
./dist/index.d.ts.map. It will begin with:{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"]. Note the correct relative path to index.tsx yarn run with-tsdxwhich usestdsxinstead oftscbut uses the same TS config.- Open
./dist/index.d.ts.mapagain. Here’s what you’ll see:{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.tsx"]. Note that the path to index.tsx is incorrectly relative to the project root, not relative todist.
Expected behavior
Correct relative path to index.tsx (../src/index.tsx) in the map file’s sources array.
Suggested solution(s)
I’m assuming that there’s a way to configure rollup-plugin-typescript2 so that declaration maps are emitted with the correct paths.
Additional context
Note that only declaration map files are affected. Relative paths in regular sourcemap files works fine.
I’m not sure if this is a problem with tsdx or rollup-plugin-typescript2. But I didn’t see any mention of this problem in rollup-plugin-typescript2’s issue list, so I figured that tsdx may be the culprit because it’s the new kid on the block. I’m assuming (perhaps naively) that if the problem was in rollup-plugin-typescript2 that it would have been reported in its GitHub issues already.
At first I thought that this problem was related to #465, but I think that issue is solely about how to handle the declarationDir config setting which I’m not using at all. Instead, I’m relying on the same outDir folder for both transpiled output and for declaration output.
This issue may be a dupe of #135, but I’m not sure, because #135 has a much more complex config. But the problem does look similar: in both issues the relative path to the source files is missing in declaration maps emitted by tsdx.
BTW, great library! Glad you’re building this.
Your environment
| Software | Version(s) |
|---|---|
| TSDX | tsdx@0.12.3 |
| TypeScript | typescript@3.7.5 |
| Browser | n/a (this is a build issue) |
| npm/Yarn | yarn@1.21.1 |
| Node | v12.13.1 |
| Operating System | MacOS Catalina 10.15.2 |
About this issue
- Original URL
- State: open
- Created 4 years ago
- Comments: 20
@agilgur5 Thank you for your insightful reponse! If only more people know about declarationMap, I don’t think think they would want to work without it. At least not in a monorepo context 😉
Or rather, not yet you didn’t 😅😅😅 See https://github.com/jaredpalmer/tsdx/pull/468#issuecomment-581436403 for some of the other problems around
outDir.Yea that change is fine and I agree I don’t think it necessarily needs a repro either. Though repros are always helpful so a maintainer doesn’t need to, like, create the code themselves. This case is also a bit unique as it’s a silent error, so you’d only notice when things are different from expected.
Ok, gotcha. I think the original case is easier to understand the root cause, but that makes sense. Well, the unit tests don’t check that
index.d.ts.mapis correct – just that it exists – which is the core issue here.I think this is fine as at least I’ve already grasped it and will probably be the one to PR a fix. Thanks for the clarification around the changes!
Since I reviewed #465 / #468, this definitely does seem related to them, as well as #135 . Based on the comments in #135, it sounds like adding
declarationDirmight work as a workaround for this once #468 is merged.A potentially relevant note is that there is this function in the codebase
moveTypes: https://github.com/jaredpalmer/tsdx/blob/4f6de1083393057903a1837fb6658f8058ee832f/src/index.ts#L104-L112 Type definitions originally get generated by rpts2 indist/src/and are then moved todist/by TSDX.I suspect that if TSDX uses, internally as a default,
declarationDir: 'dist/', that might fix the issue, or at least get closer.As they’re generated in
dist/src/instead of the output directorydist/it might also be a bug in rpts2, not really sure at this time.This comment is also still relevant, that
src/isn’t always published with libraries, only type defs. But your issue suggests that the paths are incorrect even ifsrc/was published.