【问题标题】:Conditional import with top-level-await in Firefox 85Firefox 85 中使用顶级等待的条件导入
【发布时间】:2021-02-18 15:32:17
【问题描述】:

我正在尝试在我通过 Firefox 85 中的 HTML 加载(或者更确切地说,我不会加载!)的脚本中进行条件导入(ES6 风格的 Javascript)。

这是脚本:(myscript.js)

let ENVIRONMENT_IS_NODE = false;
let require;
if (ENVIRONMENT_IS_NODE) {
  const {createRequire} = await import('module');
  require = createRequire(import.meta.url);
}

这是我的 HTML 页面:(example.html)

<!doctype html>
<html lang="en-us">
  <head>
    <meta charset="utf-8"/>
  </head>
  <body>

    <script type="module" src="myscript.js"></script>
    <!-- I also tried with async, but that did not change anything: -->
    <!--script async type="module" src="myscript.js"></script-->

    HELLO

  </body>
</html>

另外,我知道在 Firefox 中保护此功能的标志,所以我负责将 about:config 中的 javascript.options.experimental.top_level_await 设置为 true

但我在加载页面时仍然在 javascript 控制台中收到以下错误:

Uncaught SyntaxError: top level await is not currently supported

有关信息,只要设置了正确的标志 (#enable-javascript-harmony),Chrome 88 上的同一页面就可以正常加载。

为什么它不能在 Firefox 85 上运行? 我是不是做错了什么?

Herehere 我得到确认,开发工作已经完成并随版本 85 一起发布,应该使用标志启用。

编辑: 我刚刚填写了a bug report。让我们看看开发者怎么说。

【问题讨论】:

    标签: javascript firefox ecmascript-6 async-await es6-modules


    【解决方案1】:

    我在 Bugzilla 上得到了答复:该功能实际上仍在开发中,并且该标志在公开发布中处于非活动状态。

    该标志目前仅在 Nightly 版本中启用,官方文档(在我撰写本文时具有误导性)将予以修复以澄清这一点。

    the bug report

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 2022-09-28
      • 2021-03-26
      • 1970-01-01
      • 2021-09-21
      • 2016-10-04
      • 2021-08-03
      相关资源
      最近更新 更多