Built-in Loaders
Bun includes loaders for common file types:JavaScript & TypeScript
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)
Data Formats
Data Formats
.json- JSON (json loader).jsonc- JSON with comments (jsonc loader).json5- JSON5 (json5 loader).toml- TOML (toml loader).yaml,.yml- YAML (yaml loader)
Web Assets
Web Assets
.css- CSS stylesheets (css loader).html- HTML (html loader).md- Markdown (md loader)
Binary & Special
Binary & Special
.wasm- WebAssembly (wasm loader).sqlite,.db- SQLite databases (sqlite loader).node- Native addons (napi loader).txt- Plain text (text loader)
Encoding Loaders
Encoding Loaders
- 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
- No transpilation needed for modern JS
- JSX transformation when using
.jsxextension - Source map generation for debugging
- CommonJS/ESM interoperability
TypeScript Loaders (ts, tsx)
Strips TypeScript types and compiles to JavaScript:TypeScript loader
- Fast type stripping (no type checking)
- Decorator support (both legacy and standard)
tsconfig.jsonpath mapping- Experimental features: decorators, using
JSON Loaders (json, jsonc, json5)
Parses JSON and returns a JavaScript object:JSON loaders
- Uses optimized JSON parser with SIMD
- Direct object construction (no eval)
- Type:
jsonloaders 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
- 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
- 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
sqlite- External database filesqlite_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 insrc/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
- Lazy loading: Dynamically import large files
- Caching: Bun caches transpiled files automatically
- Bundling: Pre-bundle for production to avoid runtime transpilation
Lazy loading example
Related
- TypeScript - TypeScript-specific configuration
- JSX - JSX transformation options
- Modules - Module resolution
- Bundling - Build-time loader configuration