yarb JavaScript Bundler

0.8.0 · maintenance · verified Wed Apr 22

yarb (Yet Another Require Bundler) is a JavaScript module bundler designed for the browser environment, serving a similar purpose to Browserify. It focuses on bundling CommonJS-style modules and is currently at version 0.8.0. While its release cadence isn't explicitly stated, the version number suggests a more experimental or niche tool compared to its established alternatives. A key differentiator of yarb is its enhanced capability for defining dependencies between bundles, automatically identifying and excluding common files when using the `external` function, which simplifies multi-bundle setups. However, yarb is less flexible than Browserify, offering limited support for Node.js core modules (only `events`, `fs`, `module`, `net`, `path`, `stream`, `util`) and fewer configuration options. Internally, it utilizes vinyl objects for file representation, allowing for flexible input of buffers or streams, provided they have unique and absolute `path` properties.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of yarb to bundle a main entry file with a utility, apply a custom Browserify-compatible transform, and output the result to a file.

const yarb = require('yarb');
const path = require('path');
const fs = require('fs');

// Define a simple custom Browserify-compatible transform
function myTransform(file) {
  let data = '';
  const stream = require('stream');
  const tr = new stream.Transform();
  tr._transform = function (chunk, encoding, callback) {
    data += chunk.toString();
    callback();
  };
  tr._flush = function (callback) {
    this.push(Buffer.from(data.replace(/foo/g, 'bar')));
    callback();
  };
  return tr;
}

const mainDirPath = path.join(__dirname, 'yarb-src');
const mainFilePath = path.join(mainDirPath, 'main.js');
const utilFilePath = path.join(mainDirPath, 'utils', 'helper.js');
const outputFilePath = path.join(__dirname, 'yarb-bundle.js');

// Create dummy files for demonstration
fs.mkdirSync(path.dirname(utilFilePath), { recursive: true });
fs.writeFileSync(mainFilePath, "console.log(require('./utils/helper').greet('World') + ' foo');");
fs.writeFileSync(utilFilePath, "exports.greet = (name) => `Hello, ${name}!`;");

const bundle = yarb([mainFilePath], { debug: true }) // Enable source maps
  .add(utilFilePath) // Add utility file explicitly if not directly required
  .transform(myTransform); // Apply the custom transform

bundle.bundle((err, buf) => {
  if (err) {
    console.error('Bundling failed:', err);
    return;
  }
  console.log('Bundle created successfully. Writing to', outputFilePath);
  fs.writeFileSync(outputFilePath, buf.toString());
  console.log('--- Bundled Output (truncated) ---\n' + buf.toString().substring(0, 500));

  // Clean up dummy files
  fs.unlinkSync(mainFilePath);
  fs.unlinkSync(utilFilePath);
  fs.rmdirSync(path.dirname(utilFilePath));
  fs.rmdirSync(mainDirPath);
  console.log('\nDummy files cleaned up.');
});

view raw JSON →