Bit-Bundler Utilities
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
-
Error: setId can only be set once with a different value. Original value was [original_value], new value is [new_value]
cause Attempting to call `uniqueId.setId` for the same `moduleId` with a `value` different from what was previously set.fixReview calls to `uniqueId.setId` to ensure consistency. If a module's unique ID needs to change, it indicates a design flaw or a need to reset the `uniqueId` state (which is not directly exposed by the API). -
Cannot find module 'bit-bundler-utils/resolvePath'
cause The package `bit-bundler-utils` is not installed, or the specified subpath (`/resolvePath`, `/readFile`, `/uniqueId`) is incorrect.fixEnsure `bit-bundler-utils` is correctly installed via `npm install bit-bundler-utils` and that the `require()` path matches one of the documented subpath exports. -
TypeError: [exported_function] is not a function
cause Incorrect module import syntax. This library is CommonJS-based, and direct `import` statements might not correctly resolve its exports or may treat the default export differently than expected.fixChange `import` statements to CommonJS `require()` syntax: `const resolvePath = require('bit-bundler-utils/resolvePath');`
Warnings
- gotcha The `uniqueId.setId` method will throw an exception if called more than once for the same `moduleId` but with a *different* `value`. It is designed to ensure a module ID maps to a single, consistent unique value after its initial setting.
- gotcha The `uniqueId.getId` method caches its results. Subsequent calls with the same `moduleId` will return the exact same generated ID without re-computation.
- gotcha This library primarily uses CommonJS (`require()`) module syntax. While modern Node.js environments support ESM (`import`), direct ESM imports for `bit-bundler-utils`'s subpath exports might not work as expected or without additional bundler configuration.
Install
-
npm install bit-bundler-utils -
yarn add bit-bundler-utils -
pnpm add bit-bundler-utils
Imports
- uniqueId
import uniqueId from 'bit-bundler-utils/uniqueId';
const uniqueId = require('bit-bundler-utils/uniqueId'); - resolvePath
import resolvePath from 'bit-bundler-utils/resolvePath';
const resolvePath = require('bit-bundler-utils/resolvePath'); - readFile
import readFile from 'bit-bundler-utils/readFile';
const readFile = require('bit-bundler-utils/readFile');
Quickstart
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();