Klap JavaScript Bundler

7.0.11 · maintenance · verified Sun Apr 19

Klap is a zero-configuration, zero-dependency bundler designed for small to medium-sized JavaScript packages. It simplifies the build process by leveraging standard `package.json` entries (`source`, `main`, `module`, `browser`) to define multiple output targets, including CommonJS, ESM, and UMD. Despite its "zero dependency" marketing claim, it internally integrates and orchestrates established tools such as Rollup, Babel, and TypeScript to provide modern JavaScript syntax transforms, JSX support (for frameworks like React, Styled Components, and Emotion), and TypeScript compilation. The current stable version is 7.0.11, though the last significant update to the main GitHub repository was in May 2021, suggesting a maintenance or stable state rather than active, frequent development. Key differentiators include built-in minification, gzip size tracking, and a development server, offering an opinionated, complete build pipeline with minimal setup for library authors.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a TypeScript project with JSX support using `npx klap init tsx`, create a simple React component and a utility function, set up a local development server for testing, and finally build the library for distribution. It showcases `klap`'s zero-config approach for common library development patterns, particularly for React-based packages.

// 1. Initialize a new project with TypeScript and JSX support
// Open your terminal and run:
// npx klap init tsx

// 2. Create your source file (e.g., src/index.tsx)
// This file will be bundled by klap. Klap automatically detects .ts, .tsx, .js, .jsx files.

// src/index.tsx
import React from 'react';

interface GreeterProps {
  name: string;
}

export const Greeter = ({ name }: GreeterProps) => {
  return <h1>Hello, {name}!</h1>;
};

export const sum = (a: number, b: number): number => a + b;

// 3. Add a simple usage example for local development (e.g., examples/app.tsx)
// This file is used for `npm start` and demonstrates library usage, but isn't bundled into the library itself.

// examples/app.tsx
import { Greeter, sum } from '../src'; // Adjust path as needed based on your project structure
import { createRoot } from 'react-dom/client';

const rootElement = document.getElementById('root');
if (rootElement) {
  createRoot(rootElement).render(
    <>
      <Greeter name="Klap User" />
      <p>The sum of 5 and 3 is: {sum(5, 3)}</p>
    </>
  );
} else {
  console.error("Root element not found!");
}

// 4. Run the development server to test your example
// Open your terminal in the project root and run:
// npm start

// 5. Build your library for distribution
// Open your terminal in the project root and run:
// npm run build

// This command will output bundled files (cjs, esm, umd) to the 'dist' directory,
// as configured by the 'main', 'module', and 'browser' fields that 'klap init' sets up in your package.json.

view raw JSON →