rollup / rollup/rollup

Treeshake constants in if statements

Open
#2,912 2 comments 1 reaction 0 assignees View on GitHub

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?

https://rollupjs.org/repl?version=1.14.5&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMmNvbnN0JTIwQlVJTEQlMjAlM0QlMjB0cnVlJTNCJTVDbiU1Q25mdW5jdGlvbiUyMGhvbGEoZmxhZ3MpJTIwJTdCJTVDbiU1Q3RpZiUyMChmbGFncyUyMCU3QyU3QyUyMEJVSUxEKSUyMCU3QiU1Q24lNUN0JTVDdGNvbnNvbGUubG9nKCdoZWxsbycpJTNCJTVDbiU1Q3QlN0QlNUNuJTdEJTVDbiU1Q25jb25zb2xlLmxvZyhob2xhKSU1Q24lMjIlMkMlMjJpc0VudHJ5JTIyJTNBdHJ1ZSU3RCU1RCUyQyUyMm9wdGlvbnMlMjIlM0ElN0IlMjJmb3JtYXQlMjIlM0ElMjJjanMlMjIlMkMlMjJuYW1lJTIyJTNBJTIybXlCdW5kbGUlMjIlMkMlMjJhbWQlMjIlM0ElN0IlMjJpZCUyMiUzQSUyMiUyMiU3RCUyQyUyMmdsb2JhbHMlMjIlM0ElN0IlN0QlN0QlMkMlMjJleGFtcGxlJTIyJTNBbnVsbCU3RA==

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.