Skip to content

import.meta.document not referring to the module document, but rather the script #14

Open
@aaronmars

Description

@aaronmars

The transform for import.meta.document in html-module-transform.ts generates a selector for the script element that the expression appears in, like:
'script[data-inline-module-script="0"]'

According to the explanation in the MS explainer, import.meta.document is supposed to refer to the HTML Module document, not a the inner <script> element.

I think the fix would be as simple as adding parentElement at the end.

Here's an example source illustrating the issue:

<template id="header">
    <div>HTML Modules</div>
</template>
<script type="module">
    let importDoc = import.meta.document;

    // Shouldn't need this line since `import.meta.document` is supposed to refer to this whole
    //  HTML document. The 
    importDoc = importDoc.parentElement;
    class Component extends HTMLElement {
        constructor() {
            super();
            const shadowRoot = this.attachShadow({ mode: 'open' });
            const template = importDoc.querySelector('#header');
            shadowRoot.appendChild(template.content.cloneNode(true));
        }
    }
    customElements.define('my-component', Component);
</script>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions