build-if.macro
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

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

/build-if.macro/

    Package Sidebar

    Install

    npm i build-if.macro

    Weekly Downloads

    1

    Version

    0.1.0

    License

    none

    Unpacked Size

    18.2 kB

    Total Files

    9

    Last publish

    Collaborators

    • ef4