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

buildif

v1.0.2

Published

Simple build preprocessor with real JS for logic

Downloads

6

Readme

BuildIf - Simple build preprocessor with real JS for logic

Builds different version of source depending on values in JSON/JS config. Could be used as a standalone CLI utility or as a library.

Useful for making separate builds for on-premise customers with different features or customizations. Allows to keep single codebase without per-customer branches with the tradeoff of build comments and additional build step.

Source files could be any type, but it's primarily build for JS files.

Install:

npm install buildif

Syntax

In code:

// BUILD: if (features.billing) {
	console.log("Billing Enabled");
// BUILD: }

// BUILD: if(version.startsWith("1")) {
	console.log("Version 1.x");
// BUILD: } else if (version.startsWith("2")) {
	console.log("Version 2.x");
// BUILD: }

Config file:

{
	"features": {
		"billing": false
	},
	"version": "2.1.1"
}

Any line prefixed with // BUILD: will be executed at the build-time. Any valid JS statements could be used after prefix: if/else, variable declarations, function calls, require. Ifs could be nested.

Use as CLI

Use JSON config and output to stdout:

buildif --config ./examples/config.json --input ./examples/example.js > output.js

Use JS config and output to file:

buildif --config ./examples/config.js --input ./examples/example.js --output output.js

Use pipes for both input and output:

cat ./examples/example.js | bin/buildif --config ./examples/config.json > output.js

Overwrite values from config with command line options:

buildif --config ./examples/config.json --features.billing=true --input ./examples/example.js

Use only command line options without config file:

buildif --features.billing=true --version="2.1.0" --input ./examples/example.js

Command line overwrites could have multi-level names separated by dot, like "features.billing". For array indices also use dot, like "customers.1.name".

It outputs sane error at the build time. It prints the line of code that triggered error, file name and line number. Execution error in BUILD comments:

// BUILD: nonExistingVar; // This will trigger error
ReferenceError: nonExistingVar is not defined
	at <instrumented code> (./examples/example-error.js:7)

CLI options

--input - Input file path, if not specified read from stdin.

--output - Output file path, if not specified print to stdout.

--config - Config file path, if not specified config = {}.

All other options are treated as config overwrites. When using CLI buildif will try to coerse types for options:

buildif --version=2.1 ...

will result in config object { version: 2.1 }, but

buildif --version=2.1.0 ...

will result in config object { version: "2.1.0" }.

Use as library

buildif(inputText, configObject)

Example:

var buildif = require("buildif");
var text = require("fs").readFileSync("./examples/example.js");
var config = {
	features: {
		billing: false
	},
	version: "2.1.1"
};

console.log(buildif(text, config));