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

admin2025-04-15  2

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:

  • Previous discussion on this topic.
  • support for top level await · Issue #253 · evanw/esbuild

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:

  • Previous discussion on this topic.
  • support for top level await · Issue #253 · evanw/esbuild
Share Improve this question asked Feb 4 at 10:47 OokerOoker 3,1645 gold badges39 silver badges83 bronze badges 2
  • This would be not node12 but something else, probably by specifying support option esbuild.github.io/api/#supported . Can you clarify if there's a problem if you use a different node target as suggested github.com/evanw/esbuild/issues/253#issuecomment-1042859888 ? – Estus Flask Commented Feb 5 at 11:41
  • That's my current workaround (using node15). If there is a feature that node 12 doesn't support, I need to manually fix that. – Ooker Commented Feb 7 at 16:43
Add a comment  | 

1 Answer 1

Reset to default 0

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,
  },
})
转载请注明原文地址:http://www.anycun.com/QandA/1744725808a86761.html