Skip to content

Avoid top-level await #1

@remcohaszing

Description

@remcohaszing

Currently imports are converted to dynamic imports, for example:

import remarkParse from 'remark-parse'
import remarkStringify from 'remark-stringify'
import { unified } from 'unified'

export const remark = unified()
  .use(remarkParse)
  .use(remarkStringify)

becomes:

'use strict'

const [
  { default: remarkParse },
  { default: remarkStringify },
  { unified }
] = await Promise.all([
  import('remark-parse'),
  import('remark-stringify'),
  import('unified')
])

const remark = unified()
  .use(remarkParse)
  .use(remarkStringify)

return {
  remark
}

To evaluate this requires the AsyncFunction constructor, which isn’t globally available.

Instead, it could be transformed into:

return Promise.all([
  import('remark-parse'),
  import('remark-stringify'),
  import('unified')
]).then(async ([
  { default: remarkParse },
  { default: remarkStringify },
  { unified }
]) => {
  'use strict'

  const remark = unified()
    .use(remarkParse)
    .use(remarkStringify)

  return {
    remark
  }
})

This means the result can be evaluated with the globally available Function constructor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions