Skip to content

web: add default export to CJS bundle#5305

Merged
amaanq merged 1 commit intotree-sitter:masterfrom
chadxz:fix/web-cjs-default-export
Feb 10, 2026
Merged

web: add default export to CJS bundle#5305
amaanq merged 1 commit intotree-sitter:masterfrom
chadxz:fix/web-cjs-default-export

Conversation

@chadxz
Copy link
Contributor

@chadxz chadxz commented Feb 6, 2026

Why?

The CJS bundle sets __esModule: true and exports named members
(Parser, Language, etc.) but has no .default property. When
TypeScript consumers use module: "commonjs" + esModuleInterop:
true, the __importDefault helper sees __esModule and returns
module.default — which is undefined. This causes
import Parser from 'web-tree-sitter' to silently resolve to
undefined at runtime despite typechecking cleanly.

Reproduction: https://github.com/chadxz/web-tree-sitter-default-export-type-issue

How?

  • Traced the issue by inspecting the CJS bundle output and
    confirming __esModule is set but no .default exists
  • Added module.exports.default = module.exports; via
    esbuild's footer option, conditionally for CJS builds only
  • Verified by patching the published 0.26.3 bundle in a repro
    project and confirming the default import resolves correctly
  • Built the full ESM + CJS bundles from source to confirm the
    footer appears in the CJS output and the .d.cts types align

Why?
====

The CJS bundle sets __esModule: true and exports named members
(Parser, Language, etc.) but has no .default property. When
TypeScript consumers use module: "commonjs" + esModuleInterop:
true, the __importDefault helper sees __esModule and returns
module.default — which is undefined. This causes
`import Parser from 'web-tree-sitter'` to silently resolve to
undefined at runtime despite typechecking cleanly.

Reproduction: https://github.com/chadxz/web-tree-sitter-default-export-type-issue

How?
====

- Traced the issue by inspecting the CJS bundle output and
  confirming __esModule is set but no .default exists
- Added `module.exports.default = module.exports;` via
  esbuild's footer option, conditionally for CJS builds only
- Verified by patching the published 0.26.3 bundle in a repro
  project and confirming the default import resolves correctly
- Built the full ESM + CJS bundles from source to confirm the
  footer appears in the CJS output and the .d.cts types align
@chadxz
Copy link
Contributor Author

chadxz commented Feb 9, 2026

@WillLillis @clason is this something you'd be interested in merging? Any other validation you'd like to see?

@amaanq amaanq changed the title fix(web): add default export to CJS bundle web: add default export to CJS bundle Feb 10, 2026
@amaanq amaanq merged commit 081c90b into tree-sitter:master Feb 10, 2026
19 checks passed
@tree-sitter-ci-bot
Copy link

Successfully created backport PR for release-0.26:

@chadxz
Copy link
Contributor Author

chadxz commented Feb 10, 2026

Thanks all! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants