Treeshake constants in if statements
Open
Nobody has claimed this yet.
t³ ✨ enhancement
- Dominant language
- JavaScript
- Stars
- 26.3k
- Forks
- 1.8k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 20
Description
- Rollup Version: 1.14.5
- Operating System (or Browser): osx and browser
- Node Version: 12.0.0
How Do We Reproduce?
Notice that:
if (CONSTANT || variable) { }
is property treeshaken, but:
if (variable || CONSTANT) { }
is not.
I am aware of the lazy evaluation of the binary operators, but if the "variable" is a pure expression and the CONSTANT is actually constant, this transform should be safe.
Expected Behavior
It should generate:
function hola(flags) {
{
console.log('hello');
}
}
console.log(hola);
Actual Behavior
'use strict';
const BUILD = true;
function hola(flags) {
if (flags || BUILD) {
console.log('hello');
}
}
console.log(hola);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Rollup REPL reproduction and compare the two logical-expression cases shown in the issue. Trace the tree-shaking behavior for if (variable || CONSTANT) and determine whether the output can match the expected example; done means the provided reproduction generates the expected code without changing the unsafe lazy-evaluation behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100