I wrote a package let's call it commons
and another project that imports it.
I do not want to import the whole package but only one subpath of this package.
import { client } from '@commons/clients'
instead of import { client } from '@commons'
I have seen typescript packages such as jose that enable such behavior.
E.g. import createRemoteJWKSet from 'jose/jwks/remote'
.
After some research, I did see that I can define in package.json
this behavior with exports
key. Unfortunately, this seems to work only for pure js
and not typescript as far as I tested it.
Also, I have found that typescript doesn't support exports
yet in some thread.
How does a package such as jose
manage this?
Asked
Active
Viewed 769 times
3

Hein Re
- 73
- 4
-
2Author of `jose` here. I have given up on that vision. The typescript and bundler ecosystem is not there yet to be able to consume it without friction. I have moved away from this submodule scheme and instead opted in for a flat index export. The friction is gone and so long as the consumers use ESM they get easy tree shaking to remove what they don't actually use. – Mar 14 '22 at 16:18
-
First of all thanks for the quick answer. Could you maybe point to the relevant files in `jose` that makes this possible, or some example on how to do it? I do not know what is this "flat index export" (also some link to an explanation about what is it will be appreciated) and googling this term doesn't give relevant pages. – Hein Re Mar 15 '22 at 13:38
-
You've already linked to the repository, it's all there in the package.json, you can inspect the state of the repo in v3.x vs v4.x using the github tag selection UI to observe the difference. – Mar 15 '22 at 17:31
1 Answers
3
This is supported in TypeScript but not by the default module resolution. To allow TypeScript to resolve the module, you need to use either Node16
or NodeNext
for your moduleResolution
field in tsconfig.json
.

davidmyersdev
- 1,042
- 8
- 10