Esm: Top-level await in imported modules not working

Created on 14 Feb 2018  路  5Comments  路  Source: standard-things/esm

index.js:

require = require("@std/esm")(module, {
  "esm": "js",
  "cjs": true,
  "await": true
});
module.exports = require("./main.mjs").default;

main.mjs:
```typescript 
import sub from './sub.mjs';


`sub.mjs`:
```typescript
await Promise.resolve();
export default {};

node index.js causes SyntaxError: await is only valid in async function. Is this intended behaviour ?

question

All 5 comments

Hi @yuhr!

Yes this is intended we follow variant C of the top-level await proposal which states that top level await is only available in modules without an exports

Update:

I added a link to the variant C proposal in the readme. https://github.com/standard-things/esm/commit/fb94343c93eff661af6ec6aa7f4abadf129faa27

Update:

BTW our implementation is smart enough to know that
export {} or export * from "./nothing-exported" has no real exports too.

Is there any plan to make it configurable like "await": "variant A" ?

Not at this time. I dig variant C and that one seems like the most viable.

Great package. Thank you.
What would you suggest as an alternative to rendering an html string as a module ( as below ). Or rather, if you know, what am I missing architecturally to think this should be allowed? ( Yet isn't per Variant C ):

// home_view.js

import * as utils from '../static/utils.js'
import * as server_utils from '../utils/server_utils'

import sortBar from './sortBar'
import sideBar from '../static/sideBar'
import legList from '../static/legList'

export default async ( posts, votes, count ) => {

  let html = ``

  html += `<div class='app-left-pane'>`
  html += await sortBar( count )
  html += await legList( posts, votes )
  html += `</div>`

  html += `<div class='app-right-pane df fdr'>`
  html += await sideBar()
  html += `</div>`

  return html

}

Hi @59023g!

Variant C is restricting ESM but CJS should still work with module.exports if you want to experiment with that there.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

StphamZ picture StphamZ  路  3Comments

kherock picture kherock  路  3Comments

OmgImAlexis picture OmgImAlexis  路  3Comments

mbostock picture mbostock  路  3Comments

greggb picture greggb  路  3Comments