Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vite Build include unused variable(s) #16319

Closed
7 tasks done
yuan116 opened this issue Mar 31, 2024 · 4 comments
Closed
7 tasks done

Vite Build include unused variable(s) #16319

yuan116 opened this issue Mar 31, 2024 · 4 comments
Labels
invalid This doesn't seem right

Comments

@yuan116
Copy link

yuan116 commented Mar 31, 2024

Describe the bug

Vite Build include unused variable(s) when the components are short circuit evaluation

src/Layout/LeftSidebar.jsx has a variable declare as array and map into Link component from react-router-dom
the src/Layout/LeftSidebar.jsx is imported in src/Layout/Header.jsx
while src/Layout/Header.jsxis imported and short circuit evaluated in src/Layout/index.jsx

Actual output:
Variables declare in src/Layout/LeftSidebar.jsx included in output
Screenshot 2024-03-31 at 12 23 59

Expected output:
Variables declare in src/Layout/LeftSidebar.jsx or any components that short circuit evaluated (false) should not included in output

Reproduction

https://stackblitz.com/edit/vitejs-vite-rjt1ta?file=src%2Fpages%2FLayout%2FLeftSidebar.jsx

Steps to reproduce

Run npm run build

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (2) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.3 - /usr/local/bin/pnpm
npmPackages:
    @vitejs/plugin-react-swc: ^3.6.0 => 3.6.0 
    vite: ^5.2.6 => 5.2.7

Used Package Manager

npm

Logs

No response

Validations

Copy link

stackblitz bot commented Mar 31, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sapphi-red
Copy link
Member

sapphi-red commented Mar 31, 2024

The statement is included in the output because it has a sideeffect. That code accesses a global variable props and ref that might not be declared. Accessing a non-declared variable causes a ReferenceError.

Closing as this is an expected behavior.

@sapphi-red sapphi-red added invalid This doesn't seem right and removed pending triage labels Mar 31, 2024
@yuan116
Copy link
Author

yuan116 commented Mar 31, 2024

Hi @sapphi-red , sorry
I have updated the code based on the issue I mentioned

Code change:

  1. Remove {...props} and ref
  2. Change ['link-1', 'link-2', 'link-3'] to [{ text: 'link-1', to: 'link-a' },{ text: 'link-2', to: 'link-b' },{ text: 'link-3', to: 'link-c' },]
Screenshot 2024-03-31 at 17 23 34

Could you check again?

@sapphi-red
Copy link
Member

That is expected as well because property access is assumed to have sideeffects.
See rollup/rollup#5421 for more details.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants