Skip to content

ef4/build-if.macro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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");
  });
  // ...
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published