Skip to content

fix: preserve with import attributes when treeshaking#1398

Open
ATOM00blue wants to merge 1 commit into
egoist:mainfrom
ATOM00blue:fix/preserve-import-attributes
Open

fix: preserve with import attributes when treeshaking#1398
ATOM00blue wants to merge 1 commit into
egoist:mainfrom
ATOM00blue:fix/preserve-import-attributes

Conversation

@ATOM00blue
Copy link
Copy Markdown

Closes #1374

Problem

With bundle: false and treeshake: true, tsup rewrites modern import data from './x.json' with { type: 'json' } into the deprecated assert { type: 'json' } syntax, which is a SyntaxError on Node 22+.

Root cause

treeshake: true runs the output through a Rollup bundle.generate() pass (src/plugins/tree-shaking.ts), whose OutputOptions default importAttributesKey to assert. (The esbuild and .d.ts paths are unaffected.)

Fix

Set importAttributesKey: 'with' on the tree-shaking Rollup output so the standard with syntax is preserved.

Test

Added a test that builds with bundle: false + --treeshake, importing JSON via with { type: 'json' }, asserting the output contains with { type: 'json' } and not assert.

…false

The Rollup pass used for tree-shaking defaults `importAttributesKey` to
`assert`, rewriting modern `import ... with { type: 'json' }` into the
deprecated `assert { type: 'json' }` syntax, which is a SyntaxError on
Node 22+. Set `importAttributesKey: 'with'` so the emitted output keeps
the standard `with` syntax.
Copilot AI review requested due to automatic review settings May 21, 2026 02:33
@codesandbox
Copy link
Copy Markdown

codesandbox Bot commented May 21, 2026

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@vercel
Copy link
Copy Markdown

vercel Bot commented May 21, 2026

@ATOM00blue is attempting to deploy a commit to the EGOIST's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an ESM correctness issue in tsup’s tree-shaking pipeline where Rollup’s output generation was rewriting modern import attributes (with { ... }) into deprecated import assertions (assert { ... }), which fails on Node 22+ when bundle: false and treeshake: true are used.

Changes:

  • Configure the Rollup bundle.generate() step used by --treeshake to emit import attributes using with rather than assert.
  • Add a regression test that verifies the emitted code preserves with { type: 'json' } and does not contain assert.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
test/index.test.ts Adds a regression test ensuring with import attributes are preserved when using --treeshake with bundle: false.
src/plugins/tree-shaking.ts Sets Rollup importAttributesKey: 'with' during tree-shaking output generation to avoid rewriting to deprecated assert.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

bundle: false rewrites "with { type: 'json' }" to deprecated "assert { type: 'json' }"

2 participants