Skip to content

Commit

Permalink
Feature detection using HTMLScriptElement.supports()
Browse files Browse the repository at this point in the history
The HTMLScriptElement.supports() method was introduced by
whatwg/html#7008.
HTMLScriptElement.supports('importmap') must return true when supported.

Fixes WICG#171
  • Loading branch information
horo-t authored and domenic committed Sep 9, 2021
1 parent a296a42 commit 4dae693
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ _Or, how to control the behavior of JavaScript imports_
- [General URL-like specifier remapping](#general-url-like-specifier-remapping)
- [Mapping away hashes in script filenames](#mapping-away-hashes-in-script-filenames)
- [Remapping doesn't work for `<script>`](#remapping-doesnt-work-for-script)
- [Feature detection](#feature-detection)
- [Scoping examples](#scoping-examples)
- [Multiple versions of the same module](#multiple-versions-of-the-same-module)
- [Scope inheritance](#scope-inheritance)
Expand Down Expand Up @@ -282,6 +283,29 @@ would not: in all classes of browsers, it would attempt to fetch `app.mjs` direc
<script type="module">import "./app.mjs";</script>
```

### Feature detection

If the browser supports [HTMLScriptElement](https://html.spec.whatwg.org/multipage/scripting.html#htmlscriptelement)'s
[supports(type)](https://html.spec.whatwg.org/multipage/scripting.html#dom-script-supports) method,
`HTMLScriptElement.supports('importmap')` must return true.

```js
if (HTMLScriptElement.supports && HTMLScriptElement.supports('importmap')) {
console.log('Your browser supports import maps.');
}
```

#### Monkeypatch HTMLScriptElement.supports() method in HTML spec

In the HTML spec's [HTMLScriptElement](https://html.spec.whatwg.org/multipage/scripting.html#htmlscriptelement)'s
[supports(type)](https://html.spec.whatwg.org/multipage/scripting.html#dom-script-supports) method, before

> 3. Return false.
add the following step:

3. If type is "`importmap`", then return true.

### Scoping examples

#### Multiple versions of the same module
Expand Down

0 comments on commit 4dae693

Please sign in to comment.