Closed Bug 1801764 Opened 1 year ago Closed 1 year ago

Import Maps - should report error if multiple maps used?

Categories

(Core :: JavaScript Engine, enhancement)

Firefox 108
enhancement

Tracking

()

RESOLVED INVALID

People

(Reporter: hamishwillee, Unassigned)

References

()

Details

(Keywords: dev-doc-needed)

Importmap spec says:

Only one import map is processed per Document. After the first import map is seen, others will be ignored, with their corresponding script elements generating error events. Similarly, once any modules have been imported, e.g., via import() expressions or script elements with their type attribute set to "module", further import maps will be ignored.

If you create HTML with two script elements containing import maps (there is no actual script imported anywhere) then

  • on Chrome you get a warning that "Multiple import maps are not yet supported. https://crbug.com/927119" - not clear why they should be according to spec
  • Firefox doesn't report anything - load succeeds

I think there should be errors in this case.

This is related to https://bugzilla.mozilla.org/show_bug.cgi?id=1688879

My test HTML was:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Test - IMportmap in script element</title>

<script type="importmap">
{
  "imports": {
    "moment": "/node_modules/moment/src/moment.js",
    "lodash": "/node_modules/lodash-es/lodash.js"
  }
}
</script>

<script type="importmap">
{
  "imports": {
    "moment3": "/node_modules/moment/src/moment.js",
    "lodash3": "/node_modules/lodash-es/lodash.js"
  }
}
</script>
  </head>
  <body>
  <h1>Test - importmap </h1>
     </body>
</html>

I guess "with their corresponding script elements generating error events" might mean that the import is ignored, so if a script imports a module that cannot be resolved to an URL (perhaps because its map was ignored) that is where the error will appear.

IF so, I guess this is not an error sorry!

Yoshi, what do you think?

Flags: needinfo?(allstars.chh)

Hi Hamish

with their corresponding script elements generating error events.

It means the onerror handler of the script elements will be called.

So in your case, if you add the onerror handler in your 2nd import map, the onerror handler will be called.

<!-- add a onError() handler -->
<script type="importmap" onerror="onError()">
{
  "imports": {
    "moment3": "/node_modules/moment/src/moment.js",
    "lodash3": "/node_modules/lodash-es/lodash.js"
  }
}
</script>

<script>
function onError() {
  // Do you error handling here.
}
</script>

(In reply to Hamish Willee from comment #0)

If you create HTML with two script elements containing import maps (there is no actual script imported anywhere) then

  • Firefox doesn't report anything - load succeeds

The 2nd import map isn't actually loaded, you could try 'import("moment3")'
The spec only defines 'Error event should be generated', it didn't mention to display a warning here.

Flags: needinfo?(allstars.chh) → needinfo?(hamishwillee)

Spec link
https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element

Step 31.2
Switch on el's type:
"importmap":
Step 1. If el's relevant global object's import maps allowed is false, then queue an element task on the DOM manipulation task source given el to fire an event named error at el, and return.

Thanks very much. Very helpful. I'll save as invalid.

Status: NEW → RESOLVED
Closed: 1 year ago
Flags: needinfo?(hamishwillee)
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.