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

`importmap` cannot resolve dependencies entered relatively. #285

Closed tachibana-shin closed 1 year ago

tachibana-shin commented 1 year ago

This seems confusing, so I'm going to explain this as follows: I have a directory tree:

- src
   + main.js
   + log.js
+ index.html

I reported the import map as follows:

{
  "imports": {
    "src/main.js": "<prefix>/main.js",
    "src/log.js": "<prefix>/log.js"
  }
}

and in main.js I entered log.js

import "src/log.js" // working
import "./log.js" // not working. Error not resolve path ./log.js

this seems difficult because according to the declaration, when import "./log.js" is interpreted it becomes import "src/log.js" but it does not resolve the path when i declare at importmap.

Example use Blob

<!DOCTYPE html>
<html>

    <body>
        <script>
function createBlob(code) {
  return URL.createObjectURL(new Blob([code], {
    type: "application/javascript"
  }))
}
            document.write(`
\<script type="importmap"\>
{
    "imports" : {
      "src/main.js": "${createBlob(`
import "./log.js"
console.log("hello main.js")
`)}",
          "src/log.js": "${createBlob(`
console.log("hello log.js")
`)}"
    }
}
\<\/script\>`)
        </script>

    <script type="module">
        import "src/main.js"
    </script>

    </body>
</html>
domenic commented 1 year ago

This is a Blob-specific problem.

Import specifier resolution is always relative to the module's URL. (It is not relative to any of the different specifiers you have mapped that to, of which there could be many.)

Blobs have URLs like blob:https://whatwg.org/d0360e2f-caee-469f-9a2f-87d5b0456f6f. You cannot resolve ./log.js relative that URL.

tachibana-shin commented 1 year ago

Any plans for the future? I'm writing an online editor for my library. If there's no workaround, I'm going to have to write a compiler for files into standard input.

domenic commented 1 year ago

No, there are no plans to change how relative URLs resolve relative to blob: URLs.