Tripack Minimal JavaScript Bundler
Tripack is a minimal JavaScript bundler, version 0.1.1, specifically designed for educational purposes rather than production use. It aims to demystify the core concepts behind modern bundlers like webpack and Rollup by implementing fundamental features from scratch. These features include AST parsing using `acorn`, building a comprehensive dependency graph, performing ESM to runtime-compatible transformations, and optionally executing export-level tree-shaking. The package emphasizes readability and simplicity in its codebase structure (e.g., `parser.ts`, `resolver.ts`, `treeshaker.ts`). While there's no explicit release cadence, its low version number suggests ad-hoc updates focused on clarity and concept demonstration. Its key differentiator is its role as a learning tool, providing a transparent view into bundling mechanics, unlike complex production-grade alternatives.
Common errors
-
Error: You must provide an entry file (--entry)
cause The `tripack` CLI was executed without specifying the required `--entry` argument, which points to the main JavaScript file to start bundling from.fixWhen running `tripack` from the command line, always include `--entry <path/to/your/entry.js>` to specify your application's entry point. -
Error: Node.js vX.Y.Z is not supported. tripack requires Node.js >= 20
cause The runtime Node.js version on the system is older than the minimum requirement specified by tripack (Node.js 20 or higher).fixUpdate your Node.js installation to version 20 or later using a tool like `nvm` (Node Version Manager) or by downloading the latest LTS version from the official Node.js website. -
Error: Cannot find module 'acorn' or 'acorn-walk'
cause The core dependencies `acorn` and `acorn-walk`, used for AST parsing, are not installed. This typically happens if `npm install` was not run in the project directory, or if the global `tripack` command cannot locate its dependencies.fixIf running `tripack` locally, ensure you've run `npm install` in the project root. If using a global install, `npm install -g tripack` should handle dependencies, but local issues can occur. Reinstalling globally might help: `npm install -g tripack`.
Warnings
- gotcha Tripack is explicitly designed as an educational project and is not a full replacement for production-grade bundlers. It prioritizes readability and demonstrating core concepts over advanced optimizations, performance, or comprehensive feature sets.
- gotcha The tree-shaking implementation in tripack is export-assignment based and intentionally conservative. It may not achieve the same level of dead code elimination as more sophisticated production bundlers.
- gotcha Tripack detects circular dependencies but does not block the bundling process; instead, it reports them. This can lead to unexpected runtime behavior or hard-to-debug issues if not addressed in the source code.
Install
-
npm install tripack -
yarn add tripack -
pnpm add tripack
Imports
- Bundler
const Bundler = require('tripack');import { Bundler } from 'tripack'; - BundlerOptions
import { BundlerOptions } from 'tripack';import type { BundlerOptions } from 'tripack'; - Parser
import { Parser } from 'tripack';import { Parser } from 'tripack/dist/parser';
Quickstart
npm install -g tripack
# Assuming you have a project structure like:
# my-project/
# src/index.js
# console.log('Hello from index!');
# import { foo } from './foo';
# console.log(foo);
# src/foo.js
# export const foo = 'bar';
# Navigate to your project directory
# cd my-project
# Run tripack to bundle your entry file
tripack --entry src/index.js --out dist/bundle.js --tree-shake
# Verify the bundled output
node dist/bundle.js
# Expected output for the example above:
# Hello from index!
# bar