Documentation
¶
Overview ¶
Command importmap injects an import map before the first module script.
An import map is only read if it appears before the first module script that resolves through it, and only the first one in the document counts. That makes this a position problem rather than a content problem: the map itself is a few bytes of JSON, and all the difficulty is in deciding where "before the first module script" is in a stream that has no tree.
Three things decide it.
A module script is a <script> whose type attribute, with leading and trailing ASCII whitespace stripped, is an ASCII case-insensitive match for "module". The stripping is why the obvious selector is wrong: script[type=module] folds case, because type is on the HTML specification's list of attributes whose values selectors match case-insensitively, but it does not strip, so it misses <script type=" module ">, which browsers do run. So this program selects every script and decides in Go.
A <link rel=modulepreload> resolves through the map too, so it is an anchor as well. rel is a space-separated token list, so the check is per token.
Template content is inert. A module script inside a <template> is never run, so it is not an anchor, and an import map injected inside one would never be read. The library has no tree to ask, so the program keeps a depth counter and decrements it from an end-tag handler.