Lofi Bundler
Lofi Bundler is a minimalist TypeScript utility designed for projects that prioritize simplicity and a zero-dependency build process, especially when targeting browser environments or when complex module loaders are unnecessary. It operates by walking through all specified TypeScript files within a project (explicitly excluding `node_modules` and other external folders) and concatenating their content into a single TypeScript bundle. This bundled file can then be fed into the standard TypeScript compiler (`tsc`) for final compilation, eliminating the need for more elaborate bundling solutions like Webpack, Rollup, or even Babel configurations. The package's current stable version is 1.0.1, suggesting a mature, feature-complete state with an infrequent release cadence focused on stability. Its primary differentiator lies in its 'lofi' philosophy: a bare-bones approach that avoids external dependencies and configuration overhead, making it ideal for small, self-contained utilities or libraries that need to run in both Node.js and the browser without introducing complex build chains or module resolution challenges inherent to different environments.
Common errors
-
Error: Default exports are not supported.
cause Attempting to use `export default` syntax in your TypeScript files.fixChange all default exports to named exports. The bundler does not guarantee correct naming or resolution for default exports.
Warnings
- breaking Lofi Bundler explicitly does not support default exports. Using them will lead to unexpected naming or compilation errors, as the bundler is designed around named exports only.
- gotcha This bundler is intentionally designed to process only files within your immediate project scope and explicitly excludes `node_modules` or other external folders. It is not suitable for bundling third-party npm dependencies.
- gotcha Lofi Bundler outputs a single TypeScript file. This intermediate file still requires a subsequent compilation step using `tsc` to be transformed into a runnable JavaScript file, especially for specific browser targets (e.g., ES5).
Install
-
npm install lofi-bundler -
yarn add lofi-bundler -
pnpm add lofi-bundler
Imports
- traceConcat
const { traceConcat } = require('lofi-bundler');import { traceConcat } from 'lofi-bundler';
Quickstart
{
"name": "my-lofi-project",
"version": "1.0.0",
"scripts": {
"prebuild:browser": "rm -f bundle.ts",
"build:browser": "lofi-bundler && tsc bundle.ts --target es5 --module commonjs --outFile dist/browser-bundle.js",
"postbuild:browser": "rm bundle.ts"
},
"config": {
"lofi-bundler": {
"entry": "src/index.ts",
"target": "bundle.ts",
"include": "src/utils/*.ts"
}
},
"devDependencies": {
"lofi-bundler": "^1.0.0",
"typescript": "^5.0.0"
}
}
// src/index.ts
export function sayHello(name: string): string {
return `Hello from Lofi Bundler, ${name}!`;
}
// src/utils/math.ts
export function add(a: number, b: number): number {
return a + b;
}
// To run:
// 1. Save the above package.json
// 2. Create src/index.ts and src/utils/math.ts with the content above.
// 3. Run `npm install` or `yarn install`.
// 4. Run `npm run build:browser` or `yarn build:browser`.
// This will generate `dist/browser-bundle.js`.
// Example of programmatic usage (optional, does not require package.json config)
// import { traceConcat } from 'lofi-bundler';
// import * as fs from 'fs';
// async function generateBundleApi() {
// const entryFilePath = 'src/index.ts';
// const bundledSource = await traceConcat(entryFilePath);
// fs.writeFileSync('bundle-via-api.ts', bundledSource);
// console.log('Bundle generated via API to bundle-via-api.ts');
// }
// generateBundleApi();