On Tue, Sep 1, 2020 at 9:30 AM tcampbell wrote:
> There are examples of code that parse as both Script and as Module yet
have different results. SpiderMonkey will not try to guess. If your
application wishes to take a guess, it could first try parsing as Script
(since that has semantics that most people expect) and if parsing fails it
could try again as Module.
To clarify, the difference involves modules like this:
function foo() {
console.log("hello world");
}
You can run this as a script. It declares a global function `foo`.
You can run this as a module too. But since it doesn't import or export
anything, it won't do anything useful. The function `foo` is module-scope
rather than global. Other scripts and modules can't see it.
That's why if you really don't know, it's best to try parsing it as a
script first, then module as a fallback. But it's even better to just have
clear rules about which files are treated as scripts and which ones are
modules. Your JS developers will probably expect such rules—they'll be
familiar with HTML, for example, where a <script> is always a script,
unless the tag actually says <script type="module">.
-j