-
Notifications
You must be signed in to change notification settings - Fork 53
fix: add types location for cjs/src/ #66
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
Conversation
Now we're publishing a `"main"`, TypeScript is using that rather than the `"exports"` map. It points to the CJS published version, located in cjs/src in the bundle. This adds a descriptor that tells TypeScript to look for types for that location in the types/, which should be the same as to what it finds in the export map. Ref: #64 (comment)
OK, some further diagnosis of the problem this is fixing: These fail: /* @typedef {import('multiformats').CID} CID */
import { CID } from 'multiformats' But these don't: import CID from 'multiformats/cid
import raw from 'multiformats/codecs/raw So I'd assume from that that TypeScript is reading I don't know the impact on consuming these things via CJS, we may be doing that in the js-ipfs stack so it might be worth figuring out. |
As far as I know:
I think best way to figure out what resolution does is to run |
I've tested this PR with ipfs/js-ipfs-unixfs#116, which is using CommonJS modules and it works for me. I don't see TypeScript failures. |
Thanks for the explanation @Gozala. I think it might be best to isolate this to just the Thanks for testing @vmx! Great news. I'll push this forward tomorrow. |
FWIW this will error with
|
@achingbrain mentioned having issues with this today, which I think would be addressed by adding |
@@ -121,6 +121,9 @@ | |||
"*": { | |||
"*": [ | |||
"types/*" | |||
], | |||
"cjs/src/*": [ | |||
"types/*" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure I understand what this change does. Do we ever import path like multiformats/cjs/src/...
because that is what I think it would catch here.
If this is about fixing
import { CID } from 'multiformats'
I think suggestion above is probably adding following to package.json
is a better way to go:
{
"types": "./types/index.d.ts"
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment below #66 (comment)
this is for when we publish a "main"
pointing to cjs/src/index.js, that mapping causes problems for consumers of this package
Looking at this PR I'm realizing that source typedef maps generated are probably going to be broken. Because generated types def maps will point to files in Seems like ipjs should use a better strategy to do publishing otherwise paths mapping going to be a pain. |
hah, yeah ... https://unpkg.com/browse/[email protected]/types/index.d.ts.map What are the implications of this for users @Gozala? I don't know how these .ts.map files are consumed. |
When you click on hints in vscode, it won't be able to take you to definition in the actual source file. I'm guessing it would take you to either d.ts file instead or nowhere, because |
This is a reply to comment #66 (comment)
In commonjs modules, these kind of imports don't work (same with and without this PR). When I do const CID = require('multiformats/cid') I get
I also tried adding
but that didn't help either. |
I think the problem is that from TS perspective From my reading of Overall I think we would make our lives easier if we just used named exports as opposed to default exports as various tools seems to make incompatible decisions about how to interpret default exports resulting in this kind of problems. |
After having a quick look (I don't really consider myself having a good understanding of this whole CJS/ESM/TS issues), I agree named exports might be the cleanest/easiest solution. I also found https://humanwhocodes.com/blog/2019/01/stop-using-default-exports-javascript-module/ which I found quite convincing. |
I've included a version of this in #70, and I'll describe it there. Using |
😮 I do not understand. So here is the link to module resolution strategy that TS uses https://www.typescriptlang.org/docs/handbook/module-resolution.html#how-typescript-resolves-modules quoting relevant bits below:
|
I'll post updates in the ipld/js-car#7 |
Figured out what the problem was and submitted #72 to address it. TL;DR TS bug microsoft/TypeScript#41284 causes double path substitution when "types" field is present resulting in a wrong path. |
Now we're publishing a
"main"
, TypeScript is using that rather than the"exports"
map. It points to the CJS published version, located in cjs/srcin the bundle. This adds a descriptor that tells TypeScript to look for types
for that location in the types/, which should be the same as to what it finds
in the export map.
Ref: #64 (comment)