WICG / import-maps

How to control the behavior of JavaScript imports
https://html.spec.whatwg.org/multipage/webappapis.html#import-maps
Other
2.65k stars 69 forks source link

global variables (f.e. map `"jquery"` specifier to `globalThis.jquery` variable) #288

Open trusktr opened 1 year ago

trusktr commented 1 year ago

I know this goes against the concept of modules, but it feels like something necessary to help people migrate.

<script type="importmap">
{
  "imports": {
    "jquery": "env:$",
    "three": "env:THREE",
  }
}
</script>

where env:$ causes import * as jQuery from "jquery" to import from a global $ variable, and similar with three.js.

There would be no static analysis in this case. Maybe it would have a runtime check, so that import {blah} from 'jquery' would fail if "blah" in $ is false.


why

I struggle with these sorts of things often. In a lot of cases I don't want to go and spend (possibly valuable and costly) time refactoring existing apps, but I want an easy way to move forward if I can.

trusktr commented 1 year ago

This would be sweet, because with little restructure of an app, new features could start to import things and get modern type support and intellisense simply with npm i @types/jquery without having to configure additional global types.

From a purist module perspective, I can understand we don't want modules to grab things from global. But from a practical standpoint, it seems like it would so incredibly useful, especially for the many many apps that already exist.