Vite Build Issue: Source Code Lost After Build
Describe the bug
When creating a project with yarn create vite using the default Vite bundler, source code is lost in the build output. However, when using rolldown-vite as an alternative bundler, everything works correctly.
| Default Vite | Rolldown Vite | |
|---|---|---|
yarn dev |
✅ | ✅ |
yarn build && yarn preview |
❌ | ✅ |
I've included more details in the reproduction link.
Reproduction
https://github.com/wqcstrong/vite-lost-code-after-build?tab=readme-ov-file
Steps to reproduce
No response
System Info
System:
OS: macOS 26.0
CPU: (10) arm64 Apple M4
Memory: 289.56 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.18.0 - /Users/blucas/.nvm/versions/node/v22.18.0/bin/node
Yarn: 1.22.22 - /usr/local/bin/yarn
npm: 10.9.3 - /Users/blucas/.nvm/versions/node/v22.18.0/bin/npm
Browsers:
Chrome: 142.0.7444.162
Edge: 142.0.3595.53
Safari: 26.0
Used Package Manager
yarn
Logs
No response
Validations
- [x] Follow our Code of Conduct
- [x] Read the Contributing Guidelines.
- [x] Read the docs.
- [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [x] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- [x] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [x] The provided reproduction is a minimal reproducible example of the bug.
This sounds like a bug in rollup. Would you try if you can create a reproduction with raw Rollup?
Minimal reproduction of the issue:
class A {
constructor() {
const local = (this.b = {})
Object.defineProperty(local, 'c', { value: 42 })
}
}
console.log(new A().b.c) //=> 42
Rollup output:
class A {
constructor() {
(this.b = {});
}
}
console.log(new A().b.c); //=> undefined
Rollup tree-shakes away the local variable and side-effects applied to it.
@wqcstrong As quick "fix" on your side you could help out Rollup a bit and extract the chained assignments to separate statements in createUniforms and createBuffers. E.g.:
const values = (this.uniforms = {} as Record<string, unknown>);
// =>
this.uniforms = {};
const values = this.uniforms;
@jtlehtinen Thank you for trimming it down. Would you create an issue to Rollup as well?