fix(build): preserve rolldownOptions proxy (fix #22033)#22367
Closed
maruthang wants to merge 1 commit into
Closed
Conversation
`resolveBuildEnvironmentOptions` installs a getter/setter proxy via
`setupRollupOptionCompat(merged, 'build')` to keep `rollupOptions` and
`rolldownOptions` in sync, but the subsequent `{ ...merged, ... }`
spread invokes the getter and copies the current value as a plain data
property. After the spread the two fields become independent
references, so when a builder later replaces the whole
`rolldownOptions` object, `rollupOptions` keeps the stale reference and
`resolveRolldownOptions` reads `undefined` for `input`, falling back to
`index.html`.
Re-install the proxy on the resolved options object after the spread to
restore the live link.
fixes vitejs#22033
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
Fixes #22033.
When a builder (or plugin) reassigns the whole
build.rolldownOptionsobject on a resolved environment config, the resulting build ignores the newinputand falls back toindex.html, producing a chunk namedindex-*.jsinstead ofentry-*.js.Root cause.
resolveBuildEnvironmentOptionsinpackages/vite/src/node/build.tsinstalls a getter/setter proxy viasetupRollupOptionCompat(merged, 'build')so thatrollupOptionsandrolldownOptionsstay in sync. The next step buildsresolvedwith{ ...merged, ... }. The spread invokes the getter and copies the current value as a plain data property, soresolved.rollupOptionsandresolved.rolldownOptionsbecome two independent properties that happen to point at the same object. Once a consumer replaces the wholerolldownOptions,rollupOptionskeeps the stale reference;resolveRolldownOptionsthen readsoptions.rollupOptions.input(stillundefined) and falls back toindex.html.This is why earlier attempts (#22034, #22051) didn't catch it — they didn't identify the proxy-breaking spread.
Fix. Re-install the proxy on
resolvedafter the spread by callingsetupRollupOptionCompat(resolved, 'build')again. Six lines of change including a comment that points at #22033.Alternatives explored
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).Testing
Added regression test in
packages/vite/src/node/__tests__/build.spec.tsthat:rolldownOptionsto{ input: '/entry.js' },rollupOptions.input,entry-*.js(notindex-*.js, which would mean a fallback toindex.html).Verified failing before the fix and passing after.
pnpm run test-unit— full suite passes (780 passed, 4 skipped, includes the new test).pnpm run typecheck— passes.pnpm run build— passes.pnpm run lint— clean apart from a pre-existing parse error inplayground/preserve-symlinks/module-a/linked.js, which is unrelated to this change.