Closed Bug 1693570 Opened 5 years ago Closed 5 years ago

Error with conditional import using top-level-await

Categories

(Core :: JavaScript Engine, task, P2)

Firefox 85
task

Tracking

()

RESOLVED DUPLICATE of bug 1681046

People

(Reporter: maus.almeo, Unassigned)

Details

User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36

Steps to reproduce:

I'm trying to do a conditional import (Javascript of ES6 flavour) in a script that I load (or rather, that I would not load!) through HTML in Firefox 85.

Here is the script: (myscript.js)

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

Here is my HTML page: (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>

Also, I am aware of the flag protecting this feature in Firefox, so I took care of setting javascript.options.experimental.top_level_await to true in about:config.

I then launched a simple server in the same directory as these two files:
$ python3 -m http.server 9006

And then I launched Firefox:
$ firefox http://localhost:9006/example.html

Actual results:

But I still get the following error in the javascript console upon loading the page:

Uncaught SyntaxError: top level await is not currently supported

Expected results:

For information, the same page is loading fine on Chrome 88, provided the proper flag is set too (#enable-javascript-harmony).

Here and here I got the confirmation that the development work is already done and shipped with the release 85, and should be enabled with the flag.

The pref has an effect only on Nightly build.
Will fix the documentation to mention that.

https://searchfox.org/mozilla-central/rev/e84baf5c730abd453be5d50dcb0aba1e743bac47/js/xpconnect/src/XPCJSContext.cpp#1028-1032

  // Require top level await disabled outside of nightly.
  bool topLevelAwaitEnabled = false;
#ifdef NIGHTLY_BUILD
  topLevelAwaitEnabled =
      Preferences::GetBool(JS_OPTIONS_DOT_STR "experimental.top_level_await");
#endif
Component: Untriaged → JavaScript Engine
Product: Firefox → Core

Hi, thank you a lot for your help!! This solves my problem.

Is there a planned public release for which the flag will be enabled, though?

(I suppose it will not be limited to Nightly forever)

sorry, forgot to link.
it's tracked under bug 1681046.

Depends on: 1681046

Thanks a lot!! Ok, I see it is under active development!! ^^

Hi Almeo,

Thanks for getting in touch. With regards to the public release: we have plans to release this soon. The specification is currently undergoing a change, which will alter the loading behaviour if it lands. This should be decided sometime in early March. We have a branch with the proposed new behaviour and are just waiting on that.

Severity: -- → N/A
Type: defect → task
Priority: -- → P2

I don't think we need this bug now that TLA is enabled.

Status: UNCONFIRMED → RESOLVED
Closed: 5 years ago
Resolution: --- → DUPLICATE
No longer depends on: 1681046
You need to log in before you can comment on or make changes to this bug.