Build if Changed

1.5.5 · active · verified Sun Apr 19

build-if-changed (bic) is a utility designed to optimize monorepo build processes by executing `npm run build` scripts only for packages that have undergone changes since their last build. It achieves this by crawling specified directories within a package, generating SHA-1 hashes of watched files, and storing them in a `.bic_cache` file alongside each `package.json`. If the hashes indicate a change, the build script is triggered. The current stable version is 1.5.5. It focuses on reducing build times and CI/CD costs by avoiding redundant builds, differentiating itself through its simple `package.json`-based configuration and custom glob syntax for file watching.

Common errors

Warnings

Install

Quickstart

This quickstart installs `build-if-changed`, creates a dummy `package.json` with a build script, runs `bic` initially, then simulates a file change to demonstrate how `bic` triggers a build only when necessary.

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

// Ensure a package.json exists for a mock build
const pkgPath = path.join(process.cwd(), 'package.json');
if (!fs.existsSync(pkgPath)) {
  console.log('Creating a dummy package.json...');
  fs.writeFileSync(pkgPath, JSON.stringify({
    "name": "my-test-app",
    "version": "1.0.0",
    "scripts": {
      "build": "echo 'Building my-test-app...' && touch build-output.txt"
    },
    "devDependencies": {}
  }, null, 2));
}

console.log('Installing build-if-changed as a dev dependency...');
execSync('npm install build-if-changed -D', { stdio: 'inherit' });

console.log('Running build-if-changed for the first time...');
execSync('npx build-if-changed', { stdio: 'inherit' });

// Simulate a file change to trigger a build next time
console.log('\nSimulating a file change...');
fs.writeFileSync(path.join(process.cwd(), 'src/index.js'), '// Some new content', { flag: 'a' });

console.log('Running build-if-changed again after a change...');
execSync('npx build-if-changed', { stdio: 'inherit' });

console.log('\nCleanup (optional)...');
// fs.unlinkSync(pkgPath);
// fs.unlinkSync(path.join(process.cwd(), '.bic_cache'));
// fs.rmSync(path.join(process.cwd(), 'node_modules'), { recursive: true, force: true });
// fs.rmSync(path.join(process.cwd(), 'src'), { recursive: true, force: true });

console.log('Quickstart complete. Check build-output.txt.');

view raw JSON →