simonw / download-esm

Download ESM modules from npm and jsdelivr
Apache License 2.0
90 stars 4 forks source link

Consider importmaps support #4

Open simonw opened 1 year ago

simonw commented 1 year ago

https://web.dev/import-maps-in-all-modern-browsers/

It might be possible to have it rewrite modules it downloads to use this syntax, and to spit out an import maps block to make that work.

simonw commented 1 year ago

So it would generate this code:

<script type="importmap">
  {
    "imports": {
      "browser-fs-access": "https://unpkg.com/browser-fs-access@0.33.0/dist/index.modern.js"
    }
  }
</script>

And rewrite the JavaScript to use lines like this:

import {fileOpen} from 'browser-fs-access';
simonw commented 1 year ago

I'm going to add a --importmap option which dumps out importmap.json containing this:

{
  "imports": {
    "browser-fs-access": "https://unpkg.com/browser-fs-access@0.33.0/dist/index.modern.js"
  }
}

It's also going to print the full <script> block to stderr, as a convenience.

keybits commented 1 week ago

import-map rails uses the JSPM API for this. Perhaps that could be a fruitful approach?

(Also mentioned in a comment from me on lobse.rs)