Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active October 19, 2024 12:44

Revisions

  1. WebReflection revised this gist Oct 18, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion importma.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # A Runtime ImportMap Example
    # A Runtime ImportMap Example - [now as a module](https://github.com/WebReflection/importmap)

    While it's not possible to define a `<script type="importmap">` within a module, it is possible to define it in a synchronous `<script>` tag, as long as it's before any *module* starts executing.

  2. WebReflection revised this gist May 1, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion importma.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    While it's not possible to define a `<script type="importmap">` within a module, it is possible to define it in a synchronous `<script>` tag, as long as it's before any *module* starts executing.

    **[Example](https://codepen.io/WebReflection/pen/xxrmXQj?editors=1000)** <sup><sub>(works in Chrome / Edge)</sub></sup>
    **[Example](https://codepen.io/WebReflection/pen/xxrmXQj?editors=1000)** <sup><sub>(works in Chrome / Edge / WebKit / Safari / Firefox)</sub></sup>

    ```html
    <!DOCTYPE html>
  3. WebReflection revised this gist Oct 19, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion importma.md
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ While it's not possible to define a `<script type="importmap">` within a module,
    new Script;
    }
    catch (o_O) {
    imports['vanilla-elements'] = 'https://cdn.skypack.dev/vanilla-elements/es.js';
    imports['vanilla-elements'] = 'https://cdn.skypack.dev/vanilla-elements/poly';
    }
    // the importmap script
  4. WebReflection revised this gist Sep 28, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion importma.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    While it's not possible to define a `<script type="importmap">` within a module, it is possible to define it in a synchronous `<script>` tag, as long as it's before any *module* starts executing.

    **[Example](https://codepen.io/WebReflection/pen/xxrmXQj?editors=1000)** (works in Chrome / Edge)
    **[Example](https://codepen.io/WebReflection/pen/xxrmXQj?editors=1000)** <sup><sub>(works in Chrome / Edge)</sub></sup>

    ```html
    <!DOCTYPE html>
  5. WebReflection revised this gist Sep 28, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion importma.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    While it's not possible to define a `<script type="importmap">` within a module, it is possible to define it in a synchronous `<script>` tag, as long as it's before any *module* starts executing.

    **Example**
    **[Example](https://codepen.io/WebReflection/pen/xxrmXQj?editors=1000)** (works in Chrome / Edge)

    ```html
    <!DOCTYPE html>
  6. WebReflection created this gist Sep 28, 2021.
    49 changes: 49 additions & 0 deletions importma.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    # A Runtime ImportMap Example

    While it's not possible to define a `<script type="importmap">` within a module, it is possible to define it in a synchronous `<script>` tag, as long as it's before any *module* starts executing.

    **Example**

    ```html
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <!-- a synchronous script -->
    <script>
    (() => {
    // any needed import path
    const imports = {
    'vanilla-elements': 'https://cdn.skypack.dev/vanilla-elements/'
    };
    // any useful feature detection (builtin extends in this case)
    try {
    class Script extends HTMLScriptElement {}
    customElements.define('no-💩', Script, {extends: 'script'});
    new Script;
    }
    catch (o_O) {
    imports['vanilla-elements'] = 'https://cdn.skypack.dev/vanilla-elements/es.js';
    }
    // the importmap script
    const script = document.createElement('script');
    script.type = 'importmap';
    script.textContent = JSON.stringify({imports});
    document.head.appendChild(script);
    })();
    </script>

    <!-- any module -->
    <script type="module">
    import {define, HTML} from 'vanilla-elements';
    define('hello-world', class extends HTML.Body {
    connectedCallback() {
    this.innerHTML = '<h1>Hello World 👋</h1>';
    }
    });
    </script>
    </head>
    <body is="hello-world"></body>
    </html>
    ```