lbundle: Zero-Configuration Library Bundler
raw JSON →lbundle is a small, zero-configuration bundler specifically designed for NPM libraries, leveraging Rollup.js for bundling and SWC (written in Rust) for fast transpilation. Its core philosophy is to minimize setup by heavily relying on best practices defined within the `package.json` file, using fields like `source`, `main`, `module`, `types`, `unpkg`, and `exports` to determine build inputs and outputs. The current stable version is 1.6.0, with frequent minor releases focusing on bug fixes and feature enhancements, as evidenced by its recent changelog. It differentiates itself by its 'convention over configuration' approach, automatic asset optimization, and robust TypeScript support, making it an ideal choice for library authors seeking a streamlined build process without extensive build toolchain configuration.
Common errors
error Error: Cannot find module 'sass' imported from 'some-file.scss' ↓
sass as a development dependency: npm install -D sass or yarn add -D sass. error Error: Unknown file extension "./src/index.ts" for "./src/index.ts" ↓
source field in package.json points to the correct entry file (e.g., ./src/index.ts) and that TypeScript is correctly configured if using TS files. error lbundle: command not found ↓
lbundle is installed as a devDependency and run it via npx lbundle or add lbundle to your package.json scripts (e.g., "build": "lbundle"). Warnings
gotcha lbundle heavily relies on `package.json` fields (`source`, `main`, `module`, `types`, `unpkg`, `exports`). Misconfiguring these fields, or omitting them entirely, will prevent `lbundle` from generating the expected output formats or entry points. ↓
gotcha Asset imports (images, SVGs, etc.) require a TypeScript reference directive (`/// <reference types="lbundle/modules" />`) in your project's type declaration files to ensure type safety and correct resolution during development. ↓
gotcha Styling preprocessor support (Less, Sass, Stylus) depends on corresponding peer dependencies. If you import such files without installing the relevant package, `lbundle` will fail to process them. ↓
gotcha The `exports` field in `package.json` is crucial for defining how your bundled library is consumed by different environments (ESM, CJS, Node, browser). Incorrectly configuring this field can lead to module resolution issues for consumers. ↓
Install
npm install lbundle yarn add lbundle pnpm add lbundle Imports
- lbundle CLI wrong
import lbundle from 'lbundle'; // This is a CLI tool, not a directly importable function for general use.correctnpx lbundle - Asset Modules Types wrong
import { Asset } from 'lbundle/modules';correct/// <reference types="lbundle/modules" /> - build wrong
const build = require('lbundle').build;correctimport { build } from 'lbundle'; await build({ /* config */ });
Quickstart
{
"name": "my-library",
"version": "1.0.0",
"source": "./src/index.ts",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"unpkg": "./dist/index.umd.js",
"sideEffects": false,
"exports": {
".": {
"default": "./dist/index.js",
"node": "./dist/index.js",
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
},
"./index.css": "./dist/index.css",
"./package.json": "./package.json"
},
"scripts": {
"build": "lbundle",
"dev": "lbundle --watch"
},
"devDependencies": {
"lbundle": "^1.6.0"
}
}
// src/index.ts
import logo from './assets/logo.png';
import { ReactComponent as Icon } from './assets/icon.svg';
export const sayHello = (name: string) => {
console.log(`Hello, ${name}!`);
// Example usage of imported assets
document.body.innerHTML += `<img src="${logo}" alt="Logo" />`;
const iconContainer = document.createElement('div');
// In a React context, you'd render <Icon /> directly.
// For this example, we'll simulate its presence.
iconContainer.innerHTML = 'SVG Icon placeholder';
document.body.appendChild(iconContainer);
return `Hello, ${name} with logo and icon!`;
};
// global.d.ts (for TypeScript asset support)
/// <reference types="lbundle/modules" />
// Run the build
// npm install && npm run build