asbundle: Minimalistic CommonJS Bundler

2.7.0 · maintenance · verified Sun Apr 19

asbundle (current stable version 2.7.0) is a focused JavaScript bundler designed to convert single ES Module or CommonJS source files into a lightweight, optimized CommonJS bundle, primarily for browser environments. It serves as a companion to `ascjs` and utilizes `babylon` for parsing, automatically transforming ES2015+ modules into CommonJS when necessary. Its core differentiators include its minimalistic design, aiming for simplicity over feature breadth, and its ability to produce small, minifier-friendly bundles that do not rely on a global `require` or runtime dependency resolution. It prioritizes compatibility with downstream tools like Babel and UglifyJS, supports both relative file paths and `node_modules` package resolution, and reproduces a modern CommonJS environment ideal for web browsers. It explicitly avoids replacing comprehensive bundlers like Webpack or Rollup, focusing solely on `import`/`export` transpilation. The package was last published 5 years ago, suggesting a maintenance or stable status with infrequent updates.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates programmatic usage of `asbundle` to transform an ES Module entry point (`main.js`) and its dependency (`module.js`) into a single CommonJS compatible string, then logs the result.

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

// Create dummy source files for the example to be runnable
const tempDir = path.join(__dirname, 'temp_asbundle_example');
fs.mkdirSync(tempDir, { recursive: true });

fs.writeFileSync(path.join(tempDir, 'main.js'), `
import func, {a, b} from './module.js';
const val = 123;
export default function test() {
  console.log('asbundle');
};
export {func, val};
    `);
fs.writeFileSync(path.join(tempDir, 'module.js'), `
export const a = 1, b = 2;
export default function () {
  console.log('module');
};
    `);

async function bundleExample() {
  try {
    const sourceFileName = path.join(tempDir, 'main.js');
    const bundleContent = await asbundle(sourceFileName);

    console.log('Generated bundle content:\n');
    console.log(bundleContent);

    // Clean up temporary files
    fs.unlinkSync(path.join(tempDir, 'main.js'));
    fs.unlinkSync(path.join(tempDir, 'module.js'));
    fs.rmdirSync(tempDir);
    console.log('\nTemporary files cleaned up.');

  } catch (error) {
    console.error('Bundling failed:', error.message);
    // Ensure cleanup even on error
    if (fs.existsSync(path.join(tempDir, 'main.js'))) fs.unlinkSync(path.join(tempDir, 'main.js'));
    if (fs.existsSync(path.join(tempDir, 'module.js'))) fs.unlinkSync(path.join(tempDir, 'module.js'));
    if (fs.existsSync(tempDir)) fs.rmdirSync(tempDir);
  }
}

bundleExample();

view raw JSON →