Lionel CommonJS Bundler
Lionel CommonJS Bundler is a lightweight JavaScript module bundler designed to transform CommonJS modules into a single, browser-compatible JavaScript file. It is currently at version 1.1.1, with its last update approximately three years ago, suggesting a maintenance rather than actively developed phase. The bundler prioritizes simplicity and ease of use, offering minimal configuration options, which differentiates it from more complex tools like Webpack or Rollup. Its primary runtime dependency is `uglify-js` for minification. The bundler is notably used by the Lionel App by default. It supports bundling both directly from code content and from specified file paths.
Common errors
-
TypeError: commonJSBundler is not a function
cause Attempting to call `commonJSBundler()` directly as a function, instead of accessing its methods like `makeCode` or `makeFile`.fixEnsure you are calling methods on the `commonJSBundler` object, e.g., `commonJSBundler.makeCode(content, dir)`. -
Error: Cannot find module 'lionel-commonjs-bundler'
cause Incorrect import path or failure to specify the `index` entry point for the CommonJS module. Or, trying to use ESM `import` with a CJS-only package.fixUse the exact CommonJS `require` path as specified: `require('lionel-commonjs-bundler/index')`. If using `import`, it will not work as this package is CJS-only. -
Error: EACCES: permission denied, open 'output.js'
cause The Node.js process does not have sufficient write permissions to create or modify the output file at the specified path.fixCheck the permissions of the directory where the output file is being written. Run your script with appropriate permissions or choose an output path in a user-writable directory.
Warnings
- gotcha The package has not been updated in approximately three years. This may mean it lacks support for newer JavaScript language features (ESM, top-level await, etc.) or security patches for its dependencies, which could be critical for production use.
- gotcha This bundler is specifically designed for CommonJS modules. It does not support ES Modules (ESM) syntax (`import`/`export`) as input. Attempting to bundle ESM will likely result in parsing errors or incorrect output.
- gotcha The bundler emphasizes simplicity with 'not much configuration options'. This lack of advanced configuration (e.g., extensive plugin ecosystem, granular optimization, tree-shaking) might limit its applicability for complex bundling scenarios or custom requirements found in larger applications.
- deprecated While CommonJS itself is not deprecated in Node.js, the shift towards ES Modules is significant, with tools and libraries increasingly adopting ESM. Using a bundler exclusively for CJS might lead to interoperability issues with newer dependencies.
Install
-
npm install lionel-commonjs-bundler -
yarn add lionel-commonjs-bundler -
pnpm add lionel-commonjs-bundler
Imports
- commonJSBundler
import { commonJSBundler } from 'lionel-commonjs-bundler'; // ESM import will not work, also incorrect pathconst { commonJSBundler } = require('lionel-commonjs-bundler/index'); - makeCode
const bundle = require('lionel-commonjs-bundler').makeCode(content, dir); // May lead to undefined if `commonJSBundler` isn't the direct default export.const { commonJSBundler } = require('lionel-commonjs-bundler/index'); const bundle = commonJSBundler.makeCode(content, dir); - makeFile
import { makeFile } from 'lionel-commonjs-bundler/index'; // ESM import will not work.const { commonJSBundler } = require('lionel-commonjs-bundler/index'); const bundle = commonJSBundler.makeFile(filePath, dir);
Quickstart
const { commonJSBundler } = require('lionel-commonjs-bundler/index');
const path = require('path');
const contentOfFile = `
const dep = require('./dependency');
module.exports = 'Hello ' + dep;
`;
const dependencyContent = `
module.exports = 'World';
`;
// Create dummy files for demonstration
const fs = require('fs');
const tempDir = path.join(__dirname, 'temp_bundler_test');
if (!fs.existsSync(tempDir)) fs.mkdirSync(tempDir);
fs.writeFileSync(path.join(tempDir, 'main.js'), contentOfFile);
fs.writeFileSync(path.join(tempDir, 'dependency.js'), dependencyContent);
try {
const bundle = commonJSBundler.makeFile(path.join(tempDir, 'main.js'), tempDir);
console.log('Successfully bundled code:\n', bundle);
// Expected output (minified): 'var dep=require("./dependency");module.exports="Hello "+dep;' (after resolution)
} catch (error) {
console.error('Bundling failed:', error);
} finally {
// Clean up dummy files
fs.unlinkSync(path.join(tempDir, 'main.js'));
fs.unlinkSync(path.join(tempDir, 'dependency.js'));
fs.rmdirSync(tempDir);
}