I’m not primarily a front-end developer, been slowly learning how to deal with the brave new importmaps world.
I learned the hard way what it means that a js library is “fully bundled” and also “esm” (it usually has to be both to work correctly with Rails importmaps). I also understood that some library authors don’t provide fully-bundled esm out of the box, but jsdelivr CDN can auto-bundle them for me if I call bin/importmap
with the --from jsdelivr
option, and use /+esm
at the end of the link.
Additionally, I understood that even if I use --from jsdelivr
the bin/importmap
still uses jspm API to figure out what packages and dependencies are available and needed, and only goes to jsdelivr for the final download.
However, I still fail to understand the following 3 things:
1. Why is it that some packages on jsdelivr have /+esm
and some don’t?
For example:
> bin/importmap pin chartjs-plugin-annotation/+esm --from jsdelivr
Couldn't find any packages in ["chartjs-plugin-annotation/+esm"] on jsdelivr
But if I run this for popperjs/core it works:
> bin/importmap pin @popperjs/core/+esm --from jsdelivr
[downloads correctly]
NOTE: I know that for this specific Chart.js plugin package you don’t need “esm”, but rather “umd”. But my question is not about that, I’m just using it as an example.
2. What is it specifically about the package that determines whether /+esm is available?
I’ve been staring at these 2 package.json files for a long time:
- chartjs-plugin-annotation/package.json at master · chartjs/chartjs-plugin-annotation · GitHub
- popper-core/package.json at master · intercom/popper-core · GitHub
And I can’t figure out or find documentation anywhere that explains how to tell from their code what file paths or “magical” /+esm type links they expose once hosted on a CDN.
3. How can you tell which urls are available at all?
For some libraries I can pin what seems to be any file, but for others, no matter what I try, I get the “not found” error from bin/importmap
. At the same time, the same url works if I use it in the browser.
For example, this file is available directly: https://cdn.jsdelivr.net/npm/chartjs-plugin-annotation/dist/chartjs-plugin-annotation.cjs
But no matter what I try, I cannot pin it with bin/importmaps
:
bin/importmap pin chartjs-plugin-annotation/dist/chartjs-plugin-annotation.cjs --from jsdelivr
I also tried like this:
bin/importmap pin /npm/chartjs-plugin-annotation/dist/chartjs-plugin-annotation.cjs --from jsdelivr
and like this:
bin/importmap pin @chartjs/chartjs-plugin-annotation/dist/chartjs-plugin-annotation.cjs --from jsdelivr
And a few other ways (like omitting /dist/
, etc). Why are they not working? How do I determine the valid pin-compatible urls, where do I get that information from?
Any explanation would be very much appreciated!