.js .cjs .mjs .mts .cts .ts .tsx .jsx .css .json .jsonc .json5 .toml .yaml .yml .txt .wasm .node .html .sh
You can explicitly override the loader for any import using the with { type: "..." } import attribute:
JavaScript and TypeScript
js loader
Default for .cjs and .mjs. Parses JavaScript and applies default transforms like dead-code elimination and tree shaking. Bun does not down-convert syntax.
jsx loader
Default for .js and .jsx. Same as js, but JSX syntax is supported and transpiled to plain JavaScript. The JSX transform is controlled by jsx* fields in tsconfig.json.
ts loader
Default for .ts, .mts, and .cts. Strips TypeScript syntax, then behaves identically to the js loader. Bun does not perform type checking.
tsx loader
Default for .tsx. Transpiles both TypeScript and JSX to vanilla JavaScript.
All four loaders can be used with
require() as well as import. You can require a .ts or .tsx file from a CommonJS module.Data formats
JSON
Default for.json. JSON files can be imported directly:
.json file is passed as a bundler entrypoint, it is converted to a .js module that export defaults the parsed object.
JSONC
Default for.jsonc. JSONC (JSON with Comments) files can be imported directly. Bun strips comments and trailing commas during parsing.
Bun automatically uses the
jsonc loader for tsconfig.json, jsconfig.json, package.json, and bun.lock.TOML
Default for.toml. TOML files can be imported directly using Bun’s fast native TOML parser:
.toml file is passed as a bundler entrypoint, it becomes a .js module that export defaults the parsed object.
YAML
Default for.yaml and .yml. YAML files can be imported directly using Bun’s native YAML parser:
JSON5
Default for.json5. JSON5 is a superset of JSON that supports comments, trailing commas, unquoted keys, and single-quoted strings:
Text and static files
text loader
Default for .txt. The file contents are imported as a string:
.txt file is passed as a bundler entrypoint, it becomes a .js module that export defaults the string.
file loader
Default for all unrecognized file types. The import resolves to a path or URL pointing to the file. Commonly used for media and font assets:
outdir and the import resolves to a relative path. If publicPath is configured, it is used as a prefix:
| Public path | Resolved import |
|---|---|
"" (default) | /logo.svg |
"/assets" | /assets/logo.svg |
"https://cdn.example.com/" | https://cdn.example.com/logo.svg |
Special loaders
html loader
Default for .html. Processes HTML files and bundles all referenced assets:
- Bundles and hashes referenced JavaScript (
<script src="...">) - Bundles and hashes referenced CSS (
<link rel="stylesheet" href="...">) - Hashes referenced images (
<img src="...">) - Preserves external URLs (
http://andhttps://)
The
html loader behaves differently depending on context:- Static build (
bun build ./index.html): produces a static site with bundled and hashed assets. - Runtime (
bun run server.tsimporting an HTML file): bundles assets on the fly during development with hot module replacement. - Full-stack build (
bun build --target=bun server.ts): the import resolves to a manifest object thatBun.serveuses to serve pre-bundled assets in production.
css loader
Default for .css. CSS files can be imported directly. No value is returned from the import — it is used for side effects only, primarily in full-stack applications:
napi loader
Default for .node. Native Node.js addons can be imported directly at runtime:
.node files are handled by the file loader.
sqlite loader
Triggered by the with { type: "sqlite" } import attribute. Imports an SQLite database directly using bun:sqlite:
target is bun.
sh loader
Default for .sh files. Executes Bun Shell scripts. Only available at runtime (not in the bundler):