WICG / import-maps

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

Prioritized package mapping #206

Closed sashafirsov closed 4 years ago

sashafirsov commented 4 years ago

While imports:{} is giving 1:1 mapping for particular script, it is way more efficient to define the mapping on top level packages than individually on each JS or for that matter any another resource like CSS.

Proposal.

Add importRoots array to mapping config

"importRoots":
    [ { "from":"@polymer", "to":"http://my-cdn.io/polymer3/@polymer"}
    , { "from":"@my-app" , "to":"/my-app"}
    ]

The array would support the prioritized list so overriding would be possible for same prefix: would be used only the first match occurrence.

With sample mapping above the code

import '@polymer/iron-ajax/iron-ajax.js';
import '@my-app/main.js';

would be translated into

import 'http://my-cdn.io/polymer3/@polymer/iron-ajax/iron-ajax.js';
import '/my-app/main.js';

The mapping of roots are well addressed by multiple build chains and their plugins: webPack, rollUp, RequireJS.

Proposal does not conflict with original imports as meant to be a complimentary.

domenic commented 4 years ago

This is already possible with the existing spec. See the second example in the readme: https://github.com/WICG/import-maps#packages-via-trailing-slashes

sashafirsov commented 4 years ago

@domenic , are you referencing to "Packages" via trailing slashes?

With the slash as denomination it definitely works, a bit odd to fuse into same imports parameter two different concepts( Extension-less and hashes vs and package resolution), especially accounting that in other cases the separate parameter is used( scopes ).

The imports with slashes package resolution it is not clear the priority of longer path vs shorter resolution. I guess it worth to be explicitly stated. If the interface would accept the array instead of hash map, the priority would be defined by the order, i.e. explicit.