fix: preserve with import attributes when treeshaking#1398
Open
ATOM00blue wants to merge 1 commit into
Open
Conversation
…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.
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
|
@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. |
There was a problem hiding this comment.
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--treeshaketo emit import attributes usingwithrather thanassert. - Add a regression test that verifies the emitted code preserves
with { type: 'json' }and does not containassert.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1374
Problem
With
bundle: falseandtreeshake: true, tsup rewrites modernimport data from './x.json' with { type: 'json' }into the deprecatedassert { type: 'json' }syntax, which is a SyntaxError on Node 22+.Root cause
treeshake: trueruns the output through a Rollupbundle.generate()pass (src/plugins/tree-shaking.ts), whoseOutputOptionsdefaultimportAttributesKeytoassert. (The esbuild and.d.tspaths are unaffected.)Fix
Set
importAttributesKey: 'with'on the tree-shaking Rollup output so the standardwithsyntax is preserved.Test
Added a test that builds with
bundle: false+--treeshake, importing JSON viawith { type: 'json' }, asserting the output containswith { type: 'json' }and notassert.