fix(pack): guard shims banner in unbundle mode to prevent ESM imports leaking into CJS output#2098
Open
AmariahAK wants to merge 1 commit into
Open
fix(pack): guard shims banner in unbundle mode to prevent ESM imports leaking into CJS output#2098AmariahAK wants to merge 1 commit into
AmariahAK wants to merge 1 commit into
Conversation
When pack.shims and pack.unbundle are both enabled, the ESM shims banner (import 'node:path', import.meta.url) leaks into .cjs output, making it invalid CommonJS. This patches the bundled tsdown build during core package build to add a format guard (format === 'es' && platform === 'node') around the shimsPlugin in unbundle mode, matching the existing guard in getShimsInject. Also bumps tsdown catalog from ^0.22.3 to ^0.22.4 (with @tsdown/css and @tsdown/exe) and updates bundledVersions.tsdown to 0.22.4. Upstream fix: rolldown/tsdown#991 (merged but not yet in a published release). Re-evaluate after tsdown >=0.22.5. Fixes voidzero-dev#2090 Co-authored-by: atlarix-agent <agent@atlarix.dev>
✅ Deploy Preview for viteplus-preview canceled.
|
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
Member
|
We should wait for tsdown fix it then upgrade to the fix version. |
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.
Description
What was the issue
When
pack.shims: trueandpack.unbundle: trueare combined, the generated.cjsfile contains raw ESM statements at the top:This is not valid CommonJS —
require()-ing the built.cjsthrowsSyntaxError: Cannot use import statement outside a moduleat module load. The.mjsoutput is fine; only the CJS artifact is broken.Where was the issue
In tsdown's
resolveInputOptions(), the unbundle code path pushes theshimsPlugin(which injects an ESM banner) unconditionally — without checking the output format. The bundle-mode path (getShimsInject) already has aformat === 'es' && platform === 'node'guard, but the unbundle path never did.The bug is upstream: rolldown/tsdown#991. The fix was merged but not included in the published
tsdown@0.22.4release.How I fixed it
Added a build-time patch in
bundleTsdown()insidepackages/core/build.ts. It runs before the tsdown re-bundle and injects the same format guard thatgetShimsInjectalready uses:if (shims && !cjsDts) if (unbundle) { + if (format === 'es' && platform === 'node') { define = { ...define, ...shimsDefine }; plugins.push(shimsPlugin); + } } else inject = getShimsInject(format, platform);Also bumped the tsdown catalog from
^0.22.3to^0.22.4(with@tsdown/cssand@tsdown/exe), and updatedbundledVersions.tsdownto0.22.4.Why this approach
brandTsdown(), which already applies string replacements to the bundled tsdown output.tsdown >=0.22.5.How to test locally
Initialize submodules:
Rebuild the core package (the patch runs here):
Expected output:
Patched shims+unbundle CJS leak in .../build-*.mjsCreate a minimal test project with this config:
Build and verify:
Run the full test suite:
pnpm testCloses #2090