Skip to content

fix(build): mark tsdown ESM output as ES modules - #1790

Merged
N2D4 merged 2 commits into
devfrom
devin/1784668131-esm-type-module-marker
Jul 22, 2026
Merged

N2D4 merged 2 commits into
devfrom
devin/1784668131-esm-type-module-marker

Conversation

@N2D4

@N2D4 N2D4 commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The published @hexclave/* packages emit their ESM build into dist/esm/*.js and point the exports-map import condition there, but neither the package root nor dist/esm/ carries a "type": "module" marker. So .js files under dist/esm/ default to CommonJS.

Strict runtimes (tsx / esbuild loader, older Node) then parse the ESM output as CJS, fail to find the ESM named exports, and throw SyntaxError: The requested module '@hexclave/js' does not provide an export named 'HexclaveServerApp' (or ERR_MODULE_NOT_FOUND). Plain Node >=20.19/22 papers over it via automatic ESM syntax detection, so it only surfaces for ESM consumers ("type": "module") run under tsx — a common scaffold setup. The misleading error sends people chasing renamed-export / resolution rabbit holes.

Fix

js-library.ts now emits a dist/esm/package.json = {"type":"module"} at build time (a rolldown writeBundle hook keyed on the ESM output dir). This scopes only the ESM output as ESM; Node/tsx find that nested package.json before the root one, so dist/*.js stays CommonJS for require() consumers.

Applies to every package built through the shared tsdown config (js, next, react, tanstack-start, shared, ui, sc, shared-backend, dashboard-ui-components) — a per-package fix would just move the error one dependency deeper.

Validation

Reproduced against a freshly built dist, consumed from an ESM project via tsx: before, the SyntaxError above; after, loaded: function. Confirmed the marker lands in dist/esm/package.json only, never in the CJS dist/ root.

Link to Devin session: https://app.devin.ai/sessions/637056927f8642fbb51e512ac6caa93b
Requested by: @N2D4


Summary by cubic

Mark ESM outputs as ES modules by writing a nested package.json in dist/esm with type: "module" and ESM-only exports. This fixes broken named exports for ESM consumers (e.g. tsx) in @hexclave/* packages.

  • Bug Fixes
    • Added a writeBundle hook in configs/tsdown/js-library.ts to emit dist/esm/package.json and preserve self-referencing ESM exports (drops require targets, rewrites paths).
    • Keeps CommonJS dist/ behavior for require() and applies to all packages using the shared tsdown config.

Written for commit a97685c. Summary will update on new commits.

Review in cubic

Summary by CodeRabbit

  • Bug Fixes
    • Improved compatibility for ESM builds by ensuring the generated dist/esm package metadata is correctly marked as JavaScript modules.
    • Prevented strict runtimes from misinterpreting ESM output by rewriting exports so the correct module targets are exposed and CommonJS conditions are omitted.

@N2D4 N2D4 self-assigned this Jul 21, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR that start with 'DevinAI' or '@devin'.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@vercel

vercel Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
stack-auth-hosted-components Ready Ready Preview, Comment Jul 21, 2026 9:43pm
stack-auth-internal-tool Error Error Jul 21, 2026 9:43pm
stack-auth-mcp Ready Ready Preview, Comment Jul 21, 2026 9:43pm
stack-auth-skills Ready Ready Preview, Comment Jul 21, 2026 9:43pm
stack-backend Ready Ready Preview, Comment Jul 21, 2026 9:43pm
stack-dashboard Ready Ready Preview, Comment Jul 21, 2026 9:43pm
stack-demo Ready Ready Preview, Comment Jul 21, 2026 9:43pm
stack-docs Error Error Jul 21, 2026 9:43pm
stack-preview-backend Ready Ready Preview, Comment Jul 21, 2026 9:43pm
stack-preview-dashboard Ready Ready Preview, Comment Jul 21, 2026 9:43pm

@coderabbitai

coderabbitai Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The tsdown JavaScript library configuration recursively rewrites package exports for ESM output and adds a bundle-write plugin that creates an esm/package.json manifest declaring "type":"module".

Changes

ESM build marking

Layer / File(s) Summary
Rewrite exports and write ESM package manifest
configs/tsdown/js-library.ts
Export helpers remap ./dist/esm/... and ./dist/... targets while omitting require conditions. The writeBundle plugin applies this transformation and writes the ESM manifest when the output directory is esm.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • hexclave/hexclave#1598: Uses the tsdown pipeline and publishes exports from dist/esm, which is affected by the generated manifest.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the main change: marking tsdown ESM output as ES modules.
Description check ✅ Passed The description includes Problem, Fix, and Validation sections and is detailed enough for the change.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch devin/1784668131-esm-type-module-marker

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@devin-ai-integration
devin-ai-integration Bot marked this pull request as ready for review July 21, 2026 21:22
Copilot AI review requested due to automatic review settings July 21, 2026 21:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Re-trigger cubic

This branch had an error being deployed

1 failed and 4 active deployments
Preview – stack-preview-backend — a97685c0 Deployed Jul 21, 2026 by vercel[bot]
Preview – stack-preview-dashboard — a97685c0 Deployed Jul 21, 2026 by vercel[bot]
Preview – stack-auth-internal-tool — a97685c0 Deployed Jul 21, 2026 by vercel[bot]
Preview – stack-auth-skills — a97685c0 Deployed Jul 21, 2026 by vercel[bot]
Preview – stack-auth-mcp — a97685c0 Deployed Jul 21, 2026 by vercel[bot]
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.

2 participants