kit
kit copied to clipboard
`declarationMap` option in `svelte-package`
Describe the problem
tsc has a declarationMap compiler option, which is useful for libraries that distribute their source code along with the compiled .js/.d.ts files as it allows IDEs to navigate to the source code when ctrl+clicking imported symbols. see https://www.typescriptlang.org/tsconfig#declarationMap
Describe the proposed solution
a declarationMap option for svelte-package
Alternatives considered
No response
Importance
would make my life easier
Additional Information
No response
What's the use case for distributing your source code along the compiled js/dts files? Where in the wild does that happen already?
It doesn't happen in the wild very much unfortunately because no one even knows about this feature. But it's so much more useful to be able to go to a function's source code than its type declaration or its compiled js. I'm sure if more people knew about it, it would be more common. I do it with all my packages
Dug into this. Turning in declarationMap in the tsconfig.json is enough to create the d.ts.map files, so I don't think we need an extra option here. The URL inside that mapping file is problematic though:
- For Svelte files, it points to a
.svelte.tsfile, but that should be a.svelte.tsfile. The relative path also goes up one directory too far (should be../src/instead of../../src/) - In general, it's a relative path pointing to the source, which isn't available if you only publish the contents of
package
The first one is a proper bug on our side which needs fixing. The second one is a general TypeScript problem, that behavior is not configurable (there's an open issue on the TS tracker for it). The question is if we somehow smooth over it by copying over the sources, too, if we detect that the user wants this, or if it's in the hands of the developer to do the right thing there (not publishing the package directory, rather the whole project and configure the main package.json manually correctly).
I'm leaning towards first fixing the bug on our side and document the behavior.
The use case we are facing is in a monorepo where packages are referencing each other via package.json (not tsconfig.json references). We want developers to be able to navigate to the sources of packages within the monorepo. Enabling declaration map creation is easily done via tsconfig but due to the relative source path in the generated maps going up one directory too far (as the typescript transpiling happens inside a temp directory) "Go to Source Definition" cannot find the actual source. Fixing the paths manually by removing the first ../ fixes that.
#8843 was addressing exactly that issue but was closed in favor of another PR which did not actually fix anything described in this issue.
I really wish either #8843 or #11527 would get merged