Skip to content
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

How to transform tsconfig paths (aliases)? #217

Closed
kevinkucharczyk opened this issue Oct 17, 2023 · 5 comments
Closed

How to transform tsconfig paths (aliases)? #217

kevinkucharczyk opened this issue Oct 17, 2023 · 5 comments

Comments

@kevinkucharczyk
Copy link

Hi!

In a TypeScript-enabled v2 addon, is it possible to use tsconfig paths? Or rather, is it possible to add an option to compile those path aliases into real paths in the dist output?

Previously, when rollup-plugin-ts was the recommendation, any custom paths/aliases worked just fine - I could use @rollup/plugin-alias to rewrite the paths during the build.

Now that the build is split into two steps (js and types), this is no longer enough. While the js step still works with the Rollup plugin, I can't do anything to change the output for the types step. All the .d.ts results will have incorrect paths, and any TypeScript consumer of my addon won't work, because the type declarations are incorrect.

Is it possible to add import path rewriting in the build step? Maybe use something like transform-typescript-paths?

I wasn't quite sure where to post this issue, I feel like it really belongs in Glint, but it's also relevant to what the recommended addon structure here is.

@NullVoxPopuli
Copy link
Collaborator

is it possible to use tsconfig paths?

It is, and i've used them for making access to my own private types more convenient pre-publish 🎉

@kevinkucharczyk
Copy link
Author

@NullVoxPopuli could you please elaborate on that? Do you transform the paths in the published .d.ts files at all?

@NullVoxPopuli
Copy link
Collaborator

Naw, I was mistaken -- I had confused an old work project which had an incorrect TS setup.

I tried to replicate what I was thinking here: https://stackblitz.com/edit/stackblitz-starters-ifwkx1

but it does indeed need transformation, as declaration building doesn't compile away the alias.

So, I found that using your suggestion of typescript-transform-paths with ts-patch does the trick.
https://stackblitz.com/edit/stackblitz-starters-ifwkx1?description=Starter%20project%20for%20Node.js,%20a%20JavaScript%20runtime%20built%20on%20Chrome%27s%20V8%20JavaScript%20engine&file=ts-paths-demo%2Fts-paths-demo%2Fdeclarations%2Findex.d.ts%3AL1&title=node.new%20Starter

Here is the result
input:

export type { Foo } from '#types';

output:
image

Note that I can't vouch for how stable this tool is over time, or how it works with changes in ts-versions.
The README of ts-patch made me believe they were doing unspeakable shenanigans. So... be sure to read all the documentation and determine if the risk is worth having type-only aliasing

@kevinkucharczyk
Copy link
Author

@NullVoxPopuli thank you! typescript-transform-paths does indeed work - except for when importing .gts components. Those paths don't get transformed, and I assume it's because of something like LeDDGroup/typescript-transform-paths#144, but I haven't looked further into it yet.

@NullVoxPopuli
Copy link
Collaborator

yea, it's stuff like that for why I'd probably stick with regular relative imports, or try to get all the tools (tsc, rollup, etc) to support native aliases, aka: import subpaths https://nodejs.org/api/packages.html#subpath-imports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants