Search code examples
javascriptnode.jsesbuildtop-level-awaitfibery

Is it possible to tell esbuild to ignore top-level await when bundling to node12 target?


await has been supported from Node 7.5, but top-level await has been only supported from Node 14.8.

I'm writing script for a service that will wrap user scripts inside a function and run it on Node 12. It means that I can use top-level await although technically it's not possible.

When I bundle my scripts with esbuild, I'd like to set the target to node12 and can still work with top-level await. Is it possible?

Related links:


Solution

  • The supported option of esbuild says:

    This setting lets you customize esbuild's set of unsupported syntax features at the individual syntax feature level. [...] Usually this is configured for you when you use the target setting, which you should typically be using instead of this setting. If the target is specified in addition to this setting, this setting will override whatever is specified by the target.

    Try this code:

    await esbuild.build({
      supported: {
        "top-level-await": true,
      },
    })