guybedford / es-module-shims

Shims for new ES modules features on top of the basic modules support in browsers
MIT License
1.54k stars 83 forks source link

Question regarding relative urls in importmap scopes #90

Closed dmail closed 4 years ago

dmail commented 4 years ago

Hi there,

Inside an importmap file I would expect relative urls found in scopes to be resolved against the importmap file url. According to my test, they are resolved against the importer file.

Steps to reproduce

{
  "scopes": {
    "./dist/": {
      "bare": "./dist/bare.js"
    }
  }
}
dist/
  bare.js
  index.js
importmap.importmap
index.html

Actual

An error is displayed in the console saying

Uncaught (in promise) Error: 404 Not Found http://127.0.0.1:8080/dist/dist/bare.js
    at es-module-shims.js:893

Expected

No error in the console

Notes

I expected "./dist/bare.js" to be resolved to http://127.0.0.1:8080/dist/bare.js but it is resolved to http://127.0.0.1:8080/dist/dist/bare.js (dist appear twice).

When I look at reference implementation of importmaps where importmap scopes are normalized I understand that relative url in scopes are resolved against the importmap file base url (which fallbacks to document url for inline importmap). See https://github.com/WICG/import-maps/blob/a3e697d7b29d929e099cc012768245f8fd07597d/reference-implementation/lib/parser.js#L99

Can you confirm it works as designed and that you are spec compliant ?

guybedford commented 4 years ago

@dmail yes these cases are supposed to resolve against the import map file path itself, so if that is not the case then this is a bug.

The resolveAndComposeImportMap function call here - https://github.com/guybedford/es-module-shims/blob/master/src/es-module-shims.js#L231 is supposed to carry through the import map URL as the base URL for resolutions when the map is resolved on load. As far as I can tell looking at the code that is what it is doing.

If you want to look into this further or post a PR please do.

guybedford commented 4 years ago

I just released 0.6.0 with the fix.

dmail commented 4 years ago

Just tested with version 0.6.0 and it works like a charm. Thank you :)