digital-loukoum / esrun

Simple wrapper around esbuild to execute a Typescript file
MIT License
173 stars 11 forks source link

top level await #15

Closed Tobbe closed 2 years ago

Tobbe commented 2 years ago

I can't get top-level await to work with esrun.

This is my tsconfig file

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Node 16 + ESM + Strictest",
  "compilerOptions": {
    "lib": ["es2021"],
    "module": "esnext",
    "target": "es2021",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "strict": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "allowUnusedLabels": false,
    "allowUnreachableCode": false,
    "exactOptionalPropertyTypes": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitOverride": true,
    "noImplicitReturns": true,
    "noPropertyAccessFromIndexSignature": true,
    "noUncheckedIndexedAccess": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "importsNotUsedAsValues": "error",
    "checkJs": true
  }
}

It works fine if I do just npx tsc. But If I do npx @digitak/esrun myfile.ts I get myfile.ts:23:19: error: Top-level await is not available in the configured target environment ("es2021").

How can I use top-level await with esrun?

Gin-Quin commented 2 years ago

You need to set "target": "esnext"

Tobbe commented 2 years ago

Awesome! That works. Thank you πŸ™‚

Why doesn't tsc also need it to be "esnext"?

Gin-Quin commented 2 years ago

I don't know πŸ˜„ It's actually EsBuild that enforces the use of esnext over es2021. Maybe it should work with es2021 like tsc does, in that case it's a EsBuild bug.

Tobbe commented 2 years ago

Alright πŸ™‚ Thanks for getting back to me!