multiformats / js-multiformats

Multiformats interface (multihash, multicodec, multibase and CID)
Other
223 stars 51 forks source link

Loading through a script tag misses many exports #280

Open eliemichel opened 8 months ago

eliemichel commented 8 months ago

If I try to simply include microformats using the script tag provided in the readme, I can only get CID, bytes, digest, hasher and varint:

<script src="https://unpkg.com/multiformats/dist/index.min.js"></script>
<script>
console.log(Multiformats);
</script>

image

Either the README misses something, or the dist/index.min.js does! For instance how can I get codecs from multiformats/codecs?

rvagg commented 8 months ago

yeah, so that would be bundling https://github.com/multiformats/js-multiformats/blob/master/src/index.js but it sounds like you're after a more comprehensive https://github.com/multiformats/js-multiformats/blob/master/src/basics.js

@achingbrain what's the logic for aegir build and does it afford any ability to also bundle basics.js into a single package or is this too much of a technical stretch with the tools we have?

@eliemichel if you have the ability to bundle & serve a package for yourself, then basics.js would be the one you want if you don't want to be more selective in the pieces you want. It sounds like you're wanting something externally hosted though.

achingbrain commented 8 months ago

Currently aegir is hard-coded to bundle the index file into index.min.js but it's because that code predates the rise of the exports map so it assumes a single entry point for a given module.

If a package.json has an exports map, there's no reason why we couldn't build a separate bundle for every entry instead.

eliemichel commented 8 months ago

@eliemichel if you have the ability to bundle & serve a package for yourself, then basics.js would be the one you want if you don't want to be more selective in the pieces you want. It sounds like you're wanting something externally hosted though.

I typically use direct script tags when I want to quickly test a library from a single index.html file without setting up any sort of webpack and whatnot, to decide whether I will actually adopt it for a more solid project (however fast my package manager is, no package manager at all remains the fastest path for quick testing). So... if I have to build my own dist it kind of defeats the purpose.

In this use case, I am ok with downloading a couple of .js files to serve them next to my index.html, though the unpkg CDN is a nice plus.