Bit-Bundler Utilities

5.1.2 · active · verified Tue Apr 21

Bit-Bundler Utilities (`bit-bundler-utils`) is a Node.js helper library providing essential functionalities for projects built with `bit-bundler` and `bit-imports`. Currently at stable version 5.1.2, this package offers utilities for generating and managing unique module IDs, robustly resolving module and file paths, and asynchronously reading file contents from disk. Its path resolution capabilities are powered by `browser-resolve`, making it suitable for both Node.js and browser-compatible path handling. The library emphasizes stability and integration within the `bit-bundler` ecosystem, maintaining an `active` release cadence with regular patch updates to ensure reliability and address minor issues.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates resolving a module path, reading its content, and using the unique ID generation and setting functionalities.

const resolvePath = require('bit-bundler-utils/resolvePath');
const readFile = require('bit-bundler-utils/readFile');
const uniqueId = require('bit-bundler-utils/uniqueId');

async function exampleUsage() {
  try {
    // Resolve a module path
    const result = await resolvePath({
      name: 'some-local-module',
      baseUrl: process.cwd()
    });
    console.log(`Resolved path for 'some-local-module': ${result.path}`);

    // Read a file's content
    const fileContent = await readFile({
      path: result.path // Using the resolved path
    });
    console.log(`Content snippet from ${result.path}: ${fileContent.source.substring(0, 100)}...`);

    // Generate and set unique IDs
    const id1 = uniqueId.getId('path/to/moduleA.js');
    const id2 = uniqueId.getId('path/to/moduleB.js');
    console.log(`ID for moduleA: ${id1}`);
    console.log(`ID for moduleB: ${id2}`);

    uniqueId.setId('path/to/special-module.js', 123);
    console.log(`Explicitly set ID for 'path/to/special-module.js'.`);

  } catch (error) {
    console.error('An error occurred:', error.message);
  }
}

exampleUsage();

view raw JSON →