WICG / import-maps

How to control the behavior of JavaScript imports
https://html.spec.whatwg.org/multipage/webappapis.html#import-maps
Other
2.66k stars 69 forks source link

Can specifiers be relative schemes ? #215

Closed nestarz closed 4 years ago

nestarz commented 4 years ago

I would like to map some calls to files that have the relative scheme ./src/... to ./dist/... Can I do this using import-maps ? Can it be something like this:

{
    "imports": {
        "./src/foo.js": "./dist/foo.js",
    }
}

And as broader question, can specifiers be any string ?

Thanks

jkrems commented 4 years ago

I think this is mentioned in one of the README examples (https://github.com/WICG/import-maps#general-url-like-specifier-remapping). Afaik URL-like keys will be normalized to full URLs. The following should be equivalent (assuming inline source maps on a page with the base URL https://x.example/app/):

{
    "imports": {
        "./src/foo.mjs": "./dist/foo.mjs",
    }
}

{
    "imports": {
        "https://x.example/app/src/foo.mjs": "./dist/foo.mjs",
    }
}

{
    "imports": {
        "/app/src/foo.mjs": "./dist/foo.mjs",
    }
}

{
    "imports": {
        "https://x.example/app/src/foo.mjs": "https://x.example/app/dist/foo.mjs",
    }
}

And as broader question, can specifiers be any string ?

In JavaScript, I don't think there's a restriction on the specifier string (it can be any JavaScript string). In both the basic HTML spec for imports and in this proposal, there's a separation between "URL-like" specifiers (absolute URLs, relative URLs starting with .///./) and "bare" specifiers. The bare specifiers can be any string that's not URL-like. In other words, there's currently nothing stopping a remapping of 🍕:

{
    "imports": {
        "🍕": "./pizza.mjs",
    }
}