-
Notifications
You must be signed in to change notification settings - Fork 53
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
Named exports #70
Named exports #70
Conversation
This PR is related to #66 and mikeal/ipjs#11. |
Can we only upper case the exports that are classes that can be instantiated with “new.” That’s a code convention I’m not willing to part with because differentiating these is really important in pure JS. |
@mikeal Oh I see. I think I usually uppercase "class-like" objects, I haven't thought about only uppercasing only ones you can instantiate with |
One more thing. I was using |
f0eddbb
to
946e1e7
Compare
ugh. it’s really painful that the state of the JS package ecosystem is effectively “can’t use default exports, sorry.” exporting one named import just feels so wrong, but i guess it’s what we have to do. |
Sorry for another force-push. The only change is in the README, I changed one import that shouldn't have been changed. I found this while I double-checked with my current js-unixfs work if things pan out properly. Good news is, with this change I don't get any unexpected TypeScript typechecker errors. |
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.
Looks good to me, suggested few changes, but I also don’t feel very strong about them
The reason I called them |
this makes me wonder if we should just ditch the class wrappers (which I’m one to be blamed for intruding) and instead just do something like: export const { encode, decode, encoder, decoder, code, name } = codec({ name: ‘json’, ... }) that way users choose the name they want by doing I think this is also what @mikeal was advocating for in the past. |
raw and json are preferred for sure. |
OK, I changed to just |
export const { encode, decode, encoder, decoder, code, name } = codec({ name: ‘json’, ... }) This came up in Alan's original ipld/js-dag-cbor#14, that having the wrapper seemed to make it easier to introduce typing on the export. I'm actually fine either way as long as we can maintain consistency. I have some additional pieces I export for js-dag-pb that are not in the standard |
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.
no strong opinion other than just make it work which seems like a pipe dream with ESM
would like to see @Gozala's point resolved about what the codecs export because bundling that as a breaking change would be good before this is merged
Yet another force push. This removed the
And overall it really seems to be a nicer way of doing it. It means less breaking changes (at least for CJS) and we can now have custom names of the imports without much renaming. |
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.
Looks good to me, just want proposed type annotation to ensure that code
in the returned thing is inferred properly.
@@ -11,8 +11,12 @@ | |||
* @param {(data:T) => Uint8Array} options.encode | |||
* @param {(bytes:Uint8Array) => T} options.decode |
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.
Can you please add this to make sure that return complies with interface def
* @param {(bytes:Uint8Array) => T} options.decode | |
* @returns {import('./interface'). BlockCodec<Code, T>} |
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.
@Gozala note that this change forces further changes to BlockCodec
because encoder
and decoder
are part of the return value now, so we then have to adjust the types to:
-export interface BlockCodec<Code extends number, T> extends BlockEncoder<Code, T>, BlockDecoder<Code, T> { }
+export interface BlockCodec<Code extends number, T> extends BlockEncoder<Code, T>, BlockDecoder<Code, T> {
+ encoder: BlockEncoder<Code, T>,
+ decoder: BlockDecoder<Code, T>
+}
Which I've done in my fixup!
, but you may have opinions about that!
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.
Oh good catch, I forgot Codec
was just Encoder
+ Decoder
without those encoder
/ decoder
props. Thanks!
Latest commit deals with the problem that #66 is trying to address and a little bit more. It publishes now with a The other thing this does is fix the
@Gozala you may want to look at 1f91319 to sanity check. It passes here and it passes when this package is consumed by js-car, at least as an ESM. @vmx hopefully you have as much success consuming this as CJS. If it's a +1 from both of you then it's a +1 for me. @vmx if this gets held up by my latest change for whatever reason and you're keen to get this closed out, then feel free to remove that to move forward and I'll pursue that in a separate PR. |
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 tried the current version with my js-ipfs-unixfs branch and it worked without problems. The type checking passed as expected. This is a 👍 from me (I can't approve this PR as I opened it).
@rvagg I'd be happy if you (once @Gozala approved) would merge this PR. I'm a bit nervous about all the auto release magic in this repo. Feel free to fixup/squash/force-push as much as you like.
It also removes the `Codec` class and exports a plain object instead. This has consequences for imports. BREAKING CHANGE: Only use names exports as default exports don't play well with CommonJS + Typescript typing. This means that when you use ESM imports, e.g. the raw codec is no longer imported as ```js import raw from 'multiformats/codecs/raw' ``` but as ```js import * as raw from 'multiformats/codecs/raw' ``` The CJS import for codecs don't change, it's still `const raw = require('multiformats/codecs/raw`. Though other imports change, so ```js import CID from 'multiformats/cid' const CID = require('multiformats/cid') ``` is now ```js import { CID } from 'multiformats/cid' const { CID } = require ('multiformats/cid') ```
- "*" match seems to cause multiple path substitutions microsoft/TypeScript#41284 that packge name prefix seems to resolve - add types field which acts as main in TS
- modify scripts to use newer tsc --bulid command - generate types on prepare so linking to git dependency works - workaround douple substitution by mapping types/* to itself (see microsoft/TypeScript#41284)
merged, but the auto-publish didn't pick up the breaking changes so it's been published as a patch! I think it's gone by the head commit, such kind of sucks. I'm going to fix it by publishing a patch revert and then publish a major with a breaking head. also, @Gozala I had to remove the |
yeah, maybe a good thing you didn't merge this yourself @vmx, here's a post-mortem (mainly for @mikeal because its your publish stack that's broken here in a couple of ways):
|
This PR replaces all default exports with named exports. This change was triggered by using this module with CommonJ together with TypeScript.
As it does change the exports, I went all in an uppercased all the exports that feel more like objects. When I was using this library I wonder why those are all lowercase. I don't know if people agree on that. I'm happy to take those changes back. Please do bikeshed on those names, I'd really like to get this right, so that we don't need to change that in the future again. Let's do one breaking change now.
Also please do take the time to review, I'd like to get this one done rather soon as I currently work on integrating js-multiformats into js-ipfs and the sooner this is merged the less pain causes it for me.