Skip to content

feat(cjs): warn when platform is ignored for the CJS format - #1111

Merged
sxzz merged 4 commits into
mainfrom
feat/warn-cjs-platform
Oct 28, 2026
Merged

sxzz merged 4 commits into
mainfrom
feat/warn-cjs-platform

Conversation

@sxzz

@sxzz sxzz commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

AI usage

  • No AI was used in this PR.
  • AI was used: Claude Code + Claude Opus 5.5
    • I have carefully reviewed the AI-generated content myself.

Description

The cjs format is always built for the node platform, so platform: 'browser' is silently ignored for it. That includes resolving dependencies with the node export condition. tsdown now warns about this and suggests platform: 'neutral', or inputOptions: { platform: 'browser' } to build CJS for the browser anyway. platform: 'neutral' doesn't warn, since it makes no runtime assumptions (e.g. --format cjs,esm --platform neutral).

Shims are now applied by a plugin that decides in its options hook, from the final platform after the user's inputOptions are merged:

  • ESM on node with shims: __dirname/__filename inject (or define + banner for unbundle), as before.
  • CJS on a non-node platform: import.meta.url/filename/dirname are rewritten the same way Rolldown does on node.

So forcing the platform via inputOptions no longer loses the import.meta rewrite, and the warning check also runs on the merged options.

Linked Issues

Closes #758

Additional context

Projects that build CJS with platform: 'browser' will see the new warning, which fails the build under failOnWarn until they switch to neutral or override inputOptions.platform.

Copilot AI balanced review requested due to automatic review settings October 28, 2026 10:18
@netlify

netlify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

βœ… Deploy Preview for tsdown-main ready!

Name Link
πŸ”¨ Latest commit c3fde62
πŸ” Latest deploy log https://app.netlify.com/projects/tsdown-main/deploys/6ae1d09e5a765608dde3be82
😎 Deploy Preview https://deploy-preview-1111--tsdown-main.netlify.app
πŸ“± Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
πŸ€– Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new

pkg-pr-new Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

tsdown

pnpm add https://pkg.pr.new/rolldown/tsdown@1111 -D
npm i https://pkg.pr.new/rolldown/tsdown@1111 -D
yarn add https://pkg.pr.new/rolldown/tsdown@1111.tgz -D

create-tsdown

pnpm add https://pkg.pr.new/rolldown/tsdown/create-tsdown@1111 -D
npm i https://pkg.pr.new/rolldown/tsdown/create-tsdown@1111 -D
yarn add https://pkg.pr.new/rolldown/tsdown/create-tsdown@1111.tgz -D

@tsdown/css

pnpm add https://pkg.pr.new/rolldown/tsdown/@tsdown/css@1111 -D
npm i https://pkg.pr.new/rolldown/tsdown/@tsdown/css@1111 -D
yarn add https://pkg.pr.new/rolldown/tsdown/@tsdown/css@1111.tgz -D

@tsdown/exe

pnpm add https://pkg.pr.new/rolldown/tsdown/@tsdown/exe@1111 -D
npm i https://pkg.pr.new/rolldown/tsdown/@tsdown/exe@1111 -D
yarn add https://pkg.pr.new/rolldown/tsdown/@tsdown/exe@1111.tgz -D

tsdown-migrate

pnpm add https://pkg.pr.new/rolldown/tsdown/tsdown-migrate@1111 -D
npm i https://pkg.pr.new/rolldown/tsdown/tsdown-migrate@1111 -D
yarn add https://pkg.pr.new/rolldown/tsdown/tsdown-migrate@1111.tgz -D

commit: c3fde62

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.

🟒 Approval recommended

No active review issues remain for the change clarifying CJS platform behavior and warnings.

No open issues πŸŽ‰

What changed in this PR

Makes tsdown’s Node.js platform default for CJS builds explicit, addressing the unexpected dependency resolution reported in #758 without changing platform selection.

Changes:

  • Warns when CJS ignores a non-Node platform setting, after merging user inputOptions.
  • Adds tests for warning and suppression cases.
  • Documents the platform override and its effect on import.meta rewriting.
File Description
src/​features/​rolldown.ts Runs the warning check after input options resolve, skipping declaration-only passes.
src/​features/​cjs.ts Adds the platform warning with configuration guidance.
src/​features/​cjs.test.ts Tests when the warning is emitted or suppressed.
skills/​tsdown/​references/​option-platform.md Documents the warning and CJS platform override.
docs/​zh-CN/​options/​platform.md Updates Chinese platform documentation.
docs/​options/​platform.md Updates English platform documentation.

Review effort: Balanced

@sxzz
sxzz merged commit 5800398 into main Oct 28, 2026
17 checks passed
@sxzz
sxzz deleted the feat/warn-cjs-platform branch October 28, 2026 10:39
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.

Unexpected module resolution and tree-shaking from dependency package exports

3 participants