fix(build): clean stale vinext build output before rebuilds#1586
Open
NathanDrake2406 wants to merge 1 commit into
Open
fix(build): clean stale vinext build output before rebuilds#1586NathanDrake2406 wants to merge 1 commit into
NathanDrake2406 wants to merge 1 commit into
Conversation
Repeated vinext builds currently leave stale hashed server chunks in dist/server. Workers deployments with no_bundle upload every matched JS module, so stale chunks count against Worker size limits. The CLI assumed Vite's per-environment emptyOutDir would remove old output, but the multi-environment build and hybrid Pages Router pass preserve dist/server artifacts. Clean the vinext dist output once immediately before the Vite build starts, while preserving build.emptyOutDir: false as the escape hatch. Add focused tests for default cleaning, explicit preservation, and outside-root safety.
commit: |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR ensures vinext build does not retain stale hashed server chunks across rebuilds by introducing a one-time cleanup of the build output directory (dist/) before the production Vite build begins, while preserving build.emptyOutDir: false as an explicit opt-out.
Changes:
- Added a
cleanBuildOutputhelper that safely removes the build output directory (defaulting todist/) only when appropriate. - Wired the cleanup into the
vinext buildCLI flow, honoring a user’s explicitbuild.emptyOutDir: falsefrom their Vite config as an escape hatch. - Added unit tests covering default cleanup behavior, opt-out preservation, and outside-root safety; updated prerender comment to reflect the new cleanup ownership.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/clean-build-output.test.ts | Adds unit tests validating default cleanup, opt-out via emptyOutDir: false, and outside-root safety behavior. |
| packages/vinext/src/cli.ts | Runs the cleanup once before the multi-environment build, and reads user build.emptyOutDir from Vite config to support opt-out. |
| packages/vinext/src/build/run-prerender.ts | Updates comment to reflect that cleanup is now owned by vinext’s build flow rather than Vite’s emptyOutDir. |
| packages/vinext/src/build/clean-output.ts | Introduces the cleanup implementation with a safety check to avoid deleting outside-root output by default. |
💡 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.
Overview
vinext buildfrom retaining stale hashed server chunks across rebuildsdist/once before the production Vite build starts, withbuild.emptyOutDir: falsepreserved as the opt-outpackages/vinext/src/cli.ts,packages/vinext/src/build/clean-output.ts,tests/clean-build-output.test.tsno_bundlerulesFixes #1584.
Why
Production build output should represent the current build, not a historical superset of every hashed chunk ever emitted. This matters more for Cloudflare Workers because vinext's generated Wrangler config uses
no_bundle: trueplus ES module globs, so stale JS/MJS files still count toward upload size even when the current manifest no longer references them.vinext buildremovesdist/before starting the Vite builderbuild.emptyOutDir: falseshould remain an escape hatchrules.globsinno_bundlemodeWhat changed
vinext builddist/server/**/{*.js,*.mjs}files could remain foreverdist/is cleaned once immediately before the build writes current artifactsemptyOutDir: falseto preserve RSC artifactsbuild.emptyOutDir: falseMaintainer review path
packages/vinext/src/build/clean-output.tsfor the cleanup decision and filesystem mutation boundary.packages/vinext/src/cli.tsfor where the clean runs relative to standalone preflight, React upgrade, andcreateBuilder().buildApp().tests/clean-build-output.test.tsfor default cleanup, explicit preservation, and outside-root safety.packages/vinext/src/build/run-prerender.tsfor the updated comment describing the new cleanup owner.Validation
vp test run --project unit tests/clean-build-output.test.tsvp checkpnpm exec knip --no-progressvp run vinext#builddist/server/ssr/_next/static/stale-dead-module.jscounter_chunks_before=1andcounter_chunks_after=1build.emptyOutDir: falsepreserveddist/server/ssr/_next/static/preserved-by-empty-out-dir-false.jsRisk / compatibility
build.emptyOutDir: falseopt-out.dist/contents by default, matching the expected clean build model.no_bundlemodule outputs.dist/need to opt out withbuild.emptyOutDir: false.Non-goals
emptyOutDir: false, since that still preserves RSC artifacts from the same build.no_bundlemodule rules.References
cleanDistDircleanup before build work proceedsbuild.emptyOutDirdocs