Skip to content

Allow importing and exporting from inline module scripts #11202

Open

Description

@justinfagnani

What problem are you trying to solve?

This feature has been discussed over the years as a part of the HTML Modules efforts, but I think that it's relevant and useful standalone.

The feature request is to be able to export values in an inline module script, and import them from another inline module script.

The use cases for this include things like "blocks"-based documents (computational notebooks like ObservableHQ or the Blocks Protocol) that interleave UI elements with scripts. Later scripts need access to data from previous scripts. Currently these system eschew imports for bespoke import/export-like APIs.

Adding this feature would also help HTML Modules make incremental progress.

What solutions exist today?

None?

Maybe you could do this with some combination of import maps and a service worker, or a build script.

How would you solve it?

One potential way to reference the script to import from is by id, with a specifier that starts with #, like:

<html>
  <script id="foo-module" type="module">
    // This is actually available somewhere
    export const foo = 42;
  </script>
  <script id="bar-module" type="module">
    import {foo} from '#foo-module'; // strawman specifier syntax
  </script>
</html>

Another way is to allow inline module scripts to define a specifier that they can be imported with. This approach solves other use cases, like inlining modules that are part of the page's module graph while still allowing them to be imported from other modules without modification of the importing modules.

Anything else?

This is somewhat related to #7367 which would give all module scripts tags an exports object.

