Skip to main content
Bun supports multiple file types through its loader system. Each loader determines how a file is processed and executed.

Built-in Loaders

Bun includes loaders for common file types:

JavaScript & TypeScript

  • .js - JavaScript (js loader)
  • .mjs - ES module JavaScript (js loader)
  • .cjs - CommonJS JavaScript (js loader)
  • .ts - TypeScript (ts loader)
  • .mts - TypeScript ES module (ts loader)
  • .cts - TypeScript CommonJS (ts loader)
  • .jsx - JavaScript with JSX (jsx loader)
  • .tsx - TypeScript with JSX (tsx loader)
  • .json - JSON (json loader)
  • .jsonc - JSON with comments (jsonc loader)
  • .json5 - JSON5 (json5 loader)
  • .toml - TOML (toml loader)
  • .yaml, .yml - YAML (yaml loader)
  • .css - CSS stylesheets (css loader)
  • .html - HTML (html loader)
  • .md - Markdown (md loader)
  • .wasm - WebAssembly (wasm loader)
  • .sqlite, .db - SQLite databases (sqlite loader)
  • .node - Native addons (napi loader)
  • .txt - Plain text (text loader)
  • base64 - Base64 encoding
  • dataurl - Data URL encoding
  • file - Copy file to output directory

Automatic Loader Selection

Bun automatically selects the appropriate loader based on file extension:
Automatic loader selection

Loader Details

JavaScript Loaders (js, jsx)

Processes JavaScript files with optional JSX transformation:
JS loader features
Features:
  • No transpilation needed for modern JS
  • JSX transformation when using .jsx extension
  • Source map generation for debugging
  • CommonJS/ESM interoperability

TypeScript Loaders (ts, tsx)

Strips TypeScript types and compiles to JavaScript:
TypeScript loader
Features:
  • Fast type stripping (no type checking)
  • Decorator support (both legacy and standard)
  • tsconfig.json path mapping
  • Experimental features: decorators, using

JSON Loaders (json, jsonc, json5)

Parses JSON and returns a JavaScript object:
JSON loaders
Performance:
  • Uses optimized JSON parser with SIMD
  • Direct object construction (no eval)
  • Type: json loaders return immutable objects

Text Loader

Returns file contents as a string:
Text loader

Base64 & DataURL Loaders

Encode files as base64 or data URLs:
Encoding loaders
Use cases:
  • Embedding small assets in bundles
  • Inline images in HTML/CSS
  • Data URIs for web APIs

File Loader

Copies files to output directory and returns the path:
File loader
Behavior:
  • Copies file to public directory
  • Generates content-hashed filename
  • Returns public path to the file

WASM Loader

Loads WebAssembly modules:
WASM loader

SQLite Loader

Loads SQLite databases:
SQLite loader
Variants:
  • sqlite - External database file
  • sqlite_embedded - Embeds database in binary

NAPI Loader

Loads native Node.js addons:
NAPI loader

Configuring Loaders

CLI Flags

Override the loader for specific extensions:
CLI loader configuration

bunfig.toml

Persistent loader configuration:
bunfig.toml

Bun.build API

Programmatic loader configuration:
Bun.build loaders

Import Attributes

Explicitly specify loader with import attributes:
Import attributes

Loader Implementation

Loaders are implemented in src/options.zig:
Loader enum (src/options.zig:617)

Custom Loaders (Plugins)

Extend Bun with custom loaders using plugins:
Custom loader plugin

Performance Considerations

Loaders execute during import, affecting startup time:
  • Fast: js, ts, json - minimal overhead
  • Medium: jsx, tsx, toml, yaml - requires parsing
  • Slow: Large binary files (wasm, sqlite)

Optimization Tips

  1. Lazy loading: Dynamically import large files
  2. Caching: Bun caches transpiled files automatically
  3. Bundling: Pre-bundle for production to avoid runtime transpilation
Lazy loading example
  • TypeScript - TypeScript-specific configuration
  • JSX - JSX transformation options
  • Modules - Module resolution
  • Bundling - Build-time loader configuration