unreachable-branch-transform
Removes unreachable code branches in if
statements, ternaries ?
, and logical operations ||
&&
, where the test is determinable (like comparing two constants). This is similar to what UglifyJS's "dead_code" compressor option does, but without the extra code transformations.
When combined with something like envify and browserify, you can perform conditional require
calls without including more code than you need.
Install
npm install unreachable-branch-transform
Example outputs
// original var transport = processenvTARGET === 'client' ? : ; // after envifyvar transport = 'server' === 'client' ? : ;// then after unreachable-branch-transformvar transport = ;
// originalif processenvNODE_ENV === 'development' console; else console; // after envifyif 'production' === 'development' console; else console; // then after unreachable-branch-transform console;
Usage
unreachable-branch-transform
can be used a browserify transform. Just include it like any other transform.unreachable-branch-transform
can also be used on raw code by calling thetransform
function exposed by requiring the package.
Frequently asked questions
undefined
equality references not removed?
Why are If you have a branch with the format
if undefined === 'production' /* ... */
it will not be removed. Unfortunately, undefined
is not a constant in older browser runtimes and can be reassigned. In this case, it could be possible that undefined
does indeed equal 'production
'.
Credit
esmangle-evaluator
is from the esmangle project.