Skip to content

[import maps] Ability to re-write more dynamically (globs?) #11919

@trusktr

Description

@trusktr

What is the issue with the HTML Standard?

Sometimes modules might have import statements like this:

import something from 'some-lib/some/thing'

where thing could be

  • a folder
  • or a JS file.

We want to map those cases to

  • some-lib/some/thing.js
  • or some-lib/some/thing/index.js

depending on the lib (or sometimes both are found in one lib).

Sometimes modules may also import like this:

import something from 'some-lib/some/thing/'

which also implies to import some-lib/some/thing/index.js.

Solution?

How can we make this possible to do with importmap scripts? We need to be able to somehow specify a fallback. For example, for a lib that has both path/foo statements that point to both path/foo.js or path/foo/index.js, it gets tricky.

In a ServiceWorker, I can fetch first by appending .js to the URL, and then I can fall back to appending /index.js to the URL, and finally returning the result to the client.

How can we do the same with import maps?

Random idea, some sort of syntax:

{
  "imports": {
    "some-lib": "/node_modules/some/thing@{.js,/index.js}"
  }
}

Example in the wild

Here's a worst-case example, a library in the wild relying on both .js and /index.js being implied on their import statements:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions