Skip to content

Add easy sync feature detection for new module types #7017

Open
@domenic

Description

@domenic

Spinning off from #6472 (comment)

It is currently a bit nontrivial to feature-detect support for new module types like JSON or CSS modules. You can do something like the following async:

import("data:text/css,", { assert: { type: "css" } }).then(
  () => { console.log("supported"); },
  () => { console.log("not supported"); }
);

but this is kind of annoying.

I think a good API for this would be something like import.meta.supportsType("css") or maybe import.meta.supportsTypeAssert("css").

It's a little unusual for an import.meta API since it's not particularly about the current module. But I think that's OK?

Other places to put this I can think of would be:

  • As a static HTMLScriptElement.supportsTypeAssert("css"). (Not so great since it's window-only and various module types work in more places than just <script> elements.)
  • As a global, e.g. supportsImportTypeAssert("css").
  • As a new metaproperty of import itself, e.g. import.supportsType("css").

/cc @guybedford @dandclark

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