通过 <script src=""> 加载 JavaScript ES6 模块和在 <script> 标签中导入所有导出是否相同?

Are loading a JavaScript ES6 module via <script src=""> and importing all exports in a <script> tag the same?

无法理解我刚刚在the Deno manual section on the deno bundle command结尾阅读的两个示例之间的区别:

Bundles can also be loaded in the web browser. The bundle is a self-contained ES module, and so the attribute of type must be set to "module". For example:

<script type="module" src="website.bundle.js"></script>

Or you could import it into another ES module to consume:

<script type="module">
  import * as website from "website.bundle.js";
</script>

我的印象是两种形式都达到了相同的效果(即“fetched and executed immediately, before the browser continues to parse the page"), and the latter is used when a script follows or one wants to narrow down what is imported (e.g., as seen in this answer)。


这可能被认为是其他问题的重复(列表见底部),但这些答案对我帮助不大,辅助资源似乎也没有揭示我的假设是否正确。 (另一方面,我很有可能忽略了一些明显的东西,因此必须提高我的阅读理解能力...)

I was under the impression that both forms achieve the same effect

是的,这两者的效果是一样的

(i.e., "fetched and executed immediately, before the browser continues to parse the page"),

不,任何 <scripttype="module" 默认情况下都会 defer,因此加载不会阻止解析。所有延迟的脚本然后按照它们出现的顺序执行,在解析之后和 DOMContentLoaded 触发之前。

and the latter is used when a script follows or one wants to narrow down what is imported (e.g., as seen in this answer).

您想使用哪一个还取决于捆绑包中完成的工作。如果捆绑包仅包含库,并且不会产生任何副作用(即与页面交互、呈现等),那么您可能需要导入它以便可以使用这些功能。

如果它确实有副作用(即呈现给 DOM 的 React 应用程序),并且是独立的,那么只需包含标签就足以启动它