JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 14392
  • Score
    100M100P100Q141849F
  • License MIT

Browserify transform (and jstransform visitor) to comment out unreachable code branches

Package Exports

  • unreachable-branch-transform

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (unreachable-branch-transform) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

unreachable-branch-transform

Comments out 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. Unlike UglifyJS, instead of removing the "dead code", it is wrapped in a comment /* ... */. This enables further transformations and easy debugging (since you can still see the code that was unreachable).

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 and unreachable-branch-transform
var transport = 'server' === 'client' ? null /*require('ajax')*/ : require('fs');
// original
if (process.env.NODE_ENV === 'development') {
  console.log('in dev mode');
} else {
  console.log('in some other mode');
}

// after envify and unreachable-branch-transform
if ('production' === 'development') {/*
  console.log('in dev mode');
*/} else {
  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.
  • For more control, you can also use the jstransform visitors directly via the visitorList property on the module.

Credit

lib/evaluator.js is from the esmangle project.