0.1.0 • Published 5 years ago

build-if.macro v0.1.0

Weekly downloads
14
License
-
Repository
github
Last release
5 years ago

build-if.macro

This is a babel macro that lets you test a predicate at build-time, leaving the unused branch out of your runtime code, and pruning away any imports that were used only by the unused branch.

For example, this:

import buildIf from 'build-if.macro';
console.log(buildIf(true, () => 'hello', () => 'goodbye'));

Compiles to:

console.log('hello');

You will often want to use this in conjunction with a macro like preval to compute the predicate:

import preval from 'babel-plugin-preval/macro';
import buildIf from 'build-if.macro';

const DEBUG_MODE = preval`module.exports = Boolean(process.env.ENABLE_DEBUG)`;

function doTheWork(opt) {
  // ...
  buildIf(DEBUG_MODE, () => {
    console.log("some debugging info");
  });
  // ...
}