Skip to content

build: emit the ESM bundles for ES2018 - #57

Merged
stefanoverna merged 1 commit into
mainfrom
chore/esm-target-es2018
Aug 31, 2026
Merged

build: emit the ESM bundles for ES2018#57
stefanoverna merged 1 commit into
mainfrom
chore/esm-target-es2018

Conversation

@stefanoverna

Copy link
Copy Markdown
Member

npm run build printed a wall of esbuild warnings while bundling cma-client-browser:

▲ [WARNING] Top-level "this" will be replaced with undefined since this file is an ECMAScript module

    ../rest-client-utils/dist/esm/pollJobResult.js:1:17:
      1 │ var __awaiter = (this && this.__awaiter) || function (thisArg, _arg...

They were harmless. With target: es2015 TypeScript has to downlevel async/await, object rest and for await…of into its own __awaiter / __rest / __asyncValues helpers, and it emits each one guarded by (this && this.__awaiter) || function (…) {…} so the CommonJS emit can reuse a copy hoisted onto exports. In an ES module top-level this is undefined by spec, so esbuild substituting undefined is exactly right — the guard short-circuits and the inline helper is used.

Rather than silence the warning, this removes its cause: ES2018 has all three constructs natively (async/await landed in ES2017, object rest and for await…of in ES2018), so TypeScript emits no helpers at all and the shipped code is the code we wrote. pollJobResult.js is now a plain export async function, and grep "this && this.__" finds nothing left under any dist/esm.

Eight packages/*/tsconfig.esm.json move to "target": "es2018", each gaining a matching "lib" because the root tsconfig pins es2017 and for await…of needs Symbol.asyncIterator declared. cma-client-analysis is untouched — it runs tsc alone and has no dist/esm.

The CommonJS output and dist/types are byte-identical to before; CJS keeps its ES2015 target, where top-level this is exports and nothing warns. The one consequence for users is that dist/esm now expects an ES2018 runtime — Node 10+, browsers with Symbol.asyncIterator. The bundle in cma-client-browser/esbuild.ts already targeted ES2018, so this makes the rest of the repo consistent with it.

Changeset is minor for the eight packages that build an ESM output.

Test plan

  • npm run build — 9/9 tasks green, no esbuild warnings.

https://claude.ai/code/session_01Dw3PG2vUep69n8te7emzyQ

At ES2015 TypeScript downlevels `async`/`await`, object rest and `for await…of`
into `__awaiter` / `__rest` / `__asyncValues`, and emits each helper guarded by a
top-level `this` — which is `undefined` in an ES module, so esbuild warned on
every file carrying one while bundling cma-client-browser. The guard is
harmless (it falls through to the inline helper), but the noise is avoidable:
ES2018 has all three natively and the helpers disappear.

The CommonJS output and `dist/types` are untouched. `dist/esm` now expects an
ES2018 runtime; the browser bundle already targeted it.

Claude-Session: https://claude.ai/code/session_01Dw3PG2vUep69n8te7emzyQ
@stefanoverna
stefanoverna merged commit a7ade77 into main Aug 31, 2026
2 checks passed
@stefanoverna
stefanoverna deleted the chore/esm-target-es2018 branch August 31, 2026 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant