npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cond-comp

v1.1.0

Published

Alter JavaScript code through `if` statements that can be statically evaluated at build time.

Downloads

3

Readme

Conditional compilation

CI GitHub license npm version

This library allows you to alter JavaScript code through if statements that can be statically evaluated at build time.

Example

File source.js:

let value;
// #if FOO
value = 1;
// #elseif BAR
value = 2;
// #else
value = 3;
// #endif
console.log(value);

Build script:

import { condComp } from "cond-comp";
import fs from "node:fs";

const sourceCode = await fs.promises.readFile("source.js", {
  encoding: "utf-8",
});

const compiledCode = await condComp(sourceCode, {
  FOO: true,
  BAR: true,
});

console.log(compiledCode);

Output:

let value;
value = 1;
console.log(value);

Installation

npm install cond-comp

Usage

function condComp(code: string, context: Record<string, any>): Promise<string>;
function condCompSync(code: string, context: Record<string, any>): string;

Coditional code sections can be expressed with special JavaScript comments:

  • // #if expression
  • // #elseif expression
  • // #else
  • // #endif

The keywords #if, #elseif, #else and #endif must appear at the beginning of the comment (ignoring whitespace). Here are some examples:

// This is just a regular comment because #if does not appear at the beginning.

// #if 'This is a basic conditional.'
// #endif

//    #if 'This is a conditional with insignificant leading whitespace.'
//    #endif

// #if FOO
// You can put arbitrary text after #endif.
// #endif FOO

The #if and #endif expressions can be any valid JavaScript expression. Expressions are evaluated through vm.runInContext within the context that was passed to condComp.

Conditionals can of course be nested like they can in JavaScript:

// #if FOO
console.log("foo");

// #if BAR
console.log("foo and bar");
// #endif

// #else
console.log("not foo");

// #if BAZ
console.log("not foo and baz");
// #endif

// #endif

CLI

This library comes bundled with a console application that allows you to compile files from the command line.

npx cond-comp
Usage: cond-comp [options] <files...>

Conditionally compile JS code files with #if, #elseif, #else, #endif comments.
Currently the program can only modify files in place via the required option -i.

Arguments:
  files                input files

Options:
  -g --glob            treat input files as glob patterns
  -v, --var <vars...>  context variables, e.g. DEBUG or ENV=local
  -e, --env <file>     env file to use as the context (uses dotenv and dotenv-expand)
  --dry-run            do not modify files
  -i, --in-place       modify files in place
  -h, --help           display help for command

Advanced Usage

Async / Await

You can use async and await within your expressions.

// #if await Promise.resolve(true)
console.log("Yep.");
// #endif

// #if await (async () => true)()
console.log("Works.");
// #endif

Please note that async and await are not supported when using condCompSync.

Mutable state

All expressions are executed in a single context. Should your expressions mutate state, then the changes will be visible to subsequent expressions, which this contrived example illustrates:

// Assign false to a.
// #if a = false
console.log("Nope.");
// #endif

// #if a
console.log("Still nope.");
// #endif

// Assign true to a.
// #if a = true
console.log("Yep.");
// #endif

// #if a
console.log("Sure.");
// #endif

Mutating the context within your expressions will also affect to the context object that was passed to condComp:

const code = `// #if a = 1
// #endif`;

const context = { a: 0 };
await condComp(code, context);
console.assert(context.a === 1);

Multi-line comments

You can express conditionals using multi-line comments (C-style). However, please note that #if and #elseif expressions are still single-line only. Here are some examples:

/* #if 'C-style comments also work.' */
/* #endif */

/**
 * #if 'You can place the keyword on a new line. The single leading asterisk is ignored.'
 */
/**
 * #endif
 */

/**
 * #if 'The expression is single-line only.'
 * && false && 'This line is not part of the expression.'
 */
/**
 * #endif
 */

/**
 * #if 'Only the first keyword in a comment is recognized.'
 * #if This is ignored.
 * #endif This is also ignored.
 */
/**
 * #endif
 */

/**
 * You can put a comment above the keyword.
 * #if true
 * And also below it.
 */
/**
 * #endif
 */

License

MIT
https://opensource.org/licenses/mit-license.php