Description
Hey folks! @JakobJingleheimer and I sync'd up this week and discussed a plan for an Import Maps PR. The first step was to start an issue here with a more concrete technical plan, so here it is. Just to be clear: this is a plan proposal, all details are up for discussion I just wanted to get the starting point for those discussions.
Goal
The ultimate goal is import maps enabled by default if a well known file is present (ex ./importmap.json
). If no file is found at that location, the loader is not added to the chain. The file location can be changed with a cli flag (ex --import-map
), which will also turn on the loader and throw if the import map is unavailable or malformed.
The loader should apply its transformations before custom loaders, and it should work well with other existing features (ex exports, policies, etc). I am intentionally leaving some details light on what it means to "work well with other existing features", my hope is we can identify those areas and what requirements we want to put on the implementation in this discussion.
The ideal end user experience here is that their package manager of choice generates an import map for them and it is found/used without additional user intervention. Secondarily, tool authors or users could hand write (or generate) import maps which provide more bespoke features.
Getting There
To get to this end goal, we plan to start with an experimental loader and flag. This initial approach does not need to get 100% coverage, we will take an iterative approach with a goal of 80% of use cases covered in the initial PR. The initial PR will be behind this experimental flag (ex --experimental-import-map
). Before that, we need to decide to either write a new loader or pull in an existing implementation from userland. We have a few examples we can reference:
@joeldenning
's: https://github.com/node-loader/node-loader-import-maps/blob/master/lib/node-import-map-loader.js- my original example: https://gist.github.com/wesleytodd/4399b2351c59438db19a8ffb1f3fcdca
- ...add more here...
Open Questions
- An alternative to not loading the loader would be to always have it but use an empty import map as a fallback. Good idea? Bad Idea?
- Highly likley a no for performance reasons. For reference, the spec includes that all Document's have an empty one but we believe this is not important for our use case.
- what should the flag be (both experimental and final)?
- where should the well known file be by default? (
importmap.json
,.importmap.json
,node_modules/importmap.json
, etc) - Scope resolution: for large import maps it seems like we want to implement a radix tree lookup? Should we even start with a naive loop approach or just do the radix tree right away?
- Should import maps work with commonjs?
Next steps
- open the pr?
- profit?