Bundlex JavaScript Bundler
Bundlex is a JavaScript bundler designed for high performance and scalability, offering in-memory bundling of JavaScript, TypeScript, and JSON files. Currently at stable version 3.0.4, it differentiates itself by its focus on speed and the ability to create highly customizable bundling workflows through its `createBundler` API. While a specific release cadence isn't published, the frequent updates to similar tools suggest an active development cycle. Beyond basic bundling, Bundlex includes a built-in listener for file change events, enabling efficient watch-mode development. Its architecture supports custom bundler implementations by allowing users to define their own info extractor and bundler functions, providing flexibility for niche requirements.
Common errors
-
Cannot find module 'some-module' or its corresponding type declarations.
cause The bundler failed to resolve an import path, likely due to incorrect relative paths, a missing `node_modules` dependency, or a misconfigured alias.fixVerify the import path is correct and the module exists. For `node_modules`, ensure it's installed. If using aliases, consider a custom `infoExtractor` with `createBundler` to handle alias resolution. -
SyntaxError: Cannot use import statement outside a module
cause The bundled output is in ES Module format, but it's being executed in a CommonJS context (e.g., Node.js without `"type": "module"` in `package.json` or an older Node version).fixEnsure your Node.js project's `package.json` contains `"type": "module"` or ensure your runtime environment supports ESM. Bundlex's primary output is ESM, so direct CJS output options may not be available.
Warnings
- gotcha Bundlex primarily targets ESM output for modern JavaScript environments. While it processes CJS modules as inputs, the default output format is typically ESM. Direct CommonJS 'require()' calls in the bundled output for external modules might not function as expected without a transpilation step or specific configuration for Node.js environments.
- gotcha Relative path resolution within the bundler can sometimes be tricky, especially when dealing with nested directories or alias configurations. Bundlex resolves paths relative to the importing file unless explicitly configured otherwise.
- gotcha The `jsBundler.on('change', ...)` listener monitors file system changes for files included in the bundle graph. However, it might not automatically trigger a *re-bundle* operation; it merely notifies your application. You are responsible for re-invoking the `jsBundler()` function (or your custom bundler) upon receiving a 'change' event to generate an updated output.
Install
-
npm install bundlex -
yarn add bundlex -
pnpm add bundlex
Imports
- jsBundler
const jsBundler = require('bundlex').jsBundler;import { jsBundler } from 'bundlex'; - createBundler
import { createBundler } from 'bundlex'; - InfoExtractor, Bundler
import type { InfoExtractor, Bundler } from 'bundlex';
Quickstart
import { jsBundler } from 'bundlex';
import path from 'path';
import fs from 'fs/promises';
// Define temporary files for the demo
const entryFile = path.resolve('./temp-entry.ts');
const depFile = path.resolve('./temp-dependency.ts');
async function runBundlexQuickstart() {
// Create dummy files for bundling
await fs.writeFile(depFile, `export const message = "Hello from dependency!";`);
await fs.writeFile(entryFile, `
import { message } from './temp-dependency';
console.log(message);
console.log("This is the main entry point.");
`);
console.log('Starting Bundlex quickstart...');
try {
// Set up a listener for file changes
jsBundler.on('change', (changedPath: string) => {
console.log(`[Bundlex Watch] Detected change in: ${changedPath}.`);
console.log('Re-bundling logic would typically go here...');
});
// Bundle the entry file
const bundledContent = await jsBundler(entryFile);
console.log('\n--- Bundled Output (truncated) ---');
console.log(bundledContent.slice(0, 500) + '\n...'); // Display first 500 characters
// Simulate a file change to trigger the listener
await new Promise(resolve => setTimeout(resolve, 50)); // Small delay
await fs.appendFile(depFile, `\n// Added content to trigger change on ${Date.now()}`);
console.log('\nSimulated file change. Check for listener output above.');
} catch (error) {
console.error('Bundlex quickstart failed:', error);
} finally {
// Clean up temporary files
await fs.unlink(entryFile).catch(() => {});
await fs.unlink(depFile).catch(() => {});
console.log('\nCleaned up temporary files.');
}
}
runBundlexQuickstart();