Activity

  1. added
    addition/proposal
    needs implementer interest
    on Apr 10, 2025
  2. EisenbergEffect commented on Apr 10, 2025

    @EisenbergEffect

    I'd definitely be in favor of some sort of specifier so that the scripts could participate directly in the module graph.

  3. edbaafi commented on Apr 10, 2025

    @edbaafi

    @justinfagnani I like that syntax in theory, but it might not work as-is, in practice. In browser, a module specifier starting with # is just like any other specifier that the browser doesn't know how to handle unless instructed with an import map:

    <script type="importmap">
        {
          "imports": {
            "#foo": "./foo.js"
          }
        }
      </script>
    <script type="module">
        import {foo} from "#foo";
    </script>
    

    If any existing code has such a # prefix and uses an import map, that's probably fine since one expects an import map to override the default resolution (if there is one) so any code written before this change would still work as expected. Unfortunately # already has a well defined meaning at the beginning of a module specifier in node. I think this might be problematic as a browser developer might start with some imports from inline module scripts and then later need to create such an import map to import a # prefixed specifier, originally written for node. They could then have to change the id of the module script (and all imports of its exports which could be in multiple scripts), just to support resolving that # prefixed module specifier in browser.

    But what about import attributes? It seems perfectly suited for this. I don't think one would set the type key in this case as the type is still module, but the ES spec allows for any number of keys that are "known to the runtime". So what about something like:

    import {foo} from "#foo-module" with { resolution: "inline" };

    or another creative use of an import attribute key/value that the browser runtime would "know" about.

    I think this would have to mean that when the resolution attribute is set, an import map would not override it, thus these two could llve side by side:

    import {foo} from "#foo-module";
    import {foo} from "#foo-module" with { resolution: "inline" };

    with the former resolving based on an import map (if present) to achieve parity with node, and the latter resolving based on your proposed inline module script idea.

  4. EisenbergEffect commented on Apr 10, 2025

    @EisenbergEffect

    I'm firmly against having to use something like an import attribute that is dependent on what mechanism was used to supply the JS module. I'd prefer us to just add something like this:

    <script type="module" specifier="./foo/bar/baz.js">
    export class Foo {}
    </script>
    <script type="module">
    import { Foo } from "./foo/bar/baz.js";
    </script>

    another.js

    import { Foo } from "./foo/bar/baz.js";

    It shouldn't matter how the module was defined or where it comes from. As far as I'm concerned, we just need a way to:

    • Specify how a module script should be inserted into the module graph.
    • Export things from a module script.
  5. edbaafi commented on Apr 10, 2025

    @edbaafi

    @EisenbergEffect that syntax is actually pretty clean!

    But how would that interact with an actual file or import map mapping the same specifier? Which would take precedence? I'm assuming the inline specifier would?

    We already have a defined resolution story for relative URL paths and then we already have a way to override that with import maps. Is the idea is that the specifier attribute would further override both and does adding another way to override what looks like a relative URL really feel better than using an import attribute or maybe an extension to import maps?

    It shouldn't matter how the module was defined or where it comes from

    While the syntax looks clean, isn't that what you're doing? Changing the meaning of syntax that is meant to tell you where something comes from (a relative file in your example) and then if that's what you're trying to do, why isn't import map the right place?

  6. justinfagnani commented on Apr 10, 2025

    @justinfagnani
    Author

    @justinfagnani I like that syntax in theory, but it might not work as-is, in practice. In browser, a module specifier starting with # is just like any other specifier that the browser doesn't know how to handle unless instructed with an import map:

    Yes, all non-relative, non-absolute specifiers are currently bare and require an import map. This issue is proposing that the browser add a new class of specifier for importing from something in the DOM that doesn't require an import map. Import maps could presumably still remap these specifiers.

  7. justinfagnani commented on Apr 10, 2025

    @justinfagnani
    Author

    Unfortunately # already has a well defined meaning at the beginning of a module specifier in node.

    Ouch, this is really unfortunate. #'s use as a fragment marker and to reference elements by ID is widely known, and I would have hoped that someone could have foreseen that browsers might want to use it in the future for DOM integration.

    But if import maps can still override the default DOM-based resolution, then this shouldn't be a problem. #... requires an import map to work now, and it would require the same import map to work even if importing from inline scripts were added. I don't see a back-compat issue.

  8. edbaafi commented on Apr 10, 2025

    @edbaafi

    But if import maps can still override the default DOM-based resolution, then this shouldn't be a problem. #... requires an import map to work now, and it would require the same import map to work even if importing from inline scripts were added. I don't see a back-compat issue.

    Yeah that's what I was trying to say, this wouldn't break existing code. The problem is the amount of refactoring potentially needed if you were A) using inline module scripts and importing the exports in multiple places and B) then wanted to support a node module that used # with the same name. I know it's an edge case, but this honestly seems like one of the cases import attributes were more generalized (beyond just type) for.

  9. justinfagnani commented on Apr 10, 2025

    @justinfagnani
    Author

    The problem is the amount of refactoring potentially needed if you were A) using inline module scripts and importing the exports in multiple places and B) then wanted to support a node module that used # with the same name.

    I think this is very, very, very unlikely to happen, because in Node #-prefixed names are only valid for that specific package, so any import map for them should be scoped to that package. DOM imports require a resource outside of the package - the DOM elements in the page, so a reusable package couldn't reliably import such a thing any. DOM-imports would almost always be used by top-level scripts that wouldn't ever use Node-style self-imports, or the output of some build processes.

    In the extremely unlikely case of a package importing from both itself, and the DOM, using Node's self-import specifiers instead of relative paths, and having a name collision between the two, the package could just choose to not introduce that name collision.

  10. sashafirsov commented on Apr 11, 2025

    @sashafirsov

    I find it quite useful for altering the import tree with local importmap. As for development as for CDN - hosted scripts which have to be patched say by security reasons. Often such override script has a dummy export which disables particular vulnerable module. Quite similar to same in the build toolchain.

  11. edbaafi commented on Apr 11, 2025

    @edbaafi

    I think this is very, very, very unlikely to happen

    Agreed. And ok so a number of issues are now clearer so I've put together a table to highlight the considerations. @justinfagnani I think your original suggestion (A) is the best so far. I'll explain below

    Option Script tag syntax Import syntax [1] Conflicts with built-in relative/absolute resolution? (undesired) [2] Overrides resolution defined in import map? (undesired) [3] Overridden by resolution defined in import map? (desired) [4] Can be set as import map value(desired)
    A <script id="foo" type="module"> import { Foo } from "#foo"; N/A x ✓ ✓
    B <script id="foo" type="module"> import { Foo } from "#foo" with { resolution: "inline" }; N/A x x x
    C <script type="module" specifier="./foo.js"> or <script type="module" specifier="foo"> import { Foo } from "./foo.js"; or import { Foo } from "foo" ✓ (yes but undesired) ? ? ?
    D <script type="module" specifier="foo"> import { Foo } from "foo" N/A x ✓ x
    1. One consideration is whether the proposed syntax can create a scenario where the specifier defined with the script tag conflicts with the existing built-in resolution algorithm. This will happen in any case where non-bare module specifiers are allowed for inline script modules and I think it's problematic. All the browser knows how to do (unless overridden with an import map) is to resolve relative and absolute URL specifiers and these will always point to at most one module. I think creating a scenario where a specifier can point to two different modules (apart from being mapped in an import map), and then the spec has to decide which wins, should be avoided. I think this alone disqualifies C. There is a slight modification laid out in D which uses the same syntax but only allows bare specifiers. This would mostly make this in-line with A, but perhaps the attribute shouldn't be called specifier when only a bare specifier would work?

    2. Another consideration is whether the specifier created with the script tag overrides the resolution defined in an import map. I think the import map's role of being the exclusive place to override module resolution should remain, and each of the options besides C work well with this role of import maps. C already has the question of which module wins when a script's specifier conflicts with a file, so what would make sense here probably depends on the answer to that question. If an inline script module takes precedence over a file based module, then perhaps the story is that inline script modules always take precedence including over import maps? But without an answer to the first question I just left it as a ?.

    3. Conversely, the question of whether the specifier created with the script tag is overridden by an import map is also pretty straightforward given the import map's role. Again with C's question of which module wins when a script's specifier conflicts with a file, I also left this as a ?.

    4. Finally, we have the question of whether the specifier created by the script tag can be set as an import map value. This is where A shines and where my previous suggestion to use import attributes (B) falls behind. Consider A where an import map with { "imports": { "./foo/bar": "#foo"} } causes a module importing "./foo/bar" to import the inline script module with id="foo". @EisenbergEffect this gives the developer the power to specify how a module script should be inserted into the module graph with any arbitrary specifier without the problems associated with the C syntax (basically allowing import maps to be the one place that expected behavior can be changed). D would also allow the script's specifier to be set as an import map value although I think "imports": { "./foo/bar": "foo"} } is less clear that you're mapping the specifier to an inline script module. B fails here because the with { resolution: "inline" } was suggested to essentially put the specifier into a different namespace just for inline script modules, so there would be no way to override this in an import map with the existing import map schema.

    In summary, I think the two options (so far) that make the inline script module specifier story clear and that gives them the most flexibility, are A and D. I think A makes it clearer that you are importing an inline script module (or mapping to one with import maps). If it shouldn't matter how the module was defined or where it comes from (or if we're worried about the meaning of the # prefix in node) then there is option D.

  12. justinfagnani commented on Apr 11, 2025

    @justinfagnani
    Author

    Thanks for that writeup @edbaafi !

  13. justinfagnani commented on Apr 14, 2025

    @justinfagnani
    Author

    So I personally would like to see both A and D implemented. Option A is very nice for exporting between multiple inline script tags that are hand-written in a page, and Option D would be great for inlining modules from a module graph.

  14. edbaafi commented on May 1, 2025

    @edbaafi

    So I personally would like to see both A and D implemented. Option A is very nice for exporting between multiple inline script tags that are hand-written in a page, and Option D would be great for inlining modules from a module graph.

    @justinfagnani Sorry I missed this. Could you explain what you mean by "inlining modules from a module graph"? I think the word "from" is confusing me.

    Are you basically saying that "Option D would allow for inline overrides of modules in a module graph"? If so, are you opposed to doing this in two steps with what option A would specify (the new resolution rule for # prefixed module specifiers) along with the existing import map syntax?

    <script type="importmap">
      {
        "imports": {
          "foo": "#foo"
        }
      }
    </script>
    
    <script id="foo" type="module">
        export const foo = "bar";
    </script>
    
    <script type="module">
    
        // based on the above importmap, the "foo" specifier
        // references the inline module script with id="foo" above
        // and foo === "bar"
    
        import { foo } from "foo";
    
    </script>
    
    

    Basically why I like A is that this update would focus on allowing inline scripts to participate in module graphs through a new default resolution rule (that # prefixed specifiers are resolved to inline module scripts with that id), but overriding specifiers added through inline scripts within a graph would be done just like overriding any specifier with a default resolution rule (or adding a specifier that doesn't have a default rule): through import maps.

  15. justinfagnani commented on May 1, 2025

    @justinfagnani
    Author

    @edbaafi

    @justinfagnani Sorry I missed this. Could you explain what you mean by "inlining modules from a module graph"? I think the word "from" is confusing me.

    Are you basically saying that "Option D would allow for inline overrides of modules in a module graph"?

    Yes. By "from" I mean that you can take a module graph, and inline arbitrary modules from it into HTML. That solves problems people have now with inlining some code for responsiveness and using it in later loaded modules.

    If so, are you opposed to doing this in two steps with what option A would specify (the new resolution rule for # prefixed module specifiers) along with the existing import map syntax?

    I think two steps is great. Option A is easier to directly use, and is useful in HTML modules, and like you show: with import maps it can be used to implement Option D, so it's a great place to start. Options A and D don't interfere with each other, so there's no forward compat issue that I see.

  16. JensRoland commented on Nov 30, 2025

    @JensRoland
    <script type="importmap">
      {
        "imports": {
          "foo": "#foo"
        }
      }
    </script>
    
    <script id="foo" type="module">
        export const foo = "bar";
    </script>
    
    <script type="module">
    
        // based on the above importmap, the "foo" specifier
        // references the inline module script with id="foo" above
        // and foo === "bar"
    
        import { foo } from "foo";
    
    </script>
    

    This seems to solve everyone's use cases while having the benefit of being an extension to the existing relative URL path resolution story rather than a departure from it. I have a working implementation with Import Map and a Service Worker, but it doesn't work on the initial load before the Service Worker has initialized because of this limitation.

  17. added
    topic: imports/exports
    on Feb 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    addition/proposalneeds implementer interesttopic: imports/exports

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions