Make `import.meta` a `ReferenceError` instead of a `SyntaxError` in non-module code

For example:

function getOwnSrc() {
    try {
        // only available in modules
        return import.meta.url
    } catch {
        // only available in non-modules
        return document.currentScript.src
    }
}

Currently, this throws an un-catchable SyntaxError in non-module code, making it impossible to isomorphically and reliably get the current script's src. [...document.scripts].at(-1).src might work, but then you could run into issues with deferred/async loading.

Changing the un-catchable SyntaxError to a catchable ReferenceError would make it easier to write code that’s isomorphic between modules and non-modules.

Static import declarations would still throw a SyntaxError in non-module code.

I'm confused; getting the source has nothing to do with the contents of it, and you can't parse that source without knowing the type of the script tag.

In my use case, I'm interested in the src attribute (URL), not the source code. I want to grab the host, which is a unique identifier for a chrome extension in its URLs. This identifier can't otherwise easily be passed between the content script (which can't be a module) and an injected script (which can). You could pass it with a custom event, but then the name of that custom event is subject to namespace clashes, which is what using a unique identifier is intended to avoid in the first place. Otherwise, you're left with hard-coding a UUID etc. which also isn't ideal.

Probably a more common use case would be grabbing some serialized data passed from the server as a data- attribute on the current script. I think there's a pretty strong general case for being able to introspect the current script element, and being able to write code to do so that's syntactically valid regardless of ESM would be a quality of life improvement.

I'm not sure how the content (or parse goal) of a URL have any relevance to your ability to capture the URL and pass it wherever you like?

If the parse goal matters, then of course you need the "type" attribute and a parser that can handle it.