optional imports using import attributes

Contrary to this optional import idea, I'd like to support optional imports without introducing any new syntax. This idea is similar to weak imports by the same author, the discussion of which has gone stale and upstream proposals have since merged with changes. However, the latter proposal uses syntax proposed from the former, which I believe is unnecessary.

Since import attributes are now part of the ES spec, I'd like to propose behavior for the optional attribute: if the resolution of the specified module fails, then the imported value(s) are undefined. This is intended to supplement the somewhat common usage of optional dependencies in the npm ecosystem and where attributes on <script>s.

As an example, say your file optionally depends on foo. Currently, the only way I'm aware of importing optional modules requires use of gross require() in a try block:

var foo = undefined;
try {
  foo = require("foo").default;
} catch {
  console.debug("module `foo` is unavailable");
}

with proposed behavior of the optional import attribute, then the above code can be simplified to:

import foo from "foo" with { optional: true };

Since no new syntax is introduced, the language stays simple, and the above code does what is obvious: if "foo" is successfully resolved, then foo is the imported module, otherwise it's undefined.

In the other linked discussions, users wanted to be able to handle other helpful cases like fallbacks, but I think think the lack of new syntax in this proposal should mean that other desirable features are out of scope. if a user wants to fallback, then they can use ?? in the rest of the file to handle this. for example:

import { Foo } from "foo" with { optional: true };
import { BackupFoo } from "backup-foo";

const MyFoo = Foo ?? BackupFoo;

I also believe this specification is tree-shakeable, since it can be known before runtime whether Foo is resolved or not, though I'm not intimately familiar with tree shaking to know for sure under what circumstances tree-shaking fails.