GithubHelp home page GithubHelp logo

unreachable-branch-transform's Introduction

unreachable-branch-transform

Build Status

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 = process.env.TARGET === 'client' ? require('ajax') : require('fs');

// after envify
var transport = 'server' === 'client' ? require('ajax') : require('fs');
// then after unreachable-branch-transform
var transport = require('fs');
// original
if (process.env.NODE_ENV === 'development') {
  console.log('in dev mode');
} else {
  console.log('in some other mode');
}

// after envify
if ('production' === 'development') {
  console.log('in dev mode');
} else {
  console.log('in some other mode');
}

// then after unreachable-branch-transform
{
  console.log('in some other mode');
}

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 the transform function exposed by requiring the package.

Frequently asked questions

Why are undefined equality references not removed?

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.

unreachable-branch-transform's People

Contributors

debris avatar mrak avatar zenflow avatar zertosh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

unreachable-branch-transform's Issues

Issue with recast and unreachable

I'm not exactly sure the root cause of this issue, but I'm getting:

AssertionError: no line 0 (line numbers start from 1)
    at Lines.Lp.getIndentAt 

Which also happens when I do a simple

$: node
$: var r = require('recast')
$: r.parse('')

There was a fix added to recast to address this issue here benjamn/recast@34b6606, but the fix requires 0.11.4. Can we update the package.json here to either use a peerDependency or just update the dependency directly?

(note: manually patching this library inside my node_modules with 0.11.4 makes the issue disappear).

Does not remove branches using undefined

If there is a condition such as this:

if (undefined === 'development') {
  // some sensitive code only for development
}

It does not get removed. For reference, I am using the envify transform to do conditional paths based on NODE_ENV at build-time for client-side code

returning boolean instead of expression value

I'm using three transforms: coffeeify, envify, and unreachable-branch-transform; in that order.

Prior to adding unreachable-branch-transform, I had the following expression returned (after envify had run):

version: "0.2.0" || require('../package.json').version

("0.2.0" was inserted statically in place of process.env.npm_package_version)

After adding unreachable-branch-transform, I expected:

version: "0.2.0"

But instead, received:

version: true

unreachable-branch-transform should be returning the first truthy expression value, not a raw boolean (unless, of course, the first truthy expression happens to be a literal boolean).

Make `file` argument optional, for use as regular transform stream outside of browserify

I want to do something like this ...

getReadableStream().pipe(unreachableBranchTransform()).pipe(getWritableStream())

but the current implementation requires file as it's first argument, and will throw an unexpected error if it isn't provided.

All that the file argument is used for is checking whether the stream contents should be transformed or simply passed through. If no file is specified the default should be to transform.

The transform breaks Sizzle in jQuery

The transform breaks Sizzle in jQuery. This will happen:

        "CLASS": function( className ) {
            var pattern = classCache[ className + " " ];

            return pattern ||
-               (pattern = new RegExp( "(^|" + whitespace + ")" + className + "(" + whitespace + "|$)" )) &&
                classCache( className, function( elem ) {
                    return pattern.test( typeof elem.className === "string" && elem.className || typeof elem.getAttribute !== "undefined" && elem.getAttribute("class") || "" );
                });
        },

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